Skip to content

Commit

Permalink
Add unit test for codebase_relation_diff_view #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 24, 2023
1 parent a002cc7 commit 1dab3e2
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
2 changes: 1 addition & 1 deletion scanpipe/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ def _raise_if_run_in_progress(self):
)

def setup_work_directory(self):
"""Create all of the work_directory structure and skips if already existing."""
"""Create all the work_directory structure and skips if already existing."""
for subdirectory in self.WORK_DIRECTORIES:
Path(self.work_directory, subdirectory).mkdir(parents=True, exist_ok=True)

Expand Down
4 changes: 2 additions & 2 deletions scanpipe/templates/scanpipe/relation_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@
{% if relation.extra_data.path_score %}
{{ relation.extra_data.path_score }}
{% endif %}
{% if relation.match_type == "path" %}
{% if relation.match_type == "path" and resource.is_text and relation.from_resource.is_text %}
<div>
<a href="/project/{{ project.uuid }}/resources/diff/?pk_a={{ resource.path }}&pk_b={{ relation.from_resource.path }}" target="_blank">diff</a>
<a href="{% url 'resource_diff' project.uuid %}?from_path={{ resource.path }}&to_path={{ relation.from_resource.path }}" target="_blank">diff</a>
{% if relation.extra_data.diff_ratio %}
ratio: {{ relation.extra_data.diff_ratio }}
{% endif %}
Expand Down
35 changes: 35 additions & 0 deletions scanpipe/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

from scanpipe.models import CodebaseResource
from scanpipe.models import Project
from scanpipe.pipes.input import copy_inputs
from scanpipe.tests import license_policies_index
from scanpipe.views import ProjectDetailView

Expand Down Expand Up @@ -459,3 +460,37 @@ def test_scanpipe_views_codebase_resource_details_annotations_missing_policy(sel
'{"start_line": 1, "end_line": 2, "text": null, "className": "ace_info"}'
)
self.assertContains(response, expected)

def test_scanpipe_views_codebase_relation_diff_view(self):
url = reverse("resource_diff", args=[self.project1.uuid])
data = {
"from_path": "",
"to_path": "",
}
response = self.client.get(url, data=data)
expected = "The requested resource was not found on this server."
self.assertContains(response, expected, status_code=404)

resource_files = [
self.data_location / "codebase" / "a.txt",
self.data_location / "codebase" / "b.txt",
]
copy_inputs(resource_files, self.project1.codebase_path)
resource1 = CodebaseResource.objects.create(
project=self.project1,
path="a.txt",
type=CodebaseResource.Type.FILE,
is_text=True,
)
resource2 = CodebaseResource.objects.create(
project=self.project1,
path="b.txt",
type=CodebaseResource.Type.FILE,
is_text=True,
)
data = {
"from_path": resource1.path,
"to_path": resource2.path,
}
response = self.client.get(url, data=data)
self.assertContains(response, '<table class="diff"')
11 changes: 7 additions & 4 deletions scanpipe/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1138,10 +1138,13 @@ def codebase_resource_diff_view(request, uuid):
project = get_object_or_404(Project, uuid=uuid)

project_files = project.codebaseresources.files()
from_pk = request.GET.get("pk_a")
to_pk = request.GET.get("pk_b")
from_resource = get_object_or_404(project_files, path=from_pk)
to_resource = get_object_or_404(project_files, path=to_pk)
from_path = request.GET.get("from_path")
to_path = request.GET.get("to_path")
from_resource = get_object_or_404(project_files, path=from_path)
to_resource = get_object_or_404(project_files, path=to_path)

if not (from_resource.is_text and to_resource.is_text):
raise Http404("Cannot diff on binary files")

from_lines = from_resource.location_path.read_text().split("\n")
to_lines = to_resource.location_path.read_text().split("\n")
Expand Down

0 comments on commit 1dab3e2

Please # to comment.