Skip to content

Commit 62871ae

Browse files
authored
Display the entire content of map files (#1014)
* Display the entire content of map files Signed-off-by: Keshav Priyadarshi <git@keshav.space> * Use context manager while opening file Signed-off-by: Keshav Priyadarshi <git@keshav.space> --------- Signed-off-by: Keshav Priyadarshi <git@keshav.space>
1 parent 4b3a620 commit 62871ae

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

scanpipe/models.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2422,6 +2422,17 @@ def file_content(self):
24222422
for optimal compatibility.
24232423
"""
24242424
from textcode.analysis import numbered_text_lines
2425+
from typecode import get_type
2426+
2427+
# When reading a map file, Textcode only provides the content inside
2428+
# `sourcesContent`, which can be misleading during any kind of review.
2429+
# This workaround ensures that the entire content of map files is displayed.
2430+
file_type = get_type(self.location)
2431+
if file_type.is_js_map:
2432+
with open(self.location, "r") as file:
2433+
content = json.load(file)
2434+
2435+
return json.dumps(content, indent=2)
24252436

24262437
numbered_lines = numbered_text_lines(self.location)
24272438
numbered_lines = self._regroup_numbered_lines(numbered_lines)

scanpipe/tests/test_models.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
# Visit https://github.com/nexB/scancode.io for support and download.
2222

2323
import io
24+
import json
2425
import shutil
2526
import sys
2627
import tempfile
@@ -1229,6 +1230,18 @@ def test_scanpipe_codebase_resource_model_file_content(self):
12291230
line_count = len(resource.file_content.split("\n"))
12301231
self.assertEqual(101, line_count)
12311232

1233+
def test_scanpipe_codebase_resource_model_file_content_for_map(self):
1234+
map_file_path = self.data_location / "d2d-javascript/to/main.js.map"
1235+
copy_input(map_file_path, self.project1.codebase_path)
1236+
resource = self.project1.codebaseresources.create(path="main.js.map")
1237+
1238+
with open(map_file_path, "r") as file:
1239+
expected = json.load(file)
1240+
1241+
result = json.loads(resource.file_content)
1242+
1243+
self.assertEqual(expected, result)
1244+
12321245
def test_scanpipe_codebase_resource_model_compliance_alert(self):
12331246
scanpipe_app.license_policies_index = license_policies_index
12341247
resource = CodebaseResource.objects.create(project=self.project1, path="file")

0 commit comments

Comments
 (0)