Skip to content

Commit 15296e9

Browse files
committed
minor fixes and adjustments following review feedback
Signed-off-by: Aayush Kumar <aayush214.kumar@gmail.com>
1 parent 62e9462 commit 15296e9

File tree

5 files changed

+13
-10
lines changed

5 files changed

+13
-10
lines changed

scanpipe/migrations/0073_codebaseresource_parent_path_and_more.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Migration(migrations.Migration):
1313
migrations.AddField(
1414
model_name='codebaseresource',
1515
name='parent_path',
16-
field=models.CharField(blank=True, help_text='Path of the immediate parent directory of a resource. For top level resources the value is set to None', max_length=2000, null=True),
16+
field=models.CharField(blank=True, help_text='The path of the resource\'s parent directory. Set to None for top-level (root) resources. Used to efficiently retrieve a directory\'s contents.', max_length=2000, null=True),
1717
),
1818
migrations.AddIndex(
1919
model_name='codebaseresource',

scanpipe/models.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2701,8 +2701,9 @@ class CodebaseResource(
27012701
null=True,
27022702
blank=True,
27032703
help_text=_(
2704-
"Path of the immediate parent directory of a resource. "
2705-
"For top level resources the value is set to None"
2704+
"The path of the resource's parent directory. "
2705+
"Set to None for top-level (root) resources. "
2706+
"Used to efficiently retrieve a directory's contents."
27062707
),
27072708
)
27082709

@@ -2814,7 +2815,7 @@ def __str__(self):
28142815

28152816
def save(self, *args, **kwargs):
28162817
if self.path and not self.parent_path:
2817-
self.parent_path = parent_directory(str(self.path), with_trail=False)
2818+
self.parent_path = self.parent_directory()
28182819
super().save(*args, **kwargs)
28192820

28202821
def get_absolute_url(self):
@@ -2887,7 +2888,8 @@ def get_path_segments_with_subpath(self):
28872888

28882889
def parent_directory(self):
28892890
"""Return the parent path for this CodebaseResource or None."""
2890-
return parent_directory(self.path, with_trail=False)
2891+
parent_path = parent_directory(str(self.path), with_trail=False)
2892+
return None if parent_path == "" else parent_path
28912893

28922894
def has_parent(self):
28932895
"""

scanpipe/pipes/rootfs.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,9 @@ def get_res(parent, fname):
139139
rootfs_path=rootfs_path,
140140
)
141141

142+
# Explicitly yields the root directory as a resource when `with_dir` is True
142143
if with_dir:
143-
rootfs_path = pipes.normalize_path("")
144+
rootfs_path = "/"
144145
yield Resource(
145146
location=location,
146147
rootfs_path=rootfs_path,

scanpipe/tests/test_models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1648,7 +1648,7 @@ def test_scanpipe_can_compute_compliance_alert_for_license_exceptions(self):
16481648
def test_scanpipe_codebase_root_parent_path(self):
16491649
resource1 = self.project1.codebaseresources.create(path="file")
16501650

1651-
self.assertEqual("", resource1.parent_path)
1651+
self.assertIsNone(resource1.parent_path)
16521652

16531653
def test_scanpipe_codebase_regular_parent_path(self):
16541654
resource2 = self.project1.codebaseresources.create(path="dir1/dir2/file")

scanpipe/tests/test_pipelines.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,7 @@ def test_scanpipe_scan_codebase_creates_top_level_paths(self):
880880
expected_top_level_paths = ["is-npm-1.0.0.tgz", "is-npm-1.0.0.tgz-extract"]
881881

882882
top_level_resources = project1.codebaseresources.filter(parent_path=None)
883-
top_level_paths = [res.path for res in top_level_resources]
883+
top_level_paths = [resource.path for resource in top_level_resources]
884884

885885
self.assertListEqual(top_level_paths, expected_top_level_paths)
886886

@@ -906,14 +906,14 @@ def test_scanpipe_scan_codebase_creates_parent_path_field(self):
906906
]
907907

908908
top_level_resources = project1.codebaseresources.filter(parent_path=None)
909-
top_level_paths = [res.path for res in top_level_resources]
909+
top_level_paths = [resource.path for resource in top_level_resources]
910910

911911
self.assertListEqual(top_level_paths, expected_top_level_paths)
912912

913913
nested_resources = project1.codebaseresources.filter(
914914
parent_path="is-npm-1.0.0.tgz-extract/package"
915915
)
916-
nested_paths = [res.path for res in nested_resources]
916+
nested_paths = [resource.path for resource in nested_resources]
917917

918918
self.assertListEqual(nested_paths, expected_nested_paths)
919919

0 commit comments

Comments
 (0)