Skip to content

Commit a8c6bdc

Browse files
committed
Test CLI exit code through script
1 parent 1be73c0 commit a8c6bdc

1 file changed

Lines changed: 10 additions & 16 deletions

File tree

tests/test_cli.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
import os
44
import re
5+
import shutil
56
import subprocess
6-
from unittest.mock import MagicMock, patch
7+
from typing import TYPE_CHECKING
78

8-
import pytest
9+
from openapi_generator_cli import run
910

10-
from openapi_generator_cli import cli, run
11-
12-
RETURN_CODE = 23
11+
if TYPE_CHECKING:
12+
import pytest
1313

1414

1515
def test_cli_version(capfd: pytest.CaptureFixture[str]) -> None:
@@ -49,14 +49,8 @@ def test_invalid_arg(capfd: pytest.CaptureFixture[str]) -> None:
4949
)
5050

5151

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-
57-
with pytest.raises(SystemExit) as exc_info:
58-
cli()
59-
60-
assert exc_info.value.code == RETURN_CODE
61-
62-
run_mock.assert_called_once_with(["version"])
52+
def test_cli_script_exits_with_returncode() -> None:
53+
cli_path = shutil.which("openapi-generator-cli")
54+
assert cli_path is not None
55+
result = subprocess.run([cli_path, "this-is-invalid-subcommand"], check=False) # noqa: S603
56+
assert result.returncode == 1

0 commit comments

Comments
 (0)