Skip to content

Commit 185c8e9

Browse files
committed
Add tests for CLI
#3
1 parent 33ae0b7 commit 185c8e9

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

openapi_python_client/cli.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from importlib.metadata import version
12
from typing import Optional
23

34
import click
@@ -6,6 +7,7 @@
67

78

89
@click.group()
10+
@click.version_option(version(__package__), prog_name="OpenAPI Python Client")
911
def cli() -> None:
1012
""" Entrypoint into CLI """
1113
pass
@@ -17,6 +19,9 @@ def cli() -> None:
1719
def generate(url: Optional[str], path: Optional[str]) -> None:
1820
""" Generate a new OpenAPI Client library """
1921
if not url and not path:
20-
print("You must either provide --url or --path")
22+
click.secho("You must either provide --url or --path", fg="red")
23+
exit(1)
24+
elif url and path:
25+
click.secho("Provide either --url or --path, not both", fg="red")
2126
exit(1)
2227
main(url=url, path=path)

tests/test_cli.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
from click.testing import CliRunner
2+
3+
4+
def test_generate_no_params():
5+
from openapi_python_client.cli import generate
6+
7+
runner = CliRunner()
8+
result = runner.invoke(generate)
9+
10+
assert result.exit_code == 1
11+
assert result.output == "You must either provide --url or --path\n"
12+
13+
14+
def test_generate_url_and_path():
15+
from openapi_python_client.cli import generate
16+
17+
runner = CliRunner()
18+
result = runner.invoke(generate, ["--url=blah", "--path=blahblah"])
19+
20+
assert result.exit_code == 1
21+
assert result.output == "Provide either --url or --path, not both\n"
22+
23+
24+
def test_generate_url(mocker):
25+
main = mocker.patch("openapi_python_client.main")
26+
url = mocker.MagicMock()
27+
from openapi_python_client.cli import generate
28+
29+
runner = CliRunner()
30+
result = runner.invoke(generate, [f"--url={url}"])
31+
32+
assert result.exit_code == 0
33+
main.assert_called_once_with(url=str(url), path=None)
34+
35+
36+
def test_generate_path(mocker):
37+
main = mocker.patch("openapi_python_client.main")
38+
path = mocker.MagicMock()
39+
from openapi_python_client.cli import generate
40+
41+
runner = CliRunner()
42+
result = runner.invoke(generate, [f"--path={path}"])
43+
44+
assert result.exit_code == 0
45+
main.assert_called_once_with(path=str(path), url=None)
46+
47+
48+
def test_version():
49+
from importlib.metadata import version
50+
from openapi_python_client.cli import cli
51+
52+
runner = CliRunner()
53+
result = runner.invoke(cli, ["--version"])
54+
55+
assert result.exit_code == 0
56+
assert result.output == f"OpenAPI Python Client, version {version('openapi-python-client')}\n"

0 commit comments

Comments
 (0)