Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/scancode/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,24 @@ def echo_func(*_args, **_kwargs):
results = get_results(codebase, as_list=True, **requested_options)
elif return_codebase:
results = codebase
# Strip root from Resource paths. See #2985
if strip_root and not codebase.has_single_resource:
from commoncode.resource import strip_first_path_segment
new_resources_by_path = {}
for old_path, resource in list(codebase.resources_by_path.items()):
stripped_path = strip_first_path_segment(old_path)
resource.path = stripped_path
new_resources_by_path[stripped_path] = resource
codebase.resources_by_path = new_resources_by_path

# Patch parent() for direct children of root with empty parent path.
original_parent = codebase.resource_class.parent
def patched_parent(self, codebase_arg):
parent_path = self.parent_path()
if parent_path == '':
return codebase_arg.root
return original_parent(self, codebase_arg)
codebase.resource_class.parent = patched_parent

finally:
# remove temporary files
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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

Expand Down
74 changes: 74 additions & 0 deletions tests/scancode/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,80 @@ def test_run_scan_includes_outdated_in_extra():
assert results['headers'][0]['extra_data']['OUTDATED'] == 'out of date'


def test_run_scan_return_codebase_with_strip_root_strips_paths():
from scancode.cli import run_scan
test_dir = test_env.extract_test_tar('info/basic.tgz')
rc, codebase = run_scan(
test_dir,
info=True,
strip_root=True,
return_results=False,
return_codebase=True,
)
assert rc
assert codebase.root.path == ''
paths = [r.path for r in codebase.walk(skip_root=True)]
root_dir_name = os.path.basename(test_dir)
assert all(not p.startswith(root_dir_name) for p in paths)
assert 'basic' in paths
assert 'basic/main.c' in paths


def test_run_scan_return_codebase_with_strip_root_single_file_does_not_strip():
from scancode.cli import run_scan
test_file = test_env.get_test_loc('single/iproute.c')
rc, codebase = run_scan(
test_file,
info=True,
strip_root=True,
return_results=False,
return_codebase=True,
)
assert rc
assert codebase.root.path != ''
assert 'iproute.c' in codebase.root.path


def test_run_scan_return_codebase_with_strip_root_parent_traversal_works():
from scancode.cli import run_scan
test_dir = test_env.extract_test_tar('info/basic.tgz')
rc, codebase = run_scan(
test_dir,
info=True,
strip_root=True,
return_results=False,
return_codebase=True,
)
assert rc
basic_resource = codebase.get_resource('basic')
assert basic_resource is not None
parent = basic_resource.parent(codebase)
assert parent is not None
assert parent.is_root

main_c = codebase.get_resource('basic/main.c')
assert main_c is not None
main_parent = main_c.parent(codebase)
assert main_parent is not None
assert main_parent.path == 'basic'


def test_run_scan_return_codebase_without_strip_root_keeps_original_paths():
from scancode.cli import run_scan
test_dir = test_env.extract_test_tar('info/basic.tgz')
rc, codebase = run_scan(
test_dir,
info=True,
return_results=False,
return_codebase=True,
)
assert rc
root_path = codebase.root.path
assert root_path != ''
paths = [r.path for r in codebase.walk(skip_root=True)]
assert all(p.startswith(root_path) for p in paths)


def test_no_version_check_run_is_successful():
test_file = test_env.get_test_loc('single/iproute.c')
result_file = test_env.get_temp_file('json')
Expand Down