Skip to content

feat: add function for parsing version from version.txt#4

Merged
Melky-Phoe merged 4 commits intomasterfrom
BAF-1402/parse-version-file
Apr 10, 2026
Merged

feat: add function for parsing version from version.txt#4
Melky-Phoe merged 4 commits intomasterfrom
BAF-1402/parse-version-file

Conversation

@Melky-Phoe
Copy link
Copy Markdown
Contributor

@Melky-Phoe Melky-Phoe commented Apr 9, 2026

Summary by CodeRabbit

  • New Features

    • Added capability to read a version file (defaults to the source directory), validate its presence and format, and return the validated version — now fails configuration on missing/empty values.
  • Tests

    • Added test coverage that parses the version file, verifies it matches the expected value, and aborts configuration on mismatch.
  • Bug Fixes

    • Fixed a documentation comment typo.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 9, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: ef753eaf-a274-4be0-8f5d-fde9893b4109

📥 Commits

Reviewing files that changed from the base of the PR and between 576932a and 7a6f9c1.

📒 Files selected for processing (1)
  • system_modules/CMUTIL_VERSION.cmake
🚧 Files skipped from review as they are similar to previous changes (1)
  • system_modules/CMUTIL_VERSION.cmake

Walkthrough

Adds a new public CMake function CMUTIL_VERSION_GET_FROM_VERSION_FILE(output_var ...) that reads a version.txt (default ${CMAKE_CURRENT_SOURCE_DIR}/version.txt), requires a non-empty inject_version, validates it with CMUTIL_VERSION_CHECK, and returns the validated version via PARENT_SCOPE. A test verifies the parsed value.

Changes

Cohort / File(s) Summary
Version reader implementation
system_modules/CMUTIL_VERSION.cmake
Added FUNCTION(CMUTIL_VERSION_GET_FROM_VERSION_FILE output_var ...) which locates a version.txt (default ${CMAKE_CURRENT_SOURCE_DIR}/version.txt), calls CMUTIL_PROPERTY_FILE_READ to extract inject_version, treats NOT DEFINED or empty as fatal, validates via CMUTIL_VERSION_CHECK (quoted), and sets the result in PARENT_SCOPE. Also tightened CMUTIL_VERSION_VALIDATE_VERSION_FILE_FOR checks and fixed comment text/path.
Test coverage
test/VERSION/CMakeLists.txt
Added test block invoking CMUTIL_VERSION_GET_FROM_VERSION_FILE(parsed_version) and asserting parsed_version equals "1.58.3", emitting MESSAGE(FATAL_ERROR ...) on mismatch or a success message on match.

Sequence Diagram(s)

sequenceDiagram
    participant Config as CMake Configure
    participant Func as CMUTIL_VERSION_GET_FROM_VERSION_FILE
    participant Reader as CMUTIL_PROPERTY_FILE_READ
    participant Checker as CMUTIL_VERSION_CHECK

    Config->>Func: call(output_var [, path])
    Func->>Reader: read properties from version file (default or given path)
    Reader-->>Func: returns properties (including inject_version)
    Func->>Func: if inject_version == NOT DEFINED or empty -> FATAL_ERROR
    Func->>Checker: CMUTIL_VERSION_CHECK("inject_version")
    Checker-->>Func: validation result
    Func-->>Config: set validated version via PARENT_SCOPE or abort
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 I hopped through lines of version.txt bright,

Found inject_version by lantern light.
I nudged and checked each digit and dot,
Sent the true value back to the plot.
Tests cheer — the rabbit's work is spot-on!

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: a new function for parsing version information from a version.txt file, which aligns with the additions in CMUTIL_VERSION.cmake.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch BAF-1402/parse-version-file

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

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

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

🧹 Nitpick comments (2)
test/VERSION/CMakeLists.txt (1)

35-41: Add a dedicated fail test for invalid/missing version key for this new function.

The new test only covers success. Please add a fail-case directory that calls CMUTIL_VERSION_GET_FROM_VERSION_FILE(...) with invalid input and register it via TEST_INVALID_CMAKE_RUN(...) in the parent test file.

