Skip to content

Commit

Permalink
Refactor CMake test data directory code
Browse files Browse the repository at this point in the history
* Add a cache variable and set default from environment.
* Add '.git' suffix to URL variable.
  • Loading branch information
falbrechtskirchinger committed Sep 24, 2022
1 parent 9fc6d2b commit 9ea2eb7
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 14 deletions.
11 changes: 3 additions & 8 deletions cmake/json_opts.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ endif()
option(JSON_BuildTests "Build the unit tests when BUILD_TESTING is enabled." ${JSON_BuildTests_INIT})
set(JSON_32bitTest AUTO CACHE STRING "Enable the 32bit unit test (ON/OFF/AUTO/ONLY).")
cmake_dependent_option(JSON_FastTests "Skip expensive/slow tests." OFF "JSON_BuildTests" OFF)
set(JSON_TestDataDirectory "$ENV{JSON_TEST_DATA_DIRECTORY}" CACHE FILEPATH "Test data directory for the unit tests (will be downloaded if not specified).")
cmake_dependent_option(JSON_Valgrind "Execute test suite with Valgrind." OFF "JSON_BuildTests" OFF)

set(JSON_TestStandards "" CACHE STRING "The list of standards to test explicitly.")
Expand Down Expand Up @@ -63,11 +64,5 @@ if (JSON_SystemInclude)
set(NLOHMANN_JSON_SYSTEM_INCLUDE "SYSTEM")
endif()

if(JSON_TestDataDirectory)
set(JSON_TEST_DATA_DIRECTORY "${JSON_TestDataDirectory}" CACHE INTERNAL "")
elseif(DEFINED ENV{JSON_TEST_DATA_DIRECTORY})
set(JSON_TEST_DATA_DIRECTORY "$ENV{JSON_TEST_DATA_DIRECTORY}" CACHE INTERNAL "")
endif()

set(JSON_TEST_DATA_URL https://github.com/nlohmann/json_test_data CACHE INTERNAL "")
set(JSON_TEST_DATA_VERSION 3.1.0 CACHE INTERNAL "")
set(JSON_TEST_DATA_URL https://github.com/nlohmann/json_test_data.git)
set(JSON_TEST_DATA_VERSION 3.1.0)
6 changes: 3 additions & 3 deletions cmake/json_summary.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ if(JSON_BuildTests)
json_feature(JSON_32bitTest "Build the 32bit unit test?" VALUES AUTO ONLY)
json_feature(JSON_FastTests "Skip expensive/slow tests?")

if(JSON_TEST_DATA_DIRECTORY)
message(" Test data: ${JSON_TEST_DATA_DIRECTORY}")
if(JSON_TestDataDirectory)
message(" Test data directory: ${JSON_TestDataDirectory}")
else()
message(" Test data: ${JSON_TEST_DATA_URL} (v${JSON_TEST_DATA_VERSION})")
message(" Test data source: ${JSON_TEST_DATA_URL} (v${JSON_TEST_DATA_VERSION})")
endif()

json_feature(JSON_Valgrind "Execute test suite with Valgrind?")
Expand Down
6 changes: 3 additions & 3 deletions cmake/json_test.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ set(_json_test_cmake_list_file ${CMAKE_CURRENT_LIST_FILE})
#############################################################################

# if variable is set, use test data from given directory rather than downloading them
if(JSON_TEST_DATA_DIRECTORY)
if(JSON_TestDataDirectory)
add_custom_target(download_test_data)
file(WRITE ${CMAKE_BINARY_DIR}/include/test_data.hpp "#define TEST_DATA_DIRECTORY \"${JSON_TEST_DATA_DIRECTORY}\"\n")
file(WRITE ${CMAKE_BINARY_DIR}/include/test_data.hpp "#define TEST_DATA_DIRECTORY \"${JSON_TestDataDirectory}\"\n")
else()
find_package(Git REQUIRED)
# target to download test data
add_custom_target(download_test_data
COMMAND test -d json_test_data || ${GIT_EXECUTABLE} clone -c advice.detachedHead=false --branch v${JSON_TEST_DATA_VERSION} ${JSON_TEST_DATA_URL}.git --quiet --depth 1
COMMAND test -d json_test_data || ${GIT_EXECUTABLE} clone -c advice.detachedHead=false --branch v${JSON_TEST_DATA_VERSION} ${JSON_TEST_DATA_URL} --quiet --depth 1
COMMENT "Downloading test data from ${JSON_TEST_DATA_URL} (v${JSON_TEST_DATA_VERSION})"
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)
Expand Down

0 comments on commit 9ea2eb7

Please sign in to comment.