Skip to content

Commit 1a7bc88

Browse files
committed
Fix Scala case class and inner class mapping to source files
Signed-off-by: pradhyum6144 <pradhyum314@gmail.com>
1 parent 13b536b commit 1a7bc88

4 files changed

Lines changed: 37 additions & 30 deletions

File tree

scanpipe/pipes/d2d.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -168,24 +168,28 @@ def _map_jvm_to_class_resource(
168168
normalized_path = jvm_lang.get_normalized_path(
169169
path=to_resource.path, extension=extension
170170
)
171+
171172
match = pathmap.find_paths(path=normalized_path, index=from_classes_index)
172-
if not match:
173-
if jvm_lang.name == "scala":
174-
package_path = str(Path(normalized_path).parent)
175-
potential_sources = from_resources.filter(
176-
path__startswith=package_path,
177-
extension__in=jvm_lang.source_extensions
173+
174+
if not match and jvm_lang.name == "scala":
175+
package_path = str(Path(to_resource.path).parent)
176+
potential_sources = from_resources.filter(
177+
path__startswith=package_path.replace("to/", "from/"),
178+
extension__in=jvm_lang.source_extensions,
179+
)
180+
for from_resource in potential_sources:
181+
from_source_root_parts = from_resource.path.strip("/").split("/")
182+
from_source_root = "/".join(from_source_root_parts[:-1])
183+
pipes.make_relation(
184+
from_resource=from_resource,
185+
to_resource=to_resource,
186+
map_type=jvm_lang.binary_map_type,
187+
extra_data={"from_source_root": f"{from_source_root}/"},
178188
)
179-
for from_resource in potential_sources:
180-
from_source_root_parts = from_resource.path.strip("/").split("/")
181-
from_source_root = "/".join(from_source_root_parts[:-1])
182-
pipes.make_relation(
183-
from_resource=from_resource,
184-
to_resource=to_resource,
185-
map_type=jvm_lang.binary_map_type,
186-
extra_data={"from_source_root": f"{from_source_root}/"},
187-
)
188-
return
189+
continue
190+
191+
if not match:
192+
continue
189193

190194
for resource_id in match.resource_ids:
191195
from_resource = from_resources.get(id=resource_id)

scanpipe/pipes/jvm.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,12 +190,12 @@ def get_normalized_path(cls, path, extension):
190190
)
191191
path_obj = Path(path.strip("/"))
192192
class_name = path_obj.name
193-
193+
194194
if "$" in class_name:
195195
class_name, _, _ = class_name.partition("$")
196196
else:
197197
class_name, _, _ = class_name.partition(".")
198-
198+
199199
return str(path_obj.parent / f"{class_name}{extension}")
200200

201201

scanpipe/pipes/resolve.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def get_data_from_manifests(project, package_registry, manifest_resources, model
108108
if "pypi" in manifests_by_type:
109109
pypi_resources = manifests_by_type["pypi"]
110110
pypi_locations = [resource.location for resource in pypi_resources]
111-
111+
112112
resolver = package_registry.get("pypi")
113113
if resolver:
114114
try:
@@ -117,7 +117,7 @@ def get_data_from_manifests(project, package_registry, manifest_resources, model
117117
for package_data in packages:
118118
package_data["codebase_resources"] = pypi_resources
119119
resolved_packages.extend(packages)
120-
120+
121121
for resource in pypi_resources:
122122
if headers := get_manifest_headers(resource):
123123
sboms_headers[resource.name] = headers
@@ -135,7 +135,7 @@ def get_data_from_manifests(project, package_registry, manifest_resources, model
135135
model=model,
136136
object_instance=resource,
137137
)
138-
138+
139139
del manifests_by_type["pypi"]
140140

141141
for package_type, resources in manifests_by_type.items():
@@ -267,13 +267,14 @@ def get_manifest_resources(project):
267267
def resolve_pypi_packages(input_location=None, input_locations=None):
268268
"""
269269
Resolve the PyPI packages from requirement file(s).
270-
270+
271271
Args:
272272
input_location: Single requirement file path (for backward compatibility)
273273
input_locations: List of requirement file paths (for batch processing)
274-
274+
275275
Returns:
276276
List of resolved package data dictionaries
277+
277278
"""
278279
# Handle both single file and multiple files
279280
if input_locations:
@@ -282,7 +283,7 @@ def resolve_pypi_packages(input_location=None, input_locations=None):
282283
requirement_files = [input_location]
283284
else:
284285
raise ValueError("Either input_location or input_locations must be provided")
285-
286+
286287
python_version = f"{sys.version_info.major}{sys.version_info.minor}"
287288
operating_system = "linux"
288289

scanpipe/tests/pipes/test_resolve.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -393,19 +393,22 @@ def test_scanpipe_pipes_resolve_pypi_packages_multiple_files(self, mock_resolve)
393393

394394
req_files = ["requirements1.txt", "requirements2.txt"]
395395
packages = resolve.resolve_pypi_packages(input_locations=req_files)
396-
396+
397397
mock_resolve.assert_called_once()
398398
call_args = mock_resolve.call_args
399399
self.assertEqual(req_files, call_args.kwargs["requirement_files"])
400-
400+
401401
self.assertEqual(2, len(packages))
402402
self.assertEqual("pip", packages[0]["name"])
403403

404404
@mock.patch("scanpipe.pipes.resolve.python_inspector.resolve_dependencies")
405405
def test_scanpipe_pipes_resolve_pypi_packages_backward_compatibility(
406406
self, mock_resolve
407407
):
408-
"""Test that resolve_pypi_packages still works with single file (backward compatibility)."""
408+
"""
409+
Test that resolve_pypi_packages still works with single file
410+
(backward compatibility).
411+
"""
409412
inspector_output_location = (
410413
self.data / "resolve" / "python_inspector_resolve_dependencies.json"
411414
)
@@ -415,10 +418,9 @@ def test_scanpipe_pipes_resolve_pypi_packages_backward_compatibility(
415418
mock_resolve.return_value = mock.Mock(packages=inspector_output["packages"])
416419

417420
packages = resolve.resolve_pypi_packages(input_location="requirements.txt")
418-
421+
419422
mock_resolve.assert_called_once()
420423
call_args = mock_resolve.call_args
421424
self.assertEqual(["requirements.txt"], call_args.kwargs["requirement_files"])
422-
423-
self.assertEqual(2, len(packages))
424425

426+
self.assertEqual(2, len(packages))

0 commit comments

Comments
 (0)