Skip to content

Commit a334d9f

Browse files
committed
Added unit tests for the new CLI
1 parent a85f3b2 commit a334d9f

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import sys
2+
from pathlib import Path
3+
4+
import pytest
5+
from pytest_mock import MockerFixture
6+
7+
import murfey
8+
from murfey.cli.generate_openapi_schema import run
9+
10+
params_matrix: tuple[tuple[str | None, str | None, bool], ...] = (
11+
# Target | Output | To File
12+
(None, None, False),
13+
("instrument-server", "json", True),
14+
("server", "yaml", False),
15+
("instrument-server", "yaml", False),
16+
("server", "json", True),
17+
)
18+
19+
20+
@pytest.mark.parametrize("test_params", params_matrix)
21+
def test_run(
22+
mocker: MockerFixture,
23+
tmp_path: Path,
24+
test_params: tuple[str | None, str | None, bool],
25+
):
26+
# Unpack test params
27+
target, output, to_file = test_params
28+
29+
# Mock out print() and exit()
30+
mock_print = mocker.patch("builtins.print")
31+
mock_exit = mocker.patch("builtins.exit")
32+
33+
# Construct the CLI args
34+
sys_args = [""]
35+
if target is not None:
36+
sys_args.extend(["-t", target])
37+
if output is not None:
38+
sys_args.extend(["-o", output])
39+
40+
target = target if target is not None else "server"
41+
output = output if output is not None else "yaml"
42+
if to_file:
43+
save_path = tmp_path / f"openapi.{output}"
44+
sys_args.extend(["-f", str(save_path)])
45+
else:
46+
save_path = Path(murfey.__path__[0]) / "util" / f"openapi-{target}.{output}"
47+
sys_args.extend(["--debug"])
48+
sys.argv = sys_args
49+
50+
# Run the function and check that it runs as expected
51+
run()
52+
print_calls = mock_print.call_args_list
53+
last_print_call = print_calls[-1]
54+
last_printed = last_print_call.args[0]
55+
assert last_printed.startswith("OpenAPI schema saved to")
56+
mock_exit.assert_called_once()
57+
assert save_path.exists()
58+
59+
60+
failure_params_matrix = (
61+
["-t", "blah"],
62+
["-o", "blah"],
63+
)
64+
65+
66+
@pytest.mark.parametrize("test_params", failure_params_matrix)
67+
def test_run_fails(test_params: list[str]):
68+
# Construct the CLI args
69+
sys_args = [""]
70+
sys_args.extend(test_params)
71+
sys.argv = sys_args
72+
73+
with pytest.raises(ValueError):
74+
run()

0 commit comments

Comments
 (0)