Skip to content

CMCONF_GET - override by ENV variable enabled#3

Merged
koudis merged 2 commits intomainfrom
env_enabled
Aug 9, 2025
Merged

CMCONF_GET - override by ENV variable enabled#3
koudis merged 2 commits intomainfrom
env_enabled

Conversation

@koudis
Copy link
Member

@koudis koudis commented Aug 9, 2025

Summary by CodeRabbit

  • New Features

    • Configuration variables can now be overridden by environment variables, enhancing flexibility in setup.
  • Tests

    • Added new test cases to verify correct handling of environment variable overrides, fallback behavior, and enforcement of variable definition requirements.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Aug 9, 2025

Caution

Review failed

The pull request is closed.

Walkthrough

The changes introduce support for overriding configuration variables with environment variables in the CMCONF_GET function. Accompanying this, several new test cases are added to verify correct behavior when environment variables are present, absent, or used for undefined variables. The test suite is updated to include these scenarios. Additionally, the version number is incremented from 1.0.0 to 1.1.0.

Changes

Cohort / File(s) Change Summary
Core Configuration Logic
CMCONF.cmake
Modified CMCONF_GET to prioritize environment variables over CMake cache variables for configuration values. Updated internal documentation to reflect this behavior.
Test Suite Registration
test/CMakeLists.txt
Added three new test cases to the test suite: two passing (env_variable_fallback, env_variable_override) and one failing (env_variable_config_required) to cover environment variable handling.
Failing Test: Undefined Variable via Env
test/test_cases/fail/env_variable_config_required/CMakeLists.txt,
test/test_cases/fail/env_variable_config_required/test_config/CMakeLists.txt
Added a test to verify that environment variables cannot define configuration variables that are not already declared, ensuring strict enforcement in CMCONF_GET.
Passing Test: Fallback to Config Value
test/test_cases/pass/env_variable_fallback/CMakeLists.txt
Added a test to confirm that when an environment variable is absent, the configuration value is used as the fallback.
Passing Test: Env Variable Override
test/test_cases/pass/env_variable_override/CMakeLists.txt
Added a test to confirm that an environment variable overrides the configuration value when both are present.
Version Update
version.txt
Updated version number from 1.0.0 to 1.1.0.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant CMake
    participant CMCONF_GET

    User->>CMake: Configure project
    CMake->>CMCONF_GET: Request VARIABLE_X
    alt Env variable SYSTEM_VARIABLE_X is set
        CMCONF_GET->>CMake: Return env variable value
    else Env variable not set
        CMCONF_GET->>CMake: Return config/cache variable value
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~15 minutes

Poem

In the warren of configs, a breeze did blow,
Now env vars can lead, where once they’d not go.
With tests for the fallback, and overrides too,
The rabbits ensured the logic is true.
If undefined, it fails—no sneaky surprise!
🐇✨ Configured with care, in clever disguise.

Note

🔌 MCP (Model Context Protocol) integration is now available in Early Access!

Pro users can now connect to remote MCP servers under the Integrations page to get reviews and chat conversations that understand additional development context.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c8fff08 and 56321ff.

📒 Files selected for processing (1)
  • version.txt (1 hunks)
✨ Finishing Touches
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch env_enabled

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (5)
CMCONF.cmake (2)

238-240: Document precedence and provide a quick example for ENV vs -D

Current docs state both sources but not which wins. Code prefers ENV when set. Clarify to avoid ambiguity.

-# 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).
+# The variable value can come either from an environment variable or from a -D command-line cache entry.
+# Precedence: if both are provided, the environment variable wins.
+# The variable name is constructed as SYSTEM_NAME_VARIABLE_NAME (both uppercase).
+# Example: for system "TEST" and variable "VARIABLE_A", use ENV TEST_VARIABLE_A or -DTEST_VARIABLE_A=...

279-283: Optional: ignore empty ENV values and (optionally) preserve semicolons

If an ENV var is present but empty, you may want to fall back to the configured value. Also, semicolons in ENV values become list separators in CMake; escape them if you want a plain string.

-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()
+IF(DEFINED ENV{${actual_var_name}} AND NOT "$ENV{${actual_var_name}}" STREQUAL "")
+    # Uncomment the next two lines if you want to preserve semicolons as literal characters:
+    # set(_cmconf_env_value "$ENV{${actual_var_name}}")
+    # string(REPLACE ";" "\\;" _cmconf_env_value "${_cmconf_env_value}")
+    # SET(${var_name} "${_cmconf_env_value}" PARENT_SCOPE)
+    SET(${var_name} "$ENV{${actual_var_name}}" PARENT_SCOPE)
+ELSE()
+    SET(${var_name} "${${actual_var_name}}" PARENT_SCOPE)
+ENDIF()
test/test_cases/pass/env_variable_override/CMakeLists.txt (3)

27-31: Core behavior validated; consider hardening against false positives

The assertions correctly verify that ENV overrides config. To avoid accidental pass if the config value already equals "env_override_value", consider also asserting the fallback value in a separate test/run (after env is unset and state is reinitialized), or explicitly checking that the template’s default differs.

I can add a companion test that validates fallback after unsetting the ENV and reinitializing state.


