Skip to content

Commit 91e22ba

Browse files
committed
Improve Cargo testing with more cases
Signed-off-by: ziad hany <[email protected]>
1 parent f33a0ce commit 91e22ba

File tree

4 files changed

+109
-18
lines changed

4 files changed

+109
-18
lines changed

minecode_pipelines/miners/cargo.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ def process_cargo_packages(cargo_index_repo, cloned_data_repo, config_repo, logg
4545

4646
try:
4747
next_commit = get_commit_at_distance_ahead(
48-
cargo_index_repo, checkpoints_last_commit, num_commits_ahead=COMMIT_BATCH_SIZE, branch_name="master"
48+
cargo_index_repo,
49+
checkpoints_last_commit,
50+
num_commits_ahead=COMMIT_BATCH_SIZE,
51+
branch_name="master",
4952
)
5053
except ValueError as e:
5154
logger(str(e))

minecode_pipelines/pipes/cargo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ def store_cargo_packages(packages, repo):
2929
ppath = hashid.get_package_purls_yml_file_path(base_purl)
3030
purl_file_full_path = Path(repo.working_dir) / ppath
3131
write_data_to_yaml_file(path=purl_file_full_path, data=updated_purls)
32-
return purl_file_full_path, base_purl
32+
return purl_file_full_path, base_purl
Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
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+
110
import json
211
import tempfile
312
from pathlib import Path
@@ -6,7 +15,7 @@
615
import saneyaml
716
from 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
1019
from minecode_pipelines.pipes.cargo import store_cargo_packages
1120

1221
DATA_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"}])
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
import tempfile
10+
from pathlib import Path
11+
from unittest import TestCase
12+
from git import Repo
13+
14+
from minecode_pipelines.pipes import get_commit_at_distance_ahead
15+
16+
17+
class GetCommitAtDistanceAheadIntegrationTests(TestCase):
18+
def setUp(self):
19+
# Create a temporary directory and init a repo
20+
self.tmpdir = tempfile.TemporaryDirectory()
21+
self.repo_path = Path(self.tmpdir.name)
22+
self.repo = Repo.init(self.repo_path)
23+
24+
# Configure identity (needed for commits)
25+
with self.repo.config_writer() as cw:
26+
cw.set_value("user", "name", "Test User")
27+
cw.set_value("user", "email", "[email protected]")
28+
29+
# Create 5 commits
30+
self.commits = []
31+
for i in range(5):
32+
file_path = self.repo_path / f"file{i}.txt"
33+
file_path.write_text(f"content {i}")
34+
self.repo.index.add([str(file_path)])
35+
commit = self.repo.index.commit(f"commit {i}")
36+
self.commits.append(commit.hexsha)
37+
38+
# By construction, self.commits[0] = first commit, self.commits[-1] = latest commit
39+
40+
def tearDown(self):
41+
self.tmpdir.cleanup()
42+
43+
def test_get_commit_at_distance_none_current_commit(self):
44+
# If current_commit is None, it should start from the empty tree hash
45+
result = get_commit_at_distance_ahead(
46+
self.repo, None, num_commits_ahead=3, branch_name="master"
47+
)
48+
# Should return the 3rd commit in history
49+
self.assertEqual(result, self.commits[2])
50+
51+
def test_get_commit_at_distance(self):
52+
# current_commit = first commit, ask for 3 commits ahead
53+
result = get_commit_at_distance_ahead(
54+
self.repo, self.commits[0], num_commits_ahead=3, branch_name="master"
55+
)
56+
# Should return the 3rd commit from start (self.commits[3])
57+
self.assertEqual(result, self.commits[3])
58+
59+
def test_raises_if_not_enough_commits(self):
60+
# From latest commit, ask for 10 ahead (only 0 available)
61+
with self.assertRaises(ValueError) as cm:
62+
get_commit_at_distance_ahead(
63+
self.repo, self.commits[-1], num_commits_ahead=10, branch_name="master"
64+
)
65+
self.assertIn("Not enough commits ahead; only 0 available.", str(cm.exception))

0 commit comments

Comments
 (0)