Skip to content

Commit

Permalink
Merge pull request #33 from commontk/test-ExternalProject_SetIfNotDef…
Browse files Browse the repository at this point in the history
…ined-interaction-cache-variable

ExternalProject_SetIfNotDefined: Add test checking interaction with cache variable
  • Loading branch information
jcfr committed Aug 17, 2018
2 parents 78cbded + 78bcf2e commit 4b4b5d2
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 6 deletions.
23 changes: 17 additions & 6 deletions ExternalProjectDependency.cmake
Expand Up @@ -1017,13 +1017,16 @@ endfunction()
#
# ExternalProject_SetIfNotDefined(<var> <defaultvalue> [OBFUSCATE] [QUIET])
#
# The default value is set with:
# (1) if set, the value environment variable <var>.
# (2) if set, the value of local variable variable <var>.
# (3) if none of the above, the value passed as a parameter.
# If *NOT* already defined, the variable <var> is set with:
# (1) the value of the environment variable <var>, if defined.
# (2) the value of the local variable variable <var>, if defined.
# (3) if none of the above is defined, the <defaultvalue> passed as a parameter.
#
# Setting the optional parameter 'OBFUSCATE' will display 'OBFUSCATED' instead of the real value.
# Setting the optional parameter 'QUIET' will not display any message.
# Passing the optional parameter 'OBFUSCATE' will display 'OBFUSCATED' instead of the real value.
# Passing the optional parameter 'QUIET' will not display any message.
#
# For convenience, the value of the cache variable named <var> will
# be displayed if it was set and if QUIET has not been passed.
macro(ExternalProject_SetIfNotDefined var defaultvalue)
set(_obfuscate FALSE)
set(_quiet FALSE)
Expand Down Expand Up @@ -1055,6 +1058,14 @@ macro(ExternalProject_SetIfNotDefined var defaultvalue)
endif()
set(${var} "${defaultvalue}")
endif()
get_property(_is_set CACHE ${var} PROPERTY VALUE SET)
if(_is_set AND NOT _quiet)
set(_value "${${var}}")
if(_obfuscate)
set(_value "OBFUSCATED")
endif()
message(STATUS "Cache variable '${var}' set to '${_value}'")
endif()
endmacro()

#.rst:
Expand Down
8 changes: 8 additions & 0 deletions Tests/ArtichokeTestUtility.cmake
Expand Up @@ -19,6 +19,14 @@ function(check_variable var_name expected_value)
endif()
endfunction()

function(check_env_variable var_name expected_value)
if(NOT "x$ENV{${var_name}}" STREQUAL "x${expected_value}")
message(FATAL_ERROR "CMake variable [$ENV{${var_name}}] is incorrectly set !\n"
"current:$ENV{${var_name}}\n"
"expected:${expected_value}")
endif()
endfunction()

function(configure_external_projects_for_test name)
set(depends "${expected_${name}_DEPENDS}")
set(indent "${ARGV1}")
Expand Down
1 change: 1 addition & 0 deletions Tests/CMakeLists.txt
Expand Up @@ -85,6 +85,7 @@ set(tests_source_dir ${CMAKE_SOURCE_DIR})
add_project_test(AddDependencies SuperBuild)
add_project_test(AlwaysConfigure SuperBuild)
add_project_test(ExternalProjectFileLookup SuperBuild)
add_project_test(ExternalProject_SetIfNotDefined-Cache SuperBuild)
add_project_test(MarkAsSuperBuild SuperBuild)
add_project_test(MarkAsSuperBuild-EP_ARGS_VAR SuperBuild)
add_project_test(MarkAsSuperBuild-WithNotFoundVariable SuperBuild)
Expand Down
41 changes: 41 additions & 0 deletions Tests/ExternalProject_SetIfNotDefined-Cache-Test/CMakeLists.txt
@@ -0,0 +1,41 @@
cmake_minimum_required(VERSION 2.8.7)

if(POLICY CMP0054)
cmake_policy(SET CMP0054 OLD)
endif()

project(ExternalProject_SetIfNotDefined-Cache-Test NONE)

set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../" ${CMAKE_MODULE_PATH})
include(ExternalProjectDependency)

include(${CMAKE_CURRENT_SOURCE_DIR}/../ArtichokeTestUtility.cmake)

# prefix
set(p "ExternalProject_SetIfNotDefined-Cache-Test")

# Check that cache variable is not overridden
set(${p}_BLUE "cache-blue" CACHE STRING "")
check_variable(${p}_BLUE "cache-blue")
ExternalProject_SetIfNotDefined(${p}_BLUE "default-blue")
check_variable(${p}_BLUE "cache-blue")

# Check that env variable is not overriden
set(ENV{${p}_RED} "env-red")
check_env_variable(${p}_RED "env-red")
check_variable_not_defined(${p}_RED)
ExternalProject_SetIfNotDefined(${p}_RED "default-red")
check_variable(${p}_RED "env-red")

# Check that local variable is not overriden
set(${p}_GREEN "local-green")
check_variable(${p}_GREEN "local-green")
ExternalProject_SetIfNotDefined(${p}_GREEN "default-green")
check_variable(${p}_GREEN "local-green")

# Check that local variable still has precedence over cache variable
set(${p}_CYAN "cache-cyan" CACHE STRING "")
check_variable(${p}_CYAN "cache-cyan")
set(${p}_CYAN "local-cyan")
ExternalProject_SetIfNotDefined(${p}_CYAN "default-cyan")
check_variable(${p}_CYAN "local-cyan")

0 comments on commit 4b4b5d2

Please sign in to comment.