From 0da5530ff23bbd31bbad500225b3d7eaec290a03 Mon Sep 17 00:00:00 2001 From: Thomas Druez Date: Fri, 21 Apr 2023 20:56:33 +0400 Subject: [PATCH] Add unit test for get_best_checksum_matches #688 Signed-off-by: Thomas Druez --- scanpipe/pipelines/develop_to_deploy.py | 13 ++++++------- scanpipe/pipes/d2d.py | 2 ++ .../templates/scanpipe/relation_list.html | 6 +++--- scanpipe/tests/pipes/test_d2d.py | 19 +++++++++++++++++++ 4 files changed, 30 insertions(+), 10 deletions(-) diff --git a/scanpipe/pipelines/develop_to_deploy.py b/scanpipe/pipelines/develop_to_deploy.py index de0d31444..a0cd1405c 100644 --- a/scanpipe/pipelines/develop_to_deploy.py +++ b/scanpipe/pipelines/develop_to_deploy.py @@ -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, ) @@ -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): diff --git a/scanpipe/pipes/d2d.py b/scanpipe/pipes/d2d.py index 7475040c1..3a8c56b8a 100644 --- a/scanpipe/pipes/d2d.py +++ b/scanpipe/pipes/d2d.py @@ -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: diff --git a/scanpipe/templates/scanpipe/relation_list.html b/scanpipe/templates/scanpipe/relation_list.html index 41c5d0626..76b1c14e5 100644 --- a/scanpipe/templates/scanpipe/relation_list.html +++ b/scanpipe/templates/scanpipe/relation_list.html @@ -36,7 +36,7 @@ {% if forloop.first %} - {{ resource.path }} + {{ resource.path }} {% endif %} @@ -49,13 +49,13 @@ {% endif %} - {{ relation.from_resource.path }} + {{ relation.from_resource.path }} {% empty %} - {{ resource.path }} + {{ resource.path }} diff --git a/scanpipe/tests/pipes/test_d2d.py b/scanpipe/tests/pipes/test_d2d.py index f9cceaf37..348df12f5 100644 --- a/scanpipe/tests/pipes/test_d2d.py +++ b/scanpipe/tests/pipes/test_d2d.py @@ -24,6 +24,7 @@ from django.test import TestCase +from scanpipe.models import CodebaseResource from scanpipe.models import Project from scanpipe.pipes import d2d @@ -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))