From 1e56634faa44c9a26b0e7a4f529396d761f8833d Mon Sep 17 00:00:00 2001 From: Craig Scott Date: Sun, 24 Sep 2017 19:50:58 +1000 Subject: FetchContent: Add tests --- Tests/RunCMake/CMakeLists.txt | 1 + Tests/RunCMake/FetchContent/CMakeLists.txt | 3 + Tests/RunCMake/FetchContent/DirOverrides.cmake | 46 +++++++++++++ .../FetchContent/DirectIgnoresDetails-stdout.txt | 1 + .../FetchContent/DirectIgnoresDetails.cmake | 12 ++++ .../RunCMake/FetchContent/DownloadTwice-result.txt | 1 + .../RunCMake/FetchContent/DownloadTwice-stderr.txt | 1 + Tests/RunCMake/FetchContent/DownloadTwice.cmake | 9 +++ .../FetchContent/FirstDetailsWin-stdout.txt | 1 + Tests/RunCMake/FetchContent/FirstDetailsWin.cmake | 16 +++++ Tests/RunCMake/FetchContent/GetProperties.cmake | 67 +++++++++++++++++++ .../FetchContent/MissingDetails-result.txt | 1 + .../FetchContent/MissingDetails-stderr.txt | 1 + Tests/RunCMake/FetchContent/MissingDetails.cmake | 3 + Tests/RunCMake/FetchContent/RunCMakeTest.cmake | 28 ++++++++ Tests/RunCMake/FetchContent/SameGenerator.cmake | 17 +++++ Tests/RunCMake/FetchContent/ScriptMode.cmake | 35 ++++++++++ Tests/RunCMake/FetchContent/VarDefinitions.cmake | 75 ++++++++++++++++++++++ 18 files changed, 318 insertions(+) create mode 100644 Tests/RunCMake/FetchContent/CMakeLists.txt create mode 100644 Tests/RunCMake/FetchContent/DirOverrides.cmake create mode 100644 Tests/RunCMake/FetchContent/DirectIgnoresDetails-stdout.txt create mode 100644 Tests/RunCMake/FetchContent/DirectIgnoresDetails.cmake create mode 100644 Tests/RunCMake/FetchContent/DownloadTwice-result.txt create mode 100644 Tests/RunCMake/FetchContent/DownloadTwice-stderr.txt create mode 100644 Tests/RunCMake/FetchContent/DownloadTwice.cmake create mode 100644 Tests/RunCMake/FetchContent/FirstDetailsWin-stdout.txt create mode 100644 Tests/RunCMake/FetchContent/FirstDetailsWin.cmake create mode 100644 Tests/RunCMake/FetchContent/GetProperties.cmake create mode 100644 Tests/RunCMake/FetchContent/MissingDetails-result.txt create mode 100644 Tests/RunCMake/FetchContent/MissingDetails-stderr.txt create mode 100644 Tests/RunCMake/FetchContent/MissingDetails.cmake create mode 100644 Tests/RunCMake/FetchContent/RunCMakeTest.cmake create mode 100644 Tests/RunCMake/FetchContent/SameGenerator.cmake create mode 100644 Tests/RunCMake/FetchContent/ScriptMode.cmake create mode 100644 Tests/RunCMake/FetchContent/VarDefinitions.cmake diff --git a/Tests/RunCMake/CMakeLists.txt b/Tests/RunCMake/CMakeLists.txt index 57cc144..7eafa06 100644 --- a/Tests/RunCMake/CMakeLists.txt +++ b/Tests/RunCMake/CMakeLists.txt @@ -327,6 +327,7 @@ add_RunCMake_test(install) add_RunCMake_test(CPackConfig) add_RunCMake_test(CPackInstallProperties) add_RunCMake_test(ExternalProject) +add_RunCMake_test(FetchContent) add_RunCMake_test(CTestCommandLine) # Only run this test on unix platforms that support # symbolic links diff --git a/Tests/RunCMake/FetchContent/CMakeLists.txt b/Tests/RunCMake/FetchContent/CMakeLists.txt new file mode 100644 index 0000000..d3137f6 --- /dev/null +++ b/Tests/RunCMake/FetchContent/CMakeLists.txt @@ -0,0 +1,3 @@ +cmake_minimum_required(VERSION 3.9) +project(${RunCMake_TEST} NONE) +include(${RunCMake_TEST}.cmake) diff --git a/Tests/RunCMake/FetchContent/DirOverrides.cmake b/Tests/RunCMake/FetchContent/DirOverrides.cmake new file mode 100644 index 0000000..50eef16 --- /dev/null +++ b/Tests/RunCMake/FetchContent/DirOverrides.cmake @@ -0,0 +1,46 @@ +include(FetchContent) + +# Test using saved details +FetchContent_Declare( + t1 + SOURCE_DIR ${CMAKE_CURRENT_BINARY_DIR}/savedSrc + DOWNLOAD_COMMAND ${CMAKE_COMMAND} -E make_directory +) +FetchContent_Populate(t1) +if(NOT IS_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/savedSrc) + message(FATAL_ERROR "Saved details SOURCE_DIR override failed") +endif() + +# Test direct population +FetchContent_Populate( + t2 + SOURCE_DIR ${CMAKE_CURRENT_BINARY_DIR}/directSrc + DOWNLOAD_COMMAND ${CMAKE_COMMAND} -E make_directory +) +if(NOT IS_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/directSrc) + message(FATAL_ERROR "Direct details SOURCE_DIR override failed") +endif() + +# Ensure setting BINARY_DIR to SOURCE_DIR works (a technique to +# prevent an unwanted separate BINARY_DIR from being created, which +# ExternalProject_Add() does whether we like it or not) +FetchContent_Declare( + t3 + SOURCE_DIR ${CMAKE_CURRENT_BINARY_DIR}/savedNoBuildDir + BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/savedNoBuildDir + DOWNLOAD_COMMAND ${CMAKE_COMMAND} -E make_directory +) +FetchContent_Populate(t3) +if(IS_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/savedNobuildDir-build) + message(FATAL_ERROR "Saved details BINARY_DIR override failed") +endif() + +FetchContent_Populate( + t4 + SOURCE_DIR ${CMAKE_CURRENT_BINARY_DIR}/directNoBuildDir + BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/directNoBuildDir + DOWNLOAD_COMMAND ${CMAKE_COMMAND} -E make_directory +) +if(IS_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/savedNobuildDir-build) + message(FATAL_ERROR "Direct details BINARY_DIR override failed") +endif() diff --git a/Tests/RunCMake/FetchContent/DirectIgnoresDetails-stdout.txt b/Tests/RunCMake/FetchContent/DirectIgnoresDetails-stdout.txt new file mode 100644 index 0000000..6fa5a57 --- /dev/null +++ b/Tests/RunCMake/FetchContent/DirectIgnoresDetails-stdout.txt @@ -0,0 +1 @@ +Local details used diff --git a/Tests/RunCMake/FetchContent/DirectIgnoresDetails.cmake b/Tests/RunCMake/FetchContent/DirectIgnoresDetails.cmake new file mode 100644 index 0000000..0731b43 --- /dev/null +++ b/Tests/RunCMake/FetchContent/DirectIgnoresDetails.cmake @@ -0,0 +1,12 @@ +include(FetchContent) + +FetchContent_Declare( + t1 + DOWNLOAD_COMMAND ${CMAKE_COMMAND} -E echo "Saved details used" +) + +# No QUIET option given, so command output will be shown +FetchContent_Populate( + t1 + DOWNLOAD_COMMAND ${CMAKE_COMMAND} -E echo "Local details used" +) diff --git a/Tests/RunCMake/FetchContent/DownloadTwice-result.txt b/Tests/RunCMake/FetchContent/DownloadTwice-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/FetchContent/DownloadTwice-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/FetchContent/DownloadTwice-stderr.txt b/Tests/RunCMake/FetchContent/DownloadTwice-stderr.txt new file mode 100644 index 0000000..96fed48 --- /dev/null +++ b/Tests/RunCMake/FetchContent/DownloadTwice-stderr.txt @@ -0,0 +1 @@ +Content t1 already populated in diff --git a/Tests/RunCMake/FetchContent/DownloadTwice.cmake b/Tests/RunCMake/FetchContent/DownloadTwice.cmake new file mode 100644 index 0000000..6863c30 --- /dev/null +++ b/Tests/RunCMake/FetchContent/DownloadTwice.cmake @@ -0,0 +1,9 @@ +include(FetchContent) + +FetchContent_Declare( + t1 + DOWNLOAD_COMMAND ${CMAKE_COMMAND} -E echo "Download command executed" +) + +FetchContent_Populate(t1) +FetchContent_Populate(t1) # Triggers error diff --git a/Tests/RunCMake/FetchContent/FirstDetailsWin-stdout.txt b/Tests/RunCMake/FetchContent/FirstDetailsWin-stdout.txt new file mode 100644 index 0000000..7a8bf18 --- /dev/null +++ b/Tests/RunCMake/FetchContent/FirstDetailsWin-stdout.txt @@ -0,0 +1 @@ +First details used diff --git a/Tests/RunCMake/FetchContent/FirstDetailsWin.cmake b/Tests/RunCMake/FetchContent/FirstDetailsWin.cmake new file mode 100644 index 0000000..208b12d --- /dev/null +++ b/Tests/RunCMake/FetchContent/FirstDetailsWin.cmake @@ -0,0 +1,16 @@ +include(FetchContent) + +# Need to see the download command output +set(FETCHCONTENT_QUIET OFF) + +FetchContent_Declare( + t1 + DOWNLOAD_COMMAND ${CMAKE_COMMAND} -E echo "First details used" +) + +FetchContent_Declare( + t1 + DOWNLOAD_COMMAND ${CMAKE_COMMAND} -E echo "Second details used" +) + +FetchContent_Populate(t1) diff --git a/Tests/RunCMake/FetchContent/GetProperties.cmake b/Tests/RunCMake/FetchContent/GetProperties.cmake new file mode 100644 index 0000000..61c99fe --- /dev/null +++ b/Tests/RunCMake/FetchContent/GetProperties.cmake @@ -0,0 +1,67 @@ +include(FetchContent) + +# First confirm properties are empty even before declare +FetchContent_GetProperties(t1) +if(t1_POPULATED) + message(FATAL_ERROR "Property says populated before doing anything") +endif() +if(t1_SOURCE_DIR) + message(FATAL_ERROR "SOURCE_DIR property not initially empty") +endif() +if(t1_BINARY_DIR) + message(FATAL_ERROR "BINARY_DIR property not initially empty") +endif() + +# Declare, but no properties should change yet +FetchContent_Declare( + t1 + SOURCE_DIR ${CMAKE_CURRENT_BINARY_DIR}/savedSrc + BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/savedBin + DOWNLOAD_COMMAND ${CMAKE_COMMAND} -E echo "Do nothing" +) + +FetchContent_GetProperties(t1) +if(t1_POPULATED) + message(FATAL_ERROR "Property says populated after only declaring details") +endif() +if(t1_SOURCE_DIR) + message(FATAL_ERROR "SOURCE_DIR property not empty after declare") +endif() +if(t1_BINARY_DIR) + message(FATAL_ERROR "BINARY_DIR property not empty after declare") +endif() + +# Populate should make all properties non-empty/set +FetchContent_Populate(t1) + +FetchContent_GetProperties(t1) +if(NOT t1_POPULATED) + message(FATAL_ERROR "Population did not set POPULATED property") +endif() +if(NOT "${t1_SOURCE_DIR}" STREQUAL "${CMAKE_CURRENT_BINARY_DIR}/savedSrc") + message(FATAL_ERROR "SOURCE_DIR property not correct after population: " + "${t1_SOURCE_DIR}\n" + " Expected: ${CMAKE_CURRENT_BINARY_DIR}/savedSrc") +endif() +if(NOT "${t1_BINARY_DIR}" STREQUAL "${CMAKE_CURRENT_BINARY_DIR}/savedBin") + message(FATAL_ERROR "BINARY_DIR property not correct after population: " + "${t1_BINARY_DIR}\n" + " Expected: ${CMAKE_CURRENT_BINARY_DIR}/savedBin") +endif() + +# Verify we can retrieve properties individually too +FetchContent_GetProperties(t1 POPULATED varPop) +FetchContent_GetProperties(t1 SOURCE_DIR varSrc) +FetchContent_GetProperties(t1 BINARY_DIR varBin) + +if(NOT varPop) + message(FATAL_ERROR "Failed to retrieve POPULATED property") +endif() +if(NOT "${varSrc}" STREQUAL "${CMAKE_CURRENT_BINARY_DIR}/savedSrc") + message(FATAL_ERROR "SOURCE_DIR property not retrieved correctly: ${varSrc}\n" + " Expected: ${CMAKE_CURRENT_BINARY_DIR}/savedSrc") +endif() +if(NOT "${varBin}" STREQUAL "${CMAKE_CURRENT_BINARY_DIR}/savedBin") + message(FATAL_ERROR "BINARY_DIR property not retrieved correctly: ${varBin}\n" + " Expected: ${CMAKE_CURRENT_BINARY_DIR}/savedBin") +endif() diff --git a/Tests/RunCMake/FetchContent/MissingDetails-result.txt b/Tests/RunCMake/FetchContent/MissingDetails-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/FetchContent/MissingDetails-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/FetchContent/MissingDetails-stderr.txt b/Tests/RunCMake/FetchContent/MissingDetails-stderr.txt new file mode 100644 index 0000000..c4f1daf --- /dev/null +++ b/Tests/RunCMake/FetchContent/MissingDetails-stderr.txt @@ -0,0 +1 @@ +No content details recorded for t1 diff --git a/Tests/RunCMake/FetchContent/MissingDetails.cmake b/Tests/RunCMake/FetchContent/MissingDetails.cmake new file mode 100644 index 0000000..ba8d121 --- /dev/null +++ b/Tests/RunCMake/FetchContent/MissingDetails.cmake @@ -0,0 +1,3 @@ +include(FetchContent) + +FetchContent_Populate(t1) diff --git a/Tests/RunCMake/FetchContent/RunCMakeTest.cmake b/Tests/RunCMake/FetchContent/RunCMakeTest.cmake new file mode 100644 index 0000000..621fb8b --- /dev/null +++ b/Tests/RunCMake/FetchContent/RunCMakeTest.cmake @@ -0,0 +1,28 @@ +include(RunCMake) + +unset(RunCMake_TEST_NO_CLEAN) + +run_cmake(MissingDetails) +run_cmake(DirectIgnoresDetails) +run_cmake(FirstDetailsWin) +run_cmake(DownloadTwice) +run_cmake(SameGenerator) +run_cmake(VarDefinitions) +run_cmake(GetProperties) +run_cmake(DirOverrides) + +# We need to pass through CMAKE_GENERATOR and CMAKE_MAKE_PROGRAM +# to ensure the test can run on machines where the build tool +# isn't on the PATH. Some build slaves explicitly test with such +# an arrangement (e.g. to test with spaces in the path). We also +# pass through the platform and toolset for completeness, even +# though we don't build anything, just in case this somehow affects +# the way the build tool is invoked. +run_cmake_command(ScriptMode + ${CMAKE_COMMAND} + -DCMAKE_GENERATOR=${RunCMake_GENERATOR} + -DCMAKE_GENERATOR_PLATFORM=${RunCMake_GENERATOR_PLATFORM} + -DCMAKE_GENERATOR_TOOLSET=${RunCMake_GENERATOR_TOOLSET} + -DCMAKE_MAKE_PROGRAM=${RunCMake_MAKE_PROGRAM} + -P ${CMAKE_CURRENT_LIST_DIR}/ScriptMode.cmake +) diff --git a/Tests/RunCMake/FetchContent/SameGenerator.cmake b/Tests/RunCMake/FetchContent/SameGenerator.cmake new file mode 100644 index 0000000..58204ef --- /dev/null +++ b/Tests/RunCMake/FetchContent/SameGenerator.cmake @@ -0,0 +1,17 @@ +include(FetchContent) + +FetchContent_Declare( + t1 + DOWNLOAD_COMMAND ${CMAKE_COMMAND} -E echo "Download command executed" +) + +FetchContent_Populate(t1) + +file(STRINGS "${FETCHCONTENT_BASE_DIR}/t1-subbuild/CMakeCache.txt" + matchLine REGEX "^CMAKE_GENERATOR:.*=" + LIMIT_COUNT 1 +) +if(NOT matchLine MATCHES "${CMAKE_GENERATOR}") + message(FATAL_ERROR "Generator line mismatch: ${matchLine}\n" + " Expected type: ${CMAKE_GENERATOR}") +endif() diff --git a/Tests/RunCMake/FetchContent/ScriptMode.cmake b/Tests/RunCMake/FetchContent/ScriptMode.cmake new file mode 100644 index 0000000..0a93d62 --- /dev/null +++ b/Tests/RunCMake/FetchContent/ScriptMode.cmake @@ -0,0 +1,35 @@ +include(FetchContent) + +file(WRITE tmpFile.txt "Generated contents, not important") + +FetchContent_Populate( + t1 + DOWNLOAD_COMMAND ${CMAKE_COMMAND} -E copy + ${CMAKE_CURRENT_BINARY_DIR}/tmpFile.txt + /done1.txt +) +if(NOT EXISTS ${CMAKE_CURRENT_BINARY_DIR}/t1-src/done1.txt) + message(FATAL_ERROR "Default SOURCE_DIR doesn't contain done1.txt") +endif() + +FetchContent_Populate( + t2 + SOURCE_DIR ${CMAKE_CURRENT_BINARY_DIR}/mysrc + DOWNLOAD_COMMAND ${CMAKE_COMMAND} -E copy + ${CMAKE_CURRENT_BINARY_DIR}/tmpFile.txt + /done2.txt +) +if(NOT EXISTS ${CMAKE_CURRENT_BINARY_DIR}/mysrc/done2.txt) + message(FATAL_ERROR "Specified SOURCE_DIR doesn't contain done2.txt") +endif() + +FetchContent_Populate( + t3 + SOURCE_DIR myrelsrc + DOWNLOAD_COMMAND ${CMAKE_COMMAND} -E copy + ${CMAKE_CURRENT_BINARY_DIR}/tmpFile.txt + /done3.txt +) +if(NOT EXISTS ${CMAKE_CURRENT_BINARY_DIR}/myrelsrc/done3.txt) + message(FATAL_ERROR "Relative SOURCE_DIR doesn't contain done3.txt") +endif() diff --git a/Tests/RunCMake/FetchContent/VarDefinitions.cmake b/Tests/RunCMake/FetchContent/VarDefinitions.cmake new file mode 100644 index 0000000..4d2a929 --- /dev/null +++ b/Tests/RunCMake/FetchContent/VarDefinitions.cmake @@ -0,0 +1,75 @@ +unset(FETCHCONTENT_FULLY_DISCONNECTED CACHE) +unset(FETCHCONTENT_UPDATES_DISCONNECTED CACHE) +unset(FETCHCONTENT_QUIET CACHE) +unset(FETCHCONTENT_BASE_DIR CACHE) + +include(FetchContent) + +# Each of the cache entries should be defined and have the +# expected value. Be careful to check unset separately from a +# false value, since unset also equates to false. +if(FETCHCONTENT_FULLY_DISCONNECTED STREQUAL "") + message(FATAL_ERROR "FETCHCONTENT_FULLY_DISCONNECTED not defined") +elseif(FETCHCONTENT_FULLY_DISCONNECTED) + message(FATAL_ERROR "FETCHCONTENT_FULLY_DISCONNECTED not defaulted to OFF") +endif() + +if(FETCHCONTENT_UPDATES_DISCONNECTED STREQUAL "") + message(FATAL_ERROR "FETCHCONTENT_UPDATES_DISCONNECTED not defined") +elseif(FETCHCONTENT_UPDATES_DISCONNECTED) + message(FATAL_ERROR "FETCHCONTENT_UPDATES_DISCONNECTED not defaulted to OFF") +endif() + +if(FETCHCONTENT_QUIET STREQUAL "") + message(FATAL_ERROR "FETCHCONTENT_QUIET not defined") +elseif(NOT FETCHCONTENT_QUIET) + message(FATAL_ERROR "FETCHCONTENT_QUIET not defaulted to ON") +endif() + +if(NOT FETCHCONTENT_BASE_DIR STREQUAL "${CMAKE_BINARY_DIR}/_deps") + message(FATAL_ERROR "FETCHCONTENT_BASE_DIR has default value: " + "${FETCHCONTENT_BASE_DIR}\n Expected: ${CMAKE_BINARY_DIR}/_deps") +endif() + +file(REMOVE_RECURSE ${FETCHCONTENT_BASE_DIR}/t1-subbuild) + +# Use uppercase T1 test name to confirm conversion to lowercase +# for the t1_... variable names that get set +FetchContent_Declare( + T1 + DOWNLOAD_COMMAND ${CMAKE_COMMAND} -E echo "Download command executed" +) +FetchContent_Populate(T1) + +# Be careful to check both regular and cache variables. Since they have +# the same name, we can only confirm them separately by using get_property(). +get_property(srcRegVarSet VARIABLE PROPERTY t1_SOURCE_DIR SET) +get_property(bldRegVarSet VARIABLE PROPERTY t1_BINARY_DIR SET) + +get_property(srcCacheVarSet CACHE t1_SOURCE_DIR PROPERTY VALUE SET) +get_property(bldCacheVarSet CACHE t1_BINARY_DIR PROPERTY VALUE SET) + +if(NOT srcRegVarSet) + message(FATAL_ERROR "t1_SOURCE_DIR regular variable not set") +endif() +if(NOT bldRegVarSet) + message(FATAL_ERROR "t1_BINARY_DIR regular variable not set") +endif() +if(srcCacheVarSet) + message(FATAL_ERROR "t1_SOURCE_DIR cache variable unexpectedly set") +endif() +if(bldCacheVarSet) + message(FATAL_ERROR "t1_BINARY_DIR cache variable unexpectedly set") +endif() + +set(srcRegVar ${t1_SOURCE_DIR}) +set(bldRegVar ${t1_BINARY_DIR}) + +if(NOT srcRegVar STREQUAL "${CMAKE_BINARY_DIR}/_deps/t1-src") + message(FATAL_ERROR "Unexpected t1_SOURCE_DIR value: ${srcRegVar}\n" + " Expected: ${CMAKE_BINARY_DIR}/_deps/t1-src") +endif() +if(NOT bldRegVar STREQUAL "${CMAKE_BINARY_DIR}/_deps/t1-build") + message(FATAL_ERROR "Unexpected t1_BINARY_DIR value: ${bldRegVar}\n" + " Expected: ${CMAKE_BINARY_DIR}/_deps/t1-build") +endif() -- cgit v0.12