Skip to content

Commit 8dade80

Browse files
authored
Merge pull request #2202 from nlohmann/issue2189
Add option to not rely on Internet connectivity during test stage
2 parents 74c6e42 + 2499200 commit 8dade80

File tree

4 files changed

+23
-15
lines changed

4 files changed

+23
-15
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1557,4 +1557,6 @@ $ cmake --build .
15571557
$ ctest --output-on-failure
15581558
```
15591559

1560-
For more information, have a look at the file [.travis.yml](https://github.com/nlohmann/json/blob/master/.travis.yml).
1560+
Note that during the `ctest` stage, several JSON test files are downloaded from an [external repository](https://github.com/nlohmann/json_test_data). If policies forbid downloading artifacts during testing, you can download the files yourself and pass the directory with the test files via `-DJSON_TestDataDirectory=path` to CMake. Then, no Internet connectivity is required. See [issue #2189](https://github.com/nlohmann/json/issues/2189) for more information.
1561+
1562+
In case you have downloaded the library rather than checked out the code via Git, test `cmake_fetch_content_configure`. Please execute `ctest -LE git_required` to skip these tests. See [issue #2189](https://github.com/nlohmann/json/issues/2189) for more information.

cmake/download_test_data.cmake

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
1-
find_package(Git)
2-
31
set(JSON_TEST_DATA_URL https://github.com/nlohmann/json_test_data)
42
set(JSON_TEST_DATA_VERSION 3.0.0)
53

6-
# target to download test data
7-
add_custom_target(download_test_data
8-
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
9-
COMMENT "Downloading test data from ${JSON_TEST_DATA_URL} (v${JSON_TEST_DATA_VERSION})"
10-
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
11-
)
12-
13-
# create a header with the path to the downloaded test data
14-
file(WRITE ${CMAKE_BINARY_DIR}/include/test_data.hpp "#define TEST_DATA_DIRECTORY \"${CMAKE_BINARY_DIR}/json_test_data\"\n")
4+
# if variable is set, use test data from given directory rather than downloading them
5+
if(JSON_TestDataDirectory)
6+
message(STATUS "Using test data in ${JSON_TestDataDirectory}.")
7+
add_custom_target(download_test_data)
8+
file(WRITE ${CMAKE_BINARY_DIR}/include/test_data.hpp "#define TEST_DATA_DIRECTORY \"${JSON_TestDataDirectory}\"\n")
9+
else()
10+
find_package(Git)
11+
# target to download test data
12+
add_custom_target(download_test_data
13+
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
14+
COMMENT "Downloading test data from ${JSON_TEST_DATA_URL} (v${JSON_TEST_DATA_VERSION})"
15+
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
16+
)
17+
# create a header with the path to the downloaded test data
18+
file(WRITE ${CMAKE_BINARY_DIR}/include/test_data.hpp "#define TEST_DATA_DIRECTORY \"${CMAKE_BINARY_DIR}/json_test_data\"\n")
19+
endif()
1520

1621
# determine the operating system (for debug and support purposes)
1722
find_program(UNAME_COMMAND uname)

test/cmake_fetch_content/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ if (${CMAKE_VERSION} VERSION_GREATER "3.11.0")
1111
)
1212
set_tests_properties(cmake_fetch_content_configure PROPERTIES
1313
FIXTURES_SETUP cmake_fetch_content
14+
LABELS git_required
1415
)
1516
set_tests_properties(cmake_fetch_content_build PROPERTIES
1617
FIXTURES_REQUIRED cmake_fetch_content
18+
LABELS git_required
1719
)
1820
endif()

test/cmake_fetch_content/project/CMakeLists.txt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ project(DummyImport CXX)
44

55
include(FetchContent)
66

7-
FetchContent_Declare(json
8-
GIT_REPOSITORY ${CMAKE_CURRENT_SOURCE_DIR}/../../..
9-
GIT_TAG HEAD)
7+
get_filename_component(GIT_REPOSITORY_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../../.. ABSOLUTE)
8+
FetchContent_Declare(json GIT_REPOSITORY ${GIT_REPOSITORY_DIRECTORY} GIT_TAG HEAD)
109

1110
FetchContent_GetProperties(json)
1211
if(NOT json_POPULATED)

0 commit comments

Comments
 (0)