Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion CMCONF.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,9 @@ ENDFUNCTION()
# package name constructed as CMCONF_PACKAGE_NAME_PREFIX + SYSTEM_NAME.
# FIND_PACKAGE is called every time CMCONF_GET is called.
#
# The variable can be specified by environment variable or by -D cmdline option as CMake cache variable.
# The variable name is constructed as SYSTEM_NAME_VARIABLE_NAME (both uppercase).
#
# Restrictions:
# - System name must be set before calling this function
# - Cannot be called after CMCONF_SET has been used in the same session.
Expand Down Expand Up @@ -273,7 +276,11 @@ FUNCTION(CMCONF_GET var_name)
_CMCONF_MESSAGE(FATAL_ERROR "Variable '${var_name}' is not defined in configuration for system '${CMCONF_SYSTEM_NAME}'.")
ENDIF()
ENDIF()
SET(${var_name} "${${actual_var_name}}" PARENT_SCOPE)
IF(DEFINED ENV{${actual_var_name}})
SET(${var_name} "$ENV{${actual_var_name}}" PARENT_SCOPE)
ELSE()
SET(${var_name} "${${actual_var_name}}" PARENT_SCOPE)
ENDIF()
ENDFUNCTION()


Expand Down
3 changes: 3 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ TEST_RUN("${CMAKE_CURRENT_LIST_DIR}/test_cases/pass/basic_get")
TEST_RUN("${CMAKE_CURRENT_LIST_DIR}/test_cases/pass/cache_variable_behavior")
TEST_RUN("${CMAKE_CURRENT_LIST_DIR}/test_cases/pass/case_insensitive")
TEST_RUN("${CMAKE_CURRENT_LIST_DIR}/test_cases/pass/different_system_names")
TEST_RUN("${CMAKE_CURRENT_LIST_DIR}/test_cases/pass/env_variable_fallback")
TEST_RUN("${CMAKE_CURRENT_LIST_DIR}/test_cases/pass/env_variable_override")
TEST_RUN("${CMAKE_CURRENT_LIST_DIR}/test_cases/pass/multiple_variables")
TEST_RUN("${CMAKE_CURRENT_LIST_DIR}/test_cases/pass/uninstall_verification")

TEST_RUN("${CMAKE_CURRENT_LIST_DIR}/test_cases/fail/config_not_installed")
TEST_RUN("${CMAKE_CURRENT_LIST_DIR}/test_cases/fail/config_already_installed")
TEST_RUN("${CMAKE_CURRENT_LIST_DIR}/test_cases/fail/env_variable_config_required")
TEST_RUN("${CMAKE_CURRENT_LIST_DIR}/test_cases/fail/get_after_set")
TEST_RUN("${CMAKE_CURRENT_LIST_DIR}/test_cases/fail/install_from_cmake_project")
TEST_RUN("${CMAKE_CURRENT_LIST_DIR}/test_cases/fail/install_project_execution_failure")
Expand Down
29 changes: 29 additions & 0 deletions test/test_cases/fail/env_variable_config_required/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
## Main
#
# Test that ENV variables cannot override non-defined config variables
#
# This test validates that even when an environment variable is set,
# CMCONF_GET still requires the variable to be defined in the configuration.
# ENV variables cannot be used to define new variables that don't exist in config.
#

IF(NOT DEFINED CMAKE_SCRIPT_MODE_FILE)
CMAKE_MINIMUM_REQUIRED(VERSION 3.22)
PROJECT(CMCONF_ENV_VARIABLE_CONFIG_REQUIRED_TEST)
ENDIF()

FIND_PACKAGE(CMLIB REQUIRED)

INCLUDE("${CMAKE_CURRENT_LIST_DIR}/../../../TEST.cmake")
INCLUDE("${CMAKE_CURRENT_LIST_DIR}/../../../cache_var.cmake")
INCLUDE("${CMAKE_CURRENT_LIST_DIR}/../../../../CMCONF.cmake")
INCLUDE("${CMAKE_CURRENT_LIST_DIR}/../../../test_cmconf_helpers.cmake")
INCLUDE("${CMAKE_CURRENT_LIST_DIR}/../../../config_templates/config_template.cmake")

TEST_CMCONF_CHECK_AND_INSTALL_CONFIG("${CMCONF_TEST_CONFIG_FILE}" "TEST")

TEST_RUN_AND_CHECK_OUTPUT("test_config"
FATAL_ERROR_MESSAGE "CMCONF\\\\[TEST\\\\] - Variable 'UNDEFINED_VARIABLE' is not defined in.*configuration for system 'TEST'"
)