If you want, I can draft the exact fail test layout and expected error pattern.
Based on learnings: In this project, failure tests are structured using TEST_INVALID_CMAKE_RUN in parent test files, while fail-case files directly trigger FATAL_ERROR.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@test/VERSION/CMakeLists.txt` around lines 35 - 41, Add a failing test case
for CMUTIL_VERSION_GET_FROM_VERSION_FILE by creating a fail-case directory that
contains a CMakeLists.txt that calls
CMUTIL_VERSION_GET_FROM_VERSION_FILE(parsed_version) but with an invalid or
missing "version" key so it triggers MESSAGE(FATAL_ERROR ...) (use the same
pattern as existing failure tests); then register that directory in the parent
test file using TEST_INVALID_CMAKE_RUN(<fail-case-dir> ...) and supply the
expected error pattern matching the FATAL_ERROR message from
CMUTIL_VERSION_GET_FROM_VERSION_FILE so the CI recognizes the negative test.
system_modules/CMUTIL_VERSION.cmake (1)

120-123: Harden optional argument parsing for clearer API behavior.

Line 120 currently consumes all ARGN as one value. If multiple optional args are passed, CMake builds a list and error handling becomes unclear. Prefer explicit arity checks and ARGV1.

Suggested refactor
 FUNCTION(CMUTIL_VERSION_GET_FROM_VERSION_FILE output_var)
-	SET(version_file ${ARGN})
-	IF("${version_file}" STREQUAL "")
-		SET(version_file "${CMAKE_CURRENT_SOURCE_DIR}/version.txt")
-	ENDIF()
+	IF(ARGC GREATER 2)
+		MESSAGE(FATAL_ERROR "CMUTIL_VERSION_GET_FROM_VERSION_FILE expects: <output_var> [version_file]")
+	ENDIF()
+	IF(ARGC EQUAL 2)
+		SET(version_file "${ARGV1}")
+	ELSE()
+		SET(version_file "${CMAKE_CURRENT_SOURCE_DIR}/version.txt")
+	ENDIF()
@@
-	SET(${output_var} ${inject_version} PARENT_SCOPE)
+	SET(${output_var} "${inject_version}" PARENT_SCOPE)
 ENDFUNCTION()
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@system_modules/CMUTIL_VERSION.cmake` around lines 120 - 123, The code
currently uses SET(version_file ${ARGN}) which conflates multiple optional
arguments into a CMake list; change to explicit arity checks (use ARGC or
IF(DEFINED ARGV1)) and read only ARGV1 as the optional path: if no optional arg
provided, set version_file to the default
"${CMAKE_CURRENT_SOURCE_DIR}/version.txt"; if more than one optional arg is
given, fail with a clear message. Update the logic around SET(version_file ...),
the IF("${version_file}" STREQUAL "") check, and replace ARGN usage with
ARGV1/ARGC validation so the API is deterministic and reports misuse.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@system_modules/CMUTIL_VERSION.cmake`:
- Around line 111-123: The comment above CMUTIL_VERSION_GET_FROM_VERSION_FILE
states the default path is ${CMAKE_CURRENT_LIST_DIR}/version.txt but the
function sets version_file to ${CMAKE_CURRENT_SOURCE_DIR}/version.txt; update
one to match the other: either change the comment to say
${CMAKE_CURRENT_SOURCE_DIR}/version.txt or change the implementation to set
version_file to ${CMAKE_CURRENT_LIST_DIR}/version.txt (modify the assignment of
version_file in CMUTIL_VERSION_GET_FROM_VERSION_FILE accordingly) so the
documentation and code are consistent.

---

Nitpick comments:
In `@system_modules/CMUTIL_VERSION.cmake`:
- Around line 120-123: The code currently uses SET(version_file ${ARGN}) which
conflates multiple optional arguments into a CMake list; change to explicit
arity checks (use ARGC or IF(DEFINED ARGV1)) and read only ARGV1 as the optional
path: if no optional arg provided, set version_file to the default
"${CMAKE_CURRENT_SOURCE_DIR}/version.txt"; if more than one optional arg is
given, fail with a clear message. Update the logic around SET(version_file ...),
the IF("${version_file}" STREQUAL "") check, and replace ARGN usage with
ARGV1/ARGC validation so the API is deterministic and reports misuse.

In `@test/VERSION/CMakeLists.txt`:
- Around line 35-41: Add a failing test case for
CMUTIL_VERSION_GET_FROM_VERSION_FILE by creating a fail-case directory that
contains a CMakeLists.txt that calls
CMUTIL_VERSION_GET_FROM_VERSION_FILE(parsed_version) but with an invalid or
missing "version" key so it triggers MESSAGE(FATAL_ERROR ...) (use the same
pattern as existing failure tests); then register that directory in the parent
test file using TEST_INVALID_CMAKE_RUN(<fail-case-dir> ...) and supply the
expected error pattern matching the FATAL_ERROR message from
CMUTIL_VERSION_GET_FROM_VERSION_FILE so the CI recognizes the negative test.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: afe62d89-09b0-48c1-998a-ae6a6fa16dc6

📥 Commits

Reviewing files that changed from the base of the PR and between 9bd74ee and 0b0993d.

📒 Files selected for processing (2)
  • system_modules/CMUTIL_VERSION.cmake
  • test/VERSION/CMakeLists.txt

Copy link
Copy Markdown

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

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@system_modules/CMUTIL_VERSION.cmake`:
- Around line 125-131: Clear the local inject_version before calling
CMUTIL_PROPERTY_FILE_READ so a stale parent-scope value cannot be picked up;
specifically, add a local set(inject_version "") (or otherwise unset
inject_version in the current scope) immediately before the
CMUTIL_PROPERTY_FILE_READ call in the CMUTIL_VERSION_GET_FROM_VERSION_FILE
implementation and apply the identical change to
CMUTIL_VERSION_VALIDATE_VERSION_FILE_FOR so the IF(NOT inject_version) fatal
check always validates the freshly-read value rather than a leaked parent value.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: b25b31e0-203b-4e11-9a2a-0c95b05912db

