fix: correct metadata/version check in version command#2111
Conversation
Signed-off-by: Rishi Jat <rishijat098@gmail.com>
|
/cc @degenaro |
There was a problem hiding this comment.
Pull request overview
Fixes a bug in the version command where a metadata/version check could raise AttributeError and fail to properly validate missing version values.
Changes:
- Corrected the boolean condition in
VersionCmd._get_versionto safely handlemetadata is None. - Added a unit test to ensure
TrestleErroris raised whenmetadataisNone.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| trestle/core/commands/version.py | Fixes the metadata/version guard to avoid AttributeError and improve validation behavior. |
| tests/trestle/core/commands/version_test.py | Adds regression coverage for the metadata is None case by asserting TrestleError. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if not oscal_object.metadata or not oscal_object.metadata.version: # type: ignore | ||
| raise TrestleError(f'Unable to determine the version. Metadata version is missing in model: {obj_path}.') |
There was a problem hiding this comment.
The updated condition has two distinct failure modes (metadata is None vs metadata.version missing/falsey). The new test covers metadata is None, but there’s no test covering the case where metadata exists and metadata.version is missing/None/empty. Add a unit test that sets mock_oscal_object.metadata to an object with version = None (and optionally '') and assert TrestleError is raised to fully cover the corrected logic.
butler54
left a comment
There was a problem hiding this comment.
I think we need to do some more refactoring here:
TL;DR - i'm not convinced you can hit the code properly anyway. Why?
The top level OSCAL models have metadata as mandatory
e.g.
compliance-trestle/trestle/oscal/poam.py
Line 111 in 87fe40b
so Metadata, with pydandic enforcement, cannot be none.
Issue
While reviewing the code, I noticed the boolean condition in version.py:
This can raise an AttributeError when metadata is None and does not correctly validate missing version values.
Changes
Prevents a potential crash and improves validation logic.