TEST_CMCONF_UNINSTALL_CONFIG("${CMCONF_TEST_CONFIG_FILE}" "TEST")
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## Test configuration that should fail when trying to get undefined variable
#
# This attempts to use CMCONF_GET with an ENV variable set for a variable
# that is not defined in the configuration, which should generate a fatal error.
#

IF(NOT DEFINED CMAKE_SCRIPT_MODE_FILE)
CMAKE_MINIMUM_REQUIRED(VERSION 3.22)
PROJECT(CMCONF_ENV_VARIABLE_CONFIG_REQUIRED_TEST_CONFIG)
ENDIF()

FIND_PACKAGE(CMLIB REQUIRED)

INCLUDE("${CMAKE_CURRENT_LIST_DIR}/../../../../../CMCONF.cmake")

SET(ENV{TEST_UNDEFINED_VARIABLE} "env_value")

CMCONF_INIT_SYSTEM("TEST")
CMCONF_GET(UNDEFINED_VARIABLE)
35 changes: 35 additions & 0 deletions test/test_cases/pass/env_variable_fallback/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
## Main
#
# Test that config values are used when ENV variables are not set
#
# This test validates that when no environment variable is defined,
# CMCONF_GET falls back to the configuration value, maintaining
# backward compatibility.
#

IF(NOT DEFINED CMAKE_SCRIPT_MODE_FILE)
CMAKE_MINIMUM_REQUIRED(VERSION 3.22)
PROJECT(CMCONF_ENV_VARIABLE_FALLBACK_TEST)
ENDIF()

FIND_PACKAGE(CMLIB REQUIRED)

INCLUDE("${CMAKE_CURRENT_LIST_DIR}/../../../TEST.cmake")
INCLUDE("${CMAKE_CURRENT_LIST_DIR}/../../../cache_var.cmake")
INCLUDE("${CMAKE_CURRENT_LIST_DIR}/../../../../CMCONF.cmake")
INCLUDE("${CMAKE_CURRENT_LIST_DIR}/../../../test_cmconf_helpers.cmake")
INCLUDE("${CMAKE_CURRENT_LIST_DIR}/../../../config_templates/config_template.cmake")

TEST_CMCONF_CHECK_AND_INSTALL_CONFIG("${CMCONF_TEST_CONFIG_FILE}" "TEST")

CMCONF_INIT_SYSTEM(TEST)

# Ensure no ENV variable is set for TEST_VARIABLE_A
UNSET(ENV{TEST_VARIABLE_A})

CMCONF_GET(VARIABLE_A)

TEST_VAR_DEFINED(VARIABLE_A)
TEST_VAR_EQUALS_LITERAL(VARIABLE_A "test_value_a")

TEST_CMCONF_UNINSTALL_CONFIG("${CMCONF_TEST_CONFIG_FILE}" "TEST")
34 changes: 34 additions & 0 deletions test/test_cases/pass/env_variable_override/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
## Main
#
# Test that ENV variables override config values
#
# This test validates that when both a configuration variable and an environment
# variable are defined, the environment variable takes precedence.
#

IF(NOT DEFINED CMAKE_SCRIPT_MODE_FILE)
CMAKE_MINIMUM_REQUIRED(VERSION 3.22)
PROJECT(CMCONF_ENV_VARIABLE_OVERRIDE_TEST)
ENDIF()

FIND_PACKAGE(CMLIB REQUIRED)

INCLUDE("${CMAKE_CURRENT_LIST_DIR}/../../../TEST.cmake")
INCLUDE("${CMAKE_CURRENT_LIST_DIR}/../../../cache_var.cmake")
INCLUDE("${CMAKE_CURRENT_LIST_DIR}/../../../../CMCONF.cmake")
INCLUDE("${CMAKE_CURRENT_LIST_DIR}/../../../test_cmconf_helpers.cmake")
INCLUDE("${CMAKE_CURRENT_LIST_DIR}/../../../config_templates/config_template.cmake")


TEST_CMCONF_CHECK_AND_INSTALL_CONFIG("${CMCONF_TEST_CONFIG_FILE}" "TEST")

CMCONF_INIT_SYSTEM(TEST)

SET(ENV{TEST_VARIABLE_A} "env_override_value")
CMCONF_GET(VARIABLE_A)

TEST_VAR_DEFINED(VARIABLE_A)
TEST_VAR_EQUALS_LITERAL(VARIABLE_A "env_override_value")

UNSET(ENV{TEST_VARIABLE_A})
TEST_CMCONF_UNINSTALL_CONFIG("${CMCONF_TEST_CONFIG_FILE}" "TEST")
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=1.0.0
version=1.1.0
Loading