Skip to content

Commit 267ecbb

Browse files
committed
test: add unit test to mock the somef calls when --generate-codemeta flag is set
1 parent 086a767 commit 267ecbb

1 file changed

Lines changed: 72 additions & 0 deletions

File tree

tests/test_cli.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
"""Unit tests to verify CLI behavior for codemeta generation."""
2+
3+
import importlib
4+
from unittest.mock import MagicMock
5+
6+
cli_module = importlib.import_module("rsmetacheck.cli")
7+
8+
9+
REPO_URL = "https://github.com/SoftwareUnderstanding/sw-metadata-bot"
10+
11+
12+
def test_cli_with_generate_codemeta_adds_codemeta_output(monkeypatch, tmp_path):
13+
"""Ensure --generate-codemeta requests codemeta output in SoMEF command."""
14+
somef_output_dir = tmp_path / "somef_outputs"
15+
expected_codemeta = str(somef_output_dir / "somef_generated_codemeta.json")
16+
17+
run_analysis_mock = MagicMock()
18+
subprocess_run_mock = MagicMock()
19+
20+
monkeypatch.setattr(
21+
"sys.argv",
22+
[
23+
"rsmetacheck",
24+
"--input",
25+
REPO_URL,
26+
"--somef-output",
27+
str(somef_output_dir),
28+
"--generate-codemeta",
29+
],
30+
)
31+
monkeypatch.setattr(cli_module, "ensure_somef_configured", lambda: True)
32+
monkeypatch.setattr(cli_module, "run_analysis", run_analysis_mock)
33+
monkeypatch.setattr("rsmetacheck.run_somef.subprocess.run", subprocess_run_mock)
34+
35+
cli_module.cli()
36+
37+
command = subprocess_run_mock.call_args.args[0]
38+
assert command[0:2] == ["somef", "describe"]
39+
assert "-c" in command
40+
assert expected_codemeta in command
41+
42+
run_analysis_mock.assert_called_once()
43+
44+
45+
def test_cli_without_generate_codemeta_keeps_default_behavior(monkeypatch, tmp_path):
46+
"""Ensure default CLI call does not request codemeta output from SoMEF."""
47+
somef_output_dir = tmp_path / "somef_outputs"
48+
49+
run_analysis_mock = MagicMock()
50+
subprocess_run_mock = MagicMock()
51+
52+
monkeypatch.setattr(
53+
"sys.argv",
54+
[
55+
"rsmetacheck",
56+
"--input",
57+
REPO_URL,
58+
"--somef-output",
59+
str(somef_output_dir),
60+
],
61+
)
62+
monkeypatch.setattr(cli_module, "ensure_somef_configured", lambda: True)
63+
monkeypatch.setattr(cli_module, "run_analysis", run_analysis_mock)
64+
monkeypatch.setattr("rsmetacheck.run_somef.subprocess.run", subprocess_run_mock)
65+
66+
cli_module.cli()
67+
68+
command = subprocess_run_mock.call_args.args[0]
69+
assert command[0:2] == ["somef", "describe"]
70+
assert "-c" not in command
71+
72+
run_analysis_mock.assert_called_once()

0 commit comments

Comments
 (0)