Fix missing strip_root behavior in Codebase return path#4711
Fix missing strip_root behavior in Codebase return path#4711Kaushik-Kumar-CEG wants to merge 1 commit intoaboutcode-org:developfrom
Conversation
5c4dfb8 to
956183e
Compare
|
@JonoYang This PR is ready for review The issue was that The fix rebuilds Kindly review my fix and let me know if you have any feedback :) |
|
@JonoYang |
AyanSinhaMahapatra
left a comment
There was a problem hiding this comment.
@Kaushik-Kumar-CEG code is not mergable/maintainable without tests.
| codebase.resource_class.parent = patched_parent | ||
|
|
||
| finally: | ||
| # remove temporary files |
There was a problem hiding this comment.
@Kaushik-Kumar-CEG I see no tests added at all, how would I know your code is working or not without doing all the research myself?
There was a problem hiding this comment.
I should've added them from the start. Added 4 tests in test_cli.py that cover strip_root with return_codebase — including the parent() fix this comment is on
| new_resources_by_path[stripped_path] = resource | ||
| codebase.resources_by_path = new_resources_by_path | ||
|
|
||
| # Fix parent() to handle empty parent_path for direct children of root. |
There was a problem hiding this comment.
The comments are too descriptive, you can keep a short summary, and have extra comments here in github as PR comments,
There was a problem hiding this comment.
got it, trimmed them down to one-liners.
…ase mode When using cli.run_scan() with strip_root=True and return_codebase=True, the root directory was not stripped from Resource paths. This adds path stripping logic for the return_codebase branch and patches parent() for direct children of root. Adds tests for stripped paths, single file behavior, parent traversal, and regression guard. Fixes: aboutcode-org#2985 Signed-off-by: Kaushik <kaushikrjpm10@gmail.com>
956183e to
a830f08
Compare
|
@AyanSinhaMahapatra updated the PR based on your review Added 4 tests for strip_root + return_codebase (path stripping, single file, parent traversal, regression guard) |
Fixes #2985
Summary
Fixed strip_root parameter not stripping root directory from Resource paths when using cli.run_scan() programmatically.
Problem
--strip-rootworked correctlyreturn_codebase=Truedid not strip root from Resource pathsCodebaseobject instead of JSONSolution
resources_by_pathwith stripped paths after Codebase creationstrip_first_path_segment()to remove root prefix from each ResourceResource.parent()method to return root Resource (not empty string) for direct children of root by monkey-patching whenstrip_rootis enabledChanges
src/scancode/cli.py: Added path stripping logic and parent() fix forreturn_codebasebranchVerification
Resource.parent(codebase)correctly returns root Resource for direct childrenScreenshots of Fix
BEFORE fix:

AFTER Fix

Tasks
Run tests locally to check for errors.