1+ #
2+ # Copyright (c) nexB Inc. and others. All rights reserved.
3+ # purldb is a trademark of nexB Inc.
4+ # SPDX-License-Identifier: Apache-2.0
5+ # See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
6+ # See https://github.com/aboutcode-org/purldb for support or download.
7+ # See https://aboutcode.org for more information about nexB OSS projects.
8+ #
9+
110import json
211import tempfile
312from pathlib import Path
615import saneyaml
716from django .test import TestCase
817
9- from minecode_pipelines .pipes import write_packageurls_to_file
18+ from minecode_pipelines .pipes import write_data_to_yaml_file
1019from minecode_pipelines .pipes .cargo import store_cargo_packages
1120
1221DATA_DIR = Path (__file__ ).parent .parent / "test_data" / "cargo"
@@ -27,22 +36,24 @@ def test_collect_packages_from_cargo_calls_write(self, mock_write):
2736 with open (expected_file , encoding = "utf-8" ) as f :
2837 expected = saneyaml .load (f )
2938
30- repo = Mock ()
31- store_cargo_packages (packages , repo )
39+ with tempfile .TemporaryDirectory () as tmpdir :
40+ repo = Mock ()
41+ repo .working_dir = tmpdir
3242
33- mock_write .assert_called_once ()
34- args , kwargs = mock_write .call_args
35- called_repo , base_purl , written_packages = args
43+ store_cargo_packages (packages , repo )
3644
37- self .assertEqual (called_repo , repo )
45+ mock_write .assert_called_once ()
46+ args , kwargs = mock_write .call_args
47+ base_purl , written_packages = kwargs ["path" ], kwargs ["data" ]
3848
39- expected_base_purl = "aboutcode-packages-cargo-0/cargo/c5store/purls.yml"
40- self . assertEqual ( str ( base_purl ), str ( expected_base_purl ))
41- self . assertEqual ( written_packages , expected )
49+ expected_base_purl = (
50+ Path ( tmpdir ) / "aboutcode-packages-cargo-0" / "cargo" / "c5store" / "purls.yml"
51+ )
4252
43- def test_add_purl_result_with_mock_repo ( self ):
44- purls = [{ "purl" : "pkg:pypi/[email protected] " }, { "purl" : "pkg:pypi/[email protected] " }] 53+ self . assertEqual ( str ( base_purl ), str ( expected_base_purl ))
54+ self . assertEqual ( written_packages , expected )
4555
56+ def _assert_purls_written (self , purls ):
4657 with tempfile .TemporaryDirectory () as tmpdir :
4758 repo_dir = Path (tmpdir )
4859
@@ -52,11 +63,23 @@ def test_add_purl_result_with_mock_repo(self):
5263
5364 purls_file = repo_dir / "purls.yaml"
5465
55- relative_path = write_packageurls_to_file ( mock_repo , purls_file , purls )
66+ write_data_to_yaml_file ( purls_file , purls )
5667
57- written_file = repo_dir / relative_path
58- self .assertTrue (written_file .exists ())
68+ self .assertTrue (purls_file .exists ())
5969
60- with open (written_file , encoding = "utf-8" ) as f :
70+ with open (purls_file , encoding = "utf-8" ) as f :
6171 content = saneyaml .load (f )
72+
6273 self .assertEqual (content , purls )
74+
75+ def test_add_purl_result_with_mock_repo (self ):
76+ self ._assert_purls_written (
77+ [{
"purl" :
"pkg:pypi/[email protected] " }, {
"purl" :
"pkg:pypi/[email protected] " }]
78+ )
79+
80+ def test_add_empty_purl_result_with_mock_repo (self ):
81+ self ._assert_purls_written ([])
82+
83+ def test_add_invalid_purl_with_mock_repo (self ):
84+ # invalid but still written as empty file
85+ self ._assert_purls_written ([{"purl" : "pkg:pypi/django" }])
0 commit comments