Skip to content

Commit

Permalink
Add unit test for get_best_checksum_matches #688
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Druez <tdruez@nexb.com>
  • Loading branch information
tdruez committed Apr 21, 2023
1 parent 8ef6dc6 commit 0da5530
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
13 changes: 6 additions & 7 deletions scanpipe/pipelines/develop_to_deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ class DevelopToDeploy(Pipeline):
@classmethod
def steps(cls):
return (
# cls.get_inputs,
# cls.extract_inputs_to_codebase_directory,
# cls.extract_archives_in_place,
# cls.collect_and_create_codebase_resources,
# cls.tag_empty_and_ignored_files,
cls.get_inputs,
cls.extract_inputs_to_codebase_directory,
cls.extract_archives_in_place,
cls.collect_and_create_codebase_resources,
cls.tag_empty_and_ignored_files,
cls.checksum_match,
# cls.purldb_match,
cls.purldb_match,
cls.java_to_class_match,
cls.path_match,
)
Expand Down Expand Up @@ -84,7 +84,6 @@ def tag_empty_and_ignored_files(self):

def checksum_match(self):
"""Match using SHA1 checksum."""
self.project.codebaserelations.all().delete()
d2d.checksum_match(project=self.project, checksum_field="sha1", logger=self.log)

def purldb_match(self):
Expand Down
2 changes: 2 additions & 0 deletions scanpipe/pipes/d2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ def _resource_path_match(to_resource, from_resources):
matches = from_resources.filter(path__endswith=f"/{current_path}")

if len(matches) > len(current_parts):
to_resource.status = "too-many-matches"
to_resource.save()
break

for match in matches:
Expand Down
6 changes: 3 additions & 3 deletions scanpipe/templates/scanpipe/relation_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<tr>
{% if forloop.first %}
<td class="break-all" rowspan="{{ resource.related_from.all|length }}">
<a href="{{ resource.get_absolute_url }}">{{ resource.path }}</a>
<a href="{{ resource.get_absolute_url }}#viewer">{{ resource.path }}</a>
</td>
{% endif %}
<td>
Expand All @@ -49,13 +49,13 @@
{% endif %}
</td>
<td class="break-all">
<a href="{{ relation.from_resource.get_absolute_url }}">{{ relation.from_resource.path }}</a>
<a href="{{ relation.from_resource.get_absolute_url }}#viewer">{{ relation.from_resource.path }}</a>
</td>
</tr>
{% empty %}
<tr>
<td class="break-all">
<a class="has-text-danger" href="{{ resource.get_absolute_url }}">{{ resource.path }}</a>
<a class="has-text-danger" href="{{ resource.get_absolute_url }}#viewer">{{ resource.path }}</a>
</td>
<td></td>
<td></td>
Expand Down
19 changes: 19 additions & 0 deletions scanpipe/tests/pipes/test_d2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

from django.test import TestCase

from scanpipe.models import CodebaseResource
from scanpipe.models import Project
from scanpipe.pipes import d2d

Expand Down Expand Up @@ -58,3 +59,21 @@ def test_scanpipe_d2d_get_extracted_subpath(self):

path = "a.jar-extract/subpath/b.jar-extract/subpath/file.ext"
self.assertEqual("subpath/file.ext", d2d.get_extracted_subpath(path))

def test_scanpipe_d2d_get_best_checksum_matches_same_name(self):
to_1 = CodebaseResource(name="package-1.0.ext", path="to/package-1.0.ext")
to_2 = CodebaseResource(name="package-2.0.ext", path="to/package-2.0.ext")
from_1 = CodebaseResource(name="package-1.0.ext", path="from/package-1.0.ext")
from_2 = CodebaseResource(name="package-2.0.ext", path="from/package-2.0.ext")
matches = [from_1, from_2]
self.assertEqual([from_1], d2d.get_best_checksum_matches(to_1, matches))
self.assertEqual([from_2], d2d.get_best_checksum_matches(to_2, matches))

def test_scanpipe_d2d_get_best_checksum_matches_extracted_subpath(self):
to_1 = CodebaseResource(path="to/jar-extract/a/package-1.0.ext")
to_2 = CodebaseResource(path="to/jar-extract/a/package-2.0.ext")
from_1 = CodebaseResource(path="from/src/a/package-1.0.ext")
from_2 = CodebaseResource(path="from/src/a/package-2.0.ext")
matches = [from_1, from_2]
self.assertEqual([from_1], d2d.get_best_checksum_matches(to_1, matches))
self.assertEqual([from_2], d2d.get_best_checksum_matches(to_2, matches))

0 comments on commit 0da5530

Please # to comment.