-
Notifications
You must be signed in to change notification settings - Fork 13
Fix bugs in possible policy-set path detection in cfbs-analyze #267
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jakub-nt
merged 3 commits into
cfengine:master
from
jakub-nt:analyze-possible-policyset-paths-fix
Aug 12, 2025
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| import os | ||
|
|
||
| from cfbs.analyze import checksums_files, mpf_normalized_path, possible_policyset_paths | ||
|
|
||
|
|
||
| # Executing the functions in particular working directories is necessary for testing relative paths. | ||
| class cwd: | ||
| def __init__(self, new_wd): | ||
| self.new_wd = os.path.expanduser(new_wd) | ||
|
|
||
| def __enter__(self): | ||
| self.old_wd = os.getcwd() | ||
| os.chdir(self.new_wd) | ||
|
|
||
| def __exit__(self, exc_type, exc_value, traceback): | ||
| os.chdir(self.old_wd) | ||
|
|
||
|
|
||
| def ppp_scaffolded(path, masterfiles_dir="masterfiles"): | ||
| """Testable variant of `cfbs.analyze.possible_policyset_paths` including prerequisite code.""" | ||
| is_parentpath = os.path.isdir(os.path.join(path, masterfiles_dir)) | ||
|
|
||
| _, files_dict = checksums_files(path, ignored_path_components=[".gitkeep"]) | ||
| files_dict = files_dict["files"] | ||
| files_dict = { | ||
| mpf_normalized_path(file, is_parentpath, masterfiles_dir): checksums | ||
| for file, checksums in files_dict.items() | ||
| } | ||
|
|
||
| return possible_policyset_paths(path, masterfiles_dir, is_parentpath, files_dict) | ||
|
|
||
|
|
||
| def test_possible_policyset_paths(): | ||
| with cwd("tests/sample/analyze"): | ||
| path = "." | ||
| assert ppp_scaffolded(path) == ["parent_dir/mfiles"] | ||
| assert ppp_scaffolded(path, "mfiles") == ["parent_dir/mfiles"] | ||
| assert ppp_scaffolded(path, "wrong_dirname") == ["parent_dir/mfiles"] | ||
|
|
||
| path = "parent_dir" | ||
| assert ppp_scaffolded(path) == ["mfiles"] | ||
| assert ppp_scaffolded(path, "mfiles") == ["mfiles"] | ||
| assert ppp_scaffolded(path, "wrong_dirname") == ["mfiles"] | ||
|
|
||
| path = "parent_dir/mfiles" | ||
| assert ppp_scaffolded(path) == [""] | ||
| assert ppp_scaffolded(path, "mfiles") == [""] | ||
| assert ppp_scaffolded(path, "wrong_dirname") == [""] | ||
|
|
||
| path = "parent_dir/mfiles/subdir" | ||
| assert ppp_scaffolded(path) == [".."] | ||
| assert ppp_scaffolded(path, "mfiles") == [".."] | ||
| assert ppp_scaffolded(path, "wrong_dirname") == [".."] | ||
|
|
||
| with cwd("tests/sample/analyze/parent_dir"): | ||
| path = "." | ||
| assert ppp_scaffolded(path) == ["mfiles"] | ||
| assert ppp_scaffolded(path, "mfiles") == ["mfiles"] | ||
| assert ppp_scaffolded(path, "wrong_dirname") == ["mfiles"] | ||
|
|
||
| path = "mfiles" | ||
| assert ppp_scaffolded(path) == [""] | ||
| assert ppp_scaffolded(path, "mfiles") == [""] | ||
| assert ppp_scaffolded(path, "wrong_dirname") == [""] | ||
|
|
||
| path = "mfiles/subdir" | ||
| assert ppp_scaffolded(path) == [".."] | ||
| assert ppp_scaffolded(path, "mfiles") == [".."] | ||
| assert ppp_scaffolded(path, "wrong_dirname") == [".."] | ||
|
|
||
| with cwd("tests/sample/analyze/parent_dir/mfiles"): | ||
| path = "." | ||
| assert ppp_scaffolded(path) == [""] | ||
| assert ppp_scaffolded(path, "mfiles") == [""] | ||
| assert ppp_scaffolded(path, "wrong_dirname") == [""] | ||
|
|
||
| path = "subdir" | ||
| assert ppp_scaffolded(path) == [".."] | ||
| assert ppp_scaffolded(path, "mfiles") == [".."] | ||
| assert ppp_scaffolded(path, "wrong_dirname") == [".."] | ||
|
|
||
| with cwd("tests/sample/analyze/parent_dir/mfiles/subdir"): | ||
| path = "." | ||
| assert ppp_scaffolded(path) == [".."] | ||
| assert ppp_scaffolded(path, "mfiles") == [".."] | ||
| assert ppp_scaffolded(path, "wrong_dirname") == [".."] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Preexisting, but is not this
name()function the same asos.path.basename()? If so, why not use it instead?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That could be done, I've simply never ended up doing so after writing the current implementation.