Skip to content

Commit acfc32b

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

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
@@ -2691,8 +2691,9 @@ class CodebaseResource(
26912691
null=True,
26922692
blank=True,
26932693
help_text=_(
2694-
"Path of the immediate parent directory of a resource. "
2695-
"For top level resources the value is set to None"
2694+
"The path of the resource's parent directory. "
2695+
"Set to None for top-level (root) resources. "
2696+
"Used to efficiently retrieve a directory's contents."
26962697
),
26972698
)
26982699

@@ -2798,7 +2799,7 @@ def __str__(self):
27982799

27992800
def save(self, *args, **kwargs):
28002801
if self.path and not self.parent_path:
2801-
self.parent_path = parent_directory(str(self.path), with_trail=False)
2802+
self.parent_path = self.parent_directory()
28022803
super().save(*args, **kwargs)
28032804

28042805
@property
@@ -2868,7 +2869,8 @@ def get_path_segments_with_subpath(self):
28682869

28692870
def parent_directory(self):
28702871
"""Return the parent path for this CodebaseResource or None."""
2871-
return parent_directory(self.path, with_trail=False)
2872+
parent_path = parent_directory(str(self.path), with_trail=False)
2873+
return None if parent_path == "" else parent_path
28722874

28732875
def has_parent(self):
28742876
"""

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
@@ -1596,7 +1596,7 @@ def test_scanpipe_codebase_resource_model_compliance_alert_update_fields(self):
15961596
def test_scanpipe_codebase_root_parent_path(self):
15971597
resource1 = self.project1.codebaseresources.create(path="file")
15981598

1599-
self.assertEqual("", resource1.parent_path)
1599+
self.assertIsNone(resource1.parent_path)
16001600

16011601
def test_scanpipe_codebase_regular_parent_path(self):
16021602
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
@@ -877,7 +877,7 @@ def test_scanpipe_scan_codebase_creates_top_level_paths(self):
877877
expected_top_level_paths = ["is-npm-1.0.0.tgz", "is-npm-1.0.0.tgz-extract"]
878878

879879
top_level_resources = project1.codebaseresources.filter(parent_path=None)
880-
top_level_paths = [res.path for res in top_level_resources]
880+
top_level_paths = [resource.path for resource in top_level_resources]
881881

882882
self.assertListEqual(top_level_paths, expected_top_level_paths)
883883

@@ -903,14 +903,14 @@ def test_scanpipe_scan_codebase_creates_parent_path_field(self):
903903
]
904904

905905
top_level_resources = project1.codebaseresources.filter(parent_path=None)
906-
top_level_paths = [res.path for res in top_level_resources]
906+
top_level_paths = [resource.path for resource in top_level_resources]
907907

908908
self.assertListEqual(top_level_paths, expected_top_level_paths)
909909

910910
nested_resources = project1.codebaseresources.filter(
911911
parent_path="is-npm-1.0.0.tgz-extract/package"
912912
)
913-
nested_paths = [res.path for res in nested_resources]
913+
nested_paths = [resource.path for resource in nested_resources]
914914

915915
self.assertListEqual(nested_paths, expected_nested_paths)
916916

0 commit comments

Comments
 (0)