FetchContent: Add tests

This commit is contained in:
Craig Scott
2017-09-24 19:50:58 +10:00
parent 60e74d2f19
commit 1e56634faa
18 changed files with 318 additions and 0 deletions

View File

@@ -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

View File

@@ -0,0 +1,3 @@
cmake_minimum_required(VERSION 3.9)
project(${RunCMake_TEST} NONE)
include(${RunCMake_TEST}.cmake)

View File

@@ -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 <SOURCE_DIR>
)
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 <SOURCE_DIR>
)
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 <SOURCE_DIR>
)
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 <SOURCE_DIR>
)
if(IS_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/savedNobuildDir-build)
message(FATAL_ERROR "Direct details BINARY_DIR override failed")
endif()

View File

@@ -0,0 +1 @@
Local details used

View File

@@ -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"
)

View File

@@ -0,0 +1 @@
1

View File

@@ -0,0 +1 @@
Content t1 already populated in

View File

@@ -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

View File

@@ -0,0 +1 @@
First details used

View File

@@ -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)

View File

@@ -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()

View File

@@ -0,0 +1 @@
1

View File

@@ -0,0 +1 @@
No content details recorded for t1

View File

@@ -0,0 +1,3 @@
include(FetchContent)
FetchContent_Populate(t1)

View File

@@ -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
)

View File

@@ -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()

View File

@@ -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
<SOURCE_DIR>/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
<SOURCE_DIR>/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
<SOURCE_DIR>/done3.txt
)
if(NOT EXISTS ${CMAKE_CURRENT_BINARY_DIR}/myrelsrc/done3.txt)
message(FATAL_ERROR "Relative SOURCE_DIR doesn't contain done3.txt")
endif()

View File

@@ -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()