27-28: Document the expected ENV naming convention

Add a brief comment stating the expected ENV key pattern (e.g., _) so future readers understand why TEST_VARIABLE_A maps to VARIABLE_A.

-SET(ENV{TEST_VARIABLE_A} "env_override_value")
+# Convention: ENV override key is <SYSTEM>_<VAR>. Here: TEST_VARIABLE_A -> VARIABLE_A.
+SET(ENV{TEST_VARIABLE_A} "env_override_value")

25-34: Best-effort cleanup even on assertion failure (project mode only)

If a test assertion aborts, lines 33-34 won’t run. ENV is process-local, but the installed config may linger. Optionally schedule deferred cleanup when available (project mode), while keeping script-mode compatibility.

 CMCONF_INIT_SYSTEM(TEST)
 
+# In project mode, ensure cleanup even if a later assertion fails.
+IF(NOT DEFINED CMAKE_SCRIPT_MODE_FILE AND COMMAND cmake_language)
+  cmake_language(DEFER CALL UNSET "ENV{TEST_VARIABLE_A}")
+  cmake_language(DEFER CALL TEST_CMCONF_UNINSTALL_CONFIG "${CMCONF_TEST_CONFIG_FILE}" "TEST")
+ENDIF()
+
 SET(ENV{TEST_VARIABLE_A} "env_override_value")
 CMCONF_GET(VARIABLE_A)
@@
 UNSET(ENV{TEST_VARIABLE_A})
 TEST_CMCONF_UNINSTALL_CONFIG("${CMCONF_TEST_CONFIG_FILE}" "TEST")

Note: This keeps script-mode behavior unchanged and uses DEFER only where supported.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 196573f and c8fff08.

📒 Files selected for processing (6)
  • CMCONF.cmake (2 hunks)
  • test/CMakeLists.txt (1 hunks)
  • test/test_cases/fail/env_variable_config_required/CMakeLists.txt (1 hunks)
  • test/test_cases/fail/env_variable_config_required/test_config/CMakeLists.txt (1 hunks)
  • test/test_cases/pass/env_variable_fallback/CMakeLists.txt (1 hunks)
  • test/test_cases/pass/env_variable_override/CMakeLists.txt (1 hunks)
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-08-02T17:36:23.478Z
Learnt from: koudis
PR: cmakelib/cmakelib-component-cmconf#1
File: test/test_cases/fail/variable_not_defined/CMakeLists.txt:17-23
Timestamp: 2025-08-02T17:36:23.478Z
Learning: The cmake_language(DEFER) construct is not available in CMake script mode, only in project mode, making it unsuitable for tests that need to run in both modes.

Applied to files:

  • test/CMakeLists.txt
  • test/test_cases/fail/env_variable_config_required/test_config/CMakeLists.txt
  • test/test_cases/fail/env_variable_config_required/CMakeLists.txt
  • test/test_cases/pass/env_variable_fallback/CMakeLists.txt
  • test/test_cases/pass/env_variable_override/CMakeLists.txt
🔇 Additional comments (8)
CMCONF.cmake (1)

279-283: ENV override is correctly implemented and scoped

Logic is sound: ensure var exists in config, then prefer ENV if present, otherwise use cache. Matches the PR objective.

test/CMakeLists.txt (2)

21-22: Good coverage: add pass tests for ENV fallback/override

Nice additions. They exercise both absence and presence of ENV to validate precedence and backward compatibility.


28-28: Failing test inclusion looks right

The negative test ensures undefined variables cannot be sourced solely from ENV. Aligned with intended guardrails.

test/test_cases/fail/env_variable_config_required/CMakeLists.txt (1)

23-27: Failure expectation and regex look correct

Installs config, runs the sub-test, and asserts the fatal error about undefined variable in config. Regex targets the exact CMCONF message format.

test/test_cases/pass/env_variable_fallback/CMakeLists.txt (1)

27-34: Solid fallback test

Explicitly unsetting ENV then asserting the configured value verifies backward compatibility of CMCONF_GET.

test/test_cases/fail/env_variable_config_required/test_config/CMakeLists.txt (1)

16-20: Correctly simulates undefined var with ENV present

Setting TEST_UNDEFINED_VARIABLE, initializing system, then calling CMCONF_GET triggers the intended fatal path. Clean and minimal.

test/test_cases/pass/env_variable_override/CMakeLists.txt (2)

9-12: Good handling of script vs project mode; compliant with prior lessons

Guarding PROJECT/MINIMUM_REQUIRED behind CMAKE_SCRIPT_MODE_FILE avoids features unavailable in script mode (e.g., DEFER). This aligns with the retrieved learning.


1-35: Test case is already wired into the suite
The env_variable_override test is invoked in test/CMakeLists.txt:

  • test/CMakeLists.txt:22
    TEST_RUN("${CMAKE_CURRENT_LIST_DIR}/test_cases/pass/env_variable_override")

No additional registration or discovery macros are required.

@koudis koudis merged commit 81cb8ef into main Aug 9, 2025
13 of 14 checks passed
@koudis koudis deleted the env_enabled branch August 9, 2025 18:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments