Skip to content

Commit e6265f4

Browse files
author
Trong Nhan Mai
committed
test: refactor build spec unit tests and add tests for some small function in reproducible_central.py
1 parent ff7a502 commit e6265f4

File tree

8 files changed

+69
-11
lines changed

8 files changed

+69
-11
lines changed

src/macaron/build_spec_generator/cli_command_parser/maven_cli_parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ def apply_option_patch(
580580
setattr(new_maven_cli_options, attr_name, patch_value)
581581
continue
582582

583-
# Only for "-D/--define" we patch it differently than other options.
583+
# Only for "-D/--define" we patch it differently.
584584
if option_long_name == "--define":
585585
new_maven_cli_options.define = self._patch_properties_mapping(
586586
original_props=new_maven_cli_options.define or {},

src/macaron/build_spec_generator/reproducible_central/reproducible_central.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,8 @@ def _get_build_command_sequence(cmds_sequence: list[list[str]]) -> str:
114114
The build commands in the sequence will be && together, because RC's build spec
115115
is a shell script.
116116
"""
117-
result = ""
118-
if not cmds_sequence:
119-
return result
120-
121-
result += " ".join(remove_shell_quote(cmds_sequence[0]))
122-
for cmd in cmds_sequence[1:]:
123-
result += f"&& {' '.join(remove_shell_quote(cmd))}"
124-
117+
removed_shell_quote = [" ".join(remove_shell_quote(cmds)) for cmds in cmds_sequence]
118+
result = " && ".join(removed_shell_quote)
125119
return result
126120

127121

@@ -203,14 +197,14 @@ def _gen_reproducible_central_build_spec(
203197
logger.debug(
204198
"Generating build spec for %s with command patches:\n%s",
205199
purl,
206-
pformat(patches, indent=1),
200+
pformat(patches),
207201
)
208202

209203
group = purl.namespace
210204
artifact = purl.name
211205
version = purl.version
212206
if group is None or version is None:
213-
logger.error("Missing group and/or version for Maven purl %s.", purl.to_string())
207+
logger.error("Missing group and/or version for purl %s.", purl.to_string())
214208
return None
215209

216210
extra_comments.append(f"Input PURL - {purl}")
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Copyright (c) 2025 - 2025, Oracle and/or its affiliates. All rights reserved.
2+
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/.
3+
4+
"""This module contains the tests for Reproducible Central build spec generation"""
5+
6+
import pytest
7+
8+
from macaron.build_spec_generator.reproducible_central.reproducible_central import (
9+
_get_build_command_sequence,
10+
_get_extra_comments,
11+
)
12+
13+
14+
@pytest.mark.parametrize(
15+
("comments", "expected"),
16+
[
17+
pytest.param(
18+
[
19+
"Input PURL - pkg:maven/oracle/[email protected]",
20+
"Initial default JDK version 8 and default build command boo",
21+
],
22+
"# Input PURL - pkg:maven/oracle/[email protected]\n# Initial default JDK version 8 and default build command boo",
23+
),
24+
pytest.param(
25+
[
26+
"Input PURL - pkg:maven/oracle/[email protected]",
27+
],
28+
"# Input PURL - pkg:maven/oracle/[email protected]",
29+
),
30+
pytest.param(
31+
[],
32+
"",
33+
),
34+
],
35+
)
36+
def test_get_extra_comments(comments: list[str], expected: str) -> None:
37+
"""Test the _get_extra_comments function."""
38+
assert _get_extra_comments(comments) == expected
39+
40+
41+
@pytest.mark.parametrize(
42+
("cmds_sequence", "expected"),
43+
[
44+
pytest.param(
45+
[
46+
"make clean".split(),
47+
"mvn clean package".split(),
48+
],
49+
"make clean && mvn clean package",
50+
),
51+
pytest.param(
52+
[
53+
"mvn clean package".split(),
54+
],
55+
"mvn clean package",
56+
),
57+
],
58+
)
59+
def test_get_build_command_sequence(
60+
cmds_sequence: list[list[str]],
61+
expected: str,
62+
) -> None:
63+
"""Test the _get_build_command_sequence function."""
64+
assert _get_build_command_sequence(cmds_sequence) == expected

0 commit comments

Comments
 (0)