Skip to content

Commit 6df80a5

Browse files
committed
Update test and cli args
1 parent 1be73c0 commit 6df80a5

2 files changed

Lines changed: 12 additions & 18 deletions

File tree

openapi_generator_cli/__init__.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,11 @@ def run(args: list[str] | None = None) -> subprocess.CompletedProcess[bytes]:
5656
return subprocess.run(arguments, check=False) # noqa: S603
5757

5858

59-
def cli() -> None:
59+
def cli(argv: list[str] | None = None) -> None:
6060
"""Run the OpenAPI Generator CLI with the arguments provided on the command line."""
61-
args = []
62-
if len(sys.argv) > 1:
63-
args = sys.argv[1:]
61+
args = argv[1:] if argv is not None and len(argv) > 1 else []
6462
sys.exit(run(args).returncode)
6563

6664

6765
if __name__ == "__main__":
68-
cli()
66+
cli(sys.argv)

tests/test_cli.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,11 @@
22

33
import os
44
import re
5-
import subprocess
6-
from unittest.mock import MagicMock, patch
75

86
import pytest
97

108
from openapi_generator_cli import cli, run
119

12-
RETURN_CODE = 23
13-
1410

1511
def test_cli_version(capfd: pytest.CaptureFixture[str]) -> None:
1612
result = run(args=["version"])
@@ -49,14 +45,14 @@ def test_invalid_arg(capfd: pytest.CaptureFixture[str]) -> None:
4945
)
5046

5147

52-
@patch("openapi_generator_cli.run", autospec=True)
53-
@patch("sys.argv", ["openapi-generator-cli", "version"])
54-
def test_cli_exits_with_returncode(run_mock: MagicMock) -> None:
55-
run_mock.return_value = subprocess.CompletedProcess(args=[], returncode=RETURN_CODE)
56-
48+
def test_cli_invalid_arg(capfd: pytest.CaptureFixture[str]) -> None:
5749
with pytest.raises(SystemExit) as exc_info:
58-
cli()
59-
60-
assert exc_info.value.code == RETURN_CODE
50+
cli(["openapi-generator-cli", "--invalid-arg-404"])
51+
assert exc_info.value.code == 1
6152

62-
run_mock.assert_called_once_with(["version"])
53+
captured = capfd.readouterr()
54+
assert not captured.out
55+
assert (
56+
"Found unexpected parameters: [--invalid-arg-404]"
57+
in captured.err.split(os.linesep)[0]
58+
)

0 commit comments

Comments
 (0)