Skip to content

Commit c6511e2

Browse files
committed
Add unit test for InspectManifest #284
Signed-off-by: Thomas Druez <[email protected]>
1 parent 8c26f23 commit c6511e2

File tree

3 files changed

+47
-7
lines changed

3 files changed

+47
-7
lines changed

scanpipe/pipelines/inspect_manifest.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,16 @@
2929

3030
def resolve_pypi_packages(input_location):
3131
"""
32-
https://github.com/nexB/scancode-toolkit/blob/develop/requirements.txt
33-
https://raw.githubusercontent.com/nexB/python-inspector/main/requirements.txt
32+
Resolve the PyPI packages from the `input_location` requirements file.
3433
"""
35-
inspector_output = resolver_api(requirement_files=[input_location])
36-
resolved_packages = inspector_output.packages
37-
return resolved_packages
34+
inspector_output = resolver_api(
35+
requirement_files=[input_location],
36+
prefer_source=True,
37+
)
38+
return inspector_output.packages
3839

3940

40-
# `default_package_type`: resolver callable
41+
# Mapping between the `default_package_type` its related resolver function
4142
resolver_registry = {
4243
"pypi": resolve_pypi_packages,
4344
}
@@ -52,6 +53,8 @@ def get_default_package_type(input_location):
5253
class InspectManifest(Pipeline):
5354
"""
5455
A pipeline to inspect one or more manifest files and resolve its packages.
56+
57+
Only PyPI requirements file are supported.
5558
"""
5659

5760
@classmethod
@@ -75,6 +78,8 @@ def create_packages_from_manifest(self):
7578
"""
7679
for input_location in self.input_locations:
7780
default_package_type = get_default_package_type(input_location)
81+
if not default_package_type:
82+
raise Exception(f"No package type found for {input_location}")
7883

7984
resolver = resolver_registry.get(default_package_type)
8085
if not resolver:

scanpipe/tests/test_pipelines.py

+35
Original file line numberDiff line numberDiff line change
@@ -624,3 +624,38 @@ def test_scanpipe_check_vulnerabilities_pipeline_integration_test(
624624
package1.refresh_from_db()
625625
expected = {"discovered_vulnerabilities": vulnerability_data}
626626
self.assertEqual(expected, package1.extra_data)
627+
628+
@mock.patch("scanpipe.pipelines.inspect_manifest.resolver_api")
629+
def test_scanpipe_inspect_manifest_pipeline_integration_test(self, resolver_api):
630+
resolver_api.return_value = mock.Mock(packages=[])
631+
632+
pipeline_name = "inspect_manifest"
633+
project1 = Project.objects.create(name="Analysis")
634+
635+
run = project1.add_pipeline(pipeline_name)
636+
pipeline = run.make_pipeline_instance()
637+
638+
project1.move_input_from(tempfile.mkstemp()[1])
639+
exitcode, out = pipeline.execute()
640+
self.assertEqual(1, exitcode, msg=out)
641+
self.assertIn("No package type found for", out)
642+
643+
project1.reset(keep_input=False)
644+
run = project1.add_pipeline(pipeline_name)
645+
pipeline = run.make_pipeline_instance()
646+
647+
project1.move_input_from(tempfile.mkstemp(suffix="requirements.txt")[1])
648+
exitcode, out = pipeline.execute()
649+
self.assertEqual(1, exitcode, msg=out)
650+
self.assertIn("No packages could be resolved.", out)
651+
652+
resolver_api.return_value = mock.Mock(packages=[package_data1])
653+
exitcode, out = pipeline.execute()
654+
self.assertEqual(0, exitcode, msg=out)
655+
656+
self.assertEqual(1, project1.discoveredpackages.count())
657+
discoveredpackage = project1.discoveredpackages.get()
658+
exclude_fields = ["qualifiers", "release_date", "size"]
659+
for field_name, value in package_data1.items():
660+
if value and field_name not in exclude_fields:
661+
self.assertEqual(value, getattr(discoveredpackage, field_name))

setup.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ install_requires =
7676
# FetchCode
7777
fetchcode-container==1.2.3.210512; sys_platform == "linux"
7878
# Python-inspector
79-
python-inspector==0.9.0
79+
python-inspector==0.9.1
8080
# Utilities
8181
XlsxWriter==3.0.3
8282
requests==2.28.1

0 commit comments

Comments
 (0)