From 9ea2eb70279819fdc9e13387b361ad08b61b27d9 Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Sat, 24 Sep 2022 13:49:16 +0200 Subject: [PATCH] Refactor CMake test data directory code * Add a cache variable and set default from environment. * Add '.git' suffix to URL variable. --- cmake/json_opts.cmake | 11 +++-------- cmake/json_summary.cmake | 6 +++--- cmake/json_test.cmake | 6 +++--- 3 files changed, 9 insertions(+), 14 deletions(-) diff --git a/cmake/json_opts.cmake b/cmake/json_opts.cmake index 3483b15f3f..b79e1c9f6a 100644 --- a/cmake/json_opts.cmake +++ b/cmake/json_opts.cmake @@ -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.") @@ -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) diff --git a/cmake/json_summary.cmake b/cmake/json_summary.cmake index 3abbe1a279..58075f97e6 100644 --- a/cmake/json_summary.cmake +++ b/cmake/json_summary.cmake @@ -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?") diff --git a/cmake/json_test.cmake b/cmake/json_test.cmake index 8a0240a06c..6ece47d59f 100644 --- a/cmake/json_test.cmake +++ b/cmake/json_test.cmake @@ -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} )