From 7ade3a0efb6b302d25a814648cfb4e79ce95299e Mon Sep 17 00:00:00 2001
From: Niels Lohmann <mail@nlohmann.me>
Date: Fri, 1 May 2020 15:14:37 +0200
Subject: [PATCH] :heavy_minus_sign: remove dependency to FetchContent

---
 test/CMakeLists.txt       | 40 +++++++++++++++++----------------------
 test/src/test_data.hpp.in |  2 +-
 2 files changed, 18 insertions(+), 24 deletions(-)

diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 18c76aef..45bffe02 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -3,22 +3,16 @@ option(JSON_Valgrind "Execute test suite with Valgrind" OFF)
 option(JSON_NoExceptions "Build test suite without exceptions" OFF)
 option(JSON_Coverage "Build test suite with coverage information" OFF)
 
-if (${CMAKE_VERSION} VERSION_LESS "3.11.0")
-    message(FATAL_ERROR "The test suite needs at least CMake 3.11. This is CMake ${CMAKE_VERSION}.")
-endif()
-
-include(FetchContent)
-FetchContent_Declare(nlohmann_json_test_data
-    GIT_REPOSITORY https://github.com/nlohmann/json_test_data.git
-    GIT_SHALLOW TRUE
+# target to download test data
+add_custom_target(download_test_data
+    COMMAND test -d json_test_data || git clone https://github.com/nlohmann/json_test_data.git --quiet --depth 1
+    COMMENT "Downloading test data from https://github.com/nlohmann/json_test_data"
+    WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
 )
 
-FetchContent_GetProperties(nlohmann_json_test_data)
-if(NOT nlohmann_json_test_data_POPULATED)
-    message(STATUS "Download test data")
-    FetchContent_Populate(nlohmann_json_test_data)
-    message(STATUS "Download test data - done")
-endif()
+# test fixture to download test data
+add_test(NAME "download_test_data"  COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target download_test_data)
+set_tests_properties(download_test_data PROPERTIES FIXTURES_SETUP TEST_DATA)
 
 configure_file(src/test_data.hpp.in include/test_data.hpp)
 
@@ -168,7 +162,7 @@ foreach(file ${files})
 
     add_executable(${testcase} $<TARGET_OBJECTS:doctest_main> ${file})
     target_compile_definitions(${testcase} PRIVATE
-      DOCTEST_CONFIG_SUPER_FAST_ASSERTS
+        DOCTEST_CONFIG_SUPER_FAST_ASSERTS
     )
     target_compile_options(${testcase} PRIVATE
         $<$<CXX_COMPILER_ID:MSVC>:/EHsc;$<$<CONFIG:Release>:/Od>>
@@ -183,21 +177,21 @@ foreach(file ${files})
     target_link_libraries(${testcase} ${NLOHMANN_JSON_TARGET_NAME})
 
     add_test(NAME "${testcase}_default"
-      COMMAND ${testcase} ${DOCTEST_TEST_FILTER}
-      WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
+        COMMAND ${testcase} ${DOCTEST_TEST_FILTER}
+        WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
     )
-    set_tests_properties("${testcase}_default" PROPERTIES LABELS "default")
+    set_tests_properties("${testcase}_default" PROPERTIES LABELS "default" FIXTURES_REQUIRED TEST_DATA)
 
     add_test(NAME "${testcase}_all"
-      COMMAND ${testcase} ${DOCTEST_TEST_FILTER} --no-skip
-      WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
+        COMMAND ${testcase} ${DOCTEST_TEST_FILTER} --no-skip
+        WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
     )
-    set_tests_properties("${testcase}_all" PROPERTIES LABELS "all")
+    set_tests_properties("${testcase}_all" PROPERTIES LABELS "all" FIXTURES_REQUIRED TEST_DATA)
 
     if(JSON_Valgrind)
         add_test(NAME "${testcase}_valgrind"
-          COMMAND ${memcheck_command} ${CMAKE_CURRENT_BINARY_DIR}/${testcase} ${DOCTEST_TEST_FILTER}
-          WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
+            COMMAND ${memcheck_command} ${CMAKE_CURRENT_BINARY_DIR}/${testcase} ${DOCTEST_TEST_FILTER}
+            WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
         )
         set_tests_properties("${testcase}_valgrind" PROPERTIES LABELS "valgrind")
     endif()
diff --git a/test/src/test_data.hpp.in b/test/src/test_data.hpp.in
index 6da92801..ac66486e 100644
--- a/test/src/test_data.hpp.in
+++ b/test/src/test_data.hpp.in
@@ -1 +1 @@
-#define TEST_DATA_DIRECTORY "${nlohmann_json_test_data_SOURCE_DIR}"
+#define TEST_DATA_DIRECTORY "${CMAKE_BINARY_DIR}/json_test_data"