📥 Commits

Reviewing files that changed from the base of the PR and between 0b0993d and 66ea4a9.

📒 Files selected for processing (1)
  • system_modules/CMUTIL_VERSION.cmake

Copy link
Copy Markdown

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

♻️ Duplicate comments (1)
system_modules/CMUTIL_VERSION.cmake (1)

125-127: ⚠️ Potential issue | 🟠 Major

UNSET(inject_version) still allows parent-scope leakage.

At Line 125 and Line 155, UNSET(inject_version) removes only the local binding; if a parent inject_version exists, CMake can still resolve it. If the file lacks version=..., Line 127 / Line 157 may validate stale data instead of failing.

Use an explicit local shadow value before read.

🔧 Proposed fix
-	UNSET(inject_version)
+	SET(inject_version "")
 	CMUTIL_PROPERTY_FILE_READ("${version_file}" inject)
 	IF(NOT DEFINED inject_version OR "${inject_version}" STREQUAL "")
 		MESSAGE(FATAL_ERROR "${version_file} is not valid")
 	ENDIF()
-	UNSET(inject_version)
+	SET(inject_version "")
 	CMUTIL_PROPERTY_FILE_READ("${version_file}" inject)
 	IF(NOT DEFINED inject_version OR "${inject_version}" STREQUAL "")
 		MESSAGE(FATAL_ERROR "${version_file} is not valid")
 	ENDIF()
In CMake function scope, if a parent variable exists, does `unset(var)` in the function make the parent value visible again? What is the recommended pattern to prevent stale parent-scope values when expecting `var` to be populated via a child function using `PARENT_SCOPE`?

Also applies to: 155-157

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@system_modules/CMUTIL_VERSION.cmake` around lines 125 - 127, The current
UNSET(inject_version) can let a parent-scope inject_version leak back into this
scope; replace the UNSET with an explicit local shadow assignment (e.g.,
set(inject_version "")) before calling
CMUTIL_PROPERTY_FILE_READ("${version_file}" inject) so the function starts with
a known-empty local inject_version and the subsequent IF(NOT DEFINED
inject_version OR "${inject_version}" STREQUAL "") correctly detects a missing
value; apply the same change for the second occurrence around lines 155–157
referencing inject_version.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Duplicate comments:
In `@system_modules/CMUTIL_VERSION.cmake`:
- Around line 125-127: The current UNSET(inject_version) can let a parent-scope
inject_version leak back into this scope; replace the UNSET with an explicit
local shadow assignment (e.g., set(inject_version "")) before calling
CMUTIL_PROPERTY_FILE_READ("${version_file}" inject) so the function starts with
a known-empty local inject_version and the subsequent IF(NOT DEFINED
inject_version OR "${inject_version}" STREQUAL "") correctly detects a missing
value; apply the same change for the second occurrence around lines 155–157
referencing inject_version.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 65b7c352-9c87-41c8-b3f4-e4635739f1cc

📥 Commits

Reviewing files that changed from the base of the PR and between 66ea4a9 and 576932a.

📒 Files selected for processing (1)
  • system_modules/CMUTIL_VERSION.cmake

@Melky-Phoe Melky-Phoe merged commit 67f377d into master Apr 10, 2026
45 of 61 checks passed
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