Skip to content

Commit c19053e

Browse files
committed
test cli config
1 parent 4f79faa commit c19053e

6 files changed

Lines changed: 177 additions & 4 deletions

File tree

poetry.lock

Lines changed: 80 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ tomlkit = "^0.11.6"
2121

2222
[tool.poetry.group.dev.dependencies]
2323
openapi-python-client = "^0.13.1"
24+
pytest = "^7.2.1"
2425

2526
[build-system]
2627
requires = ["poetry-core>=1.0.0"]

taskbadger/cli.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,7 @@ def docs():
4949
@app.command()
5050
def info(ctx: typer.Context):
5151
config = ctx.meta["tb_config"]
52-
print(f"Default Organization: {config.organization_slug or '-'}")
53-
print(f"Default Project: {config.project_slug or '-'}")
54-
print(f"Auth Token: {config.token or '-'}")
52+
print(str(config))
5553

5654

5755
@app.callback()

taskbadger/config.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import dataclasses
2+
import inspect
23
import os
34
from pathlib import Path
45

@@ -33,6 +34,13 @@ def from_dict(config_dict, **overrides) -> "Config":
3334
project_slug=overrides.get("project") or _from_env("PROJECT", defaults.get("project")),
3435
)
3536

37+
def __str__(self):
38+
return inspect.cleandoc(f"""
39+
Organization slug: {self.organization_slug or '-'}
40+
Project slug: {self.project_slug or '-'}
41+
Auth token: {self.token or '-'}
42+
""")
43+
3644

3745
def _from_env(name, default=None, prefix="TASKBADGER_"):
3846
return os.environ.get(f"{prefix}{name}", default)

tests/__init__.py

Whitespace-only changes.

tests/test_cli_config.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import os
2+
from pathlib import Path
3+
from unittest import mock
4+
5+
import pytest
6+
from typer.testing import CliRunner
7+
8+
from taskbadger.cli import app
9+
from taskbadger.config import write_config, Config
10+
11+
runner = CliRunner()
12+
13+
14+
@pytest.fixture
15+
def test_config():
16+
app_dir = Path(__file__).parent / "_test_config"
17+
with mock.patch("taskbadger.config._get_config_path", return_value=app_dir):
18+
config = Config(organization_slug="test_org", project_slug="test_project", token="test_token")
19+
write_config(config)
20+
yield
21+
os.remove(app_dir)
22+
23+
24+
def test_info_blank():
25+
result = runner.invoke(app, ["info"])
26+
_check_output(result, "-", "-", "-")
27+
28+
29+
def test_info_args():
30+
result = runner.invoke(app, ["-o", "org1", "-p", "project1", "info"])
31+
_check_output(result, "org1", "project1", "-")
32+
33+
34+
@mock.patch.dict(os.environ, {
35+
"TASKBADGER_ORG": "org2",
36+
"TASKBADGER_PROJECT": "project2",
37+
"TASKBADGER_TOKEN": "123",
38+
}, clear=True)
39+
def test_info_env():
40+
result = runner.invoke(app, ["info"])
41+
_check_output(result, "org2", "project2", "123")
42+
43+
44+
@mock.patch.dict(os.environ, {
45+
"TASKBADGER_ORG": "org2",
46+
"TASKBADGER_PROJECT": "project2",
47+
}, clear=True)
48+
def test_info_args_trump_env():
49+
result = runner.invoke(app, ["-o", "org1", "-p", "project1", "info"])
50+
_check_output(result, "org1", "project1", "-")
51+
52+
53+
def test_info_config(test_config):
54+
result = runner.invoke(app, ["info"])
55+
_check_output(result, "test_org", "test_project", "test_token")
56+
57+
58+
@mock.patch.dict(os.environ, {
59+
"TASKBADGER_ORG": "org2",
60+
"TASKBADGER_PROJECT": "project2",
61+
"TASKBADGER_TOKEN": "token2",
62+
}, clear=True)
63+
def test_info_config_env(test_config):
64+
result = runner.invoke(app, ["info"])
65+
_check_output(result, "org2", "project2", "token2")
66+
67+
68+
def test_info_config_args(test_config):
69+
result = runner.invoke(app, ["-o", "org1", "-p", "project1", "info"])
70+
_check_output(result, "org1", "project1", "test_token")
71+
72+
73+
@mock.patch.dict(os.environ, {
74+
"TASKBADGER_ORG": "org2",
75+
"TASKBADGER_PROJECT": "project2",
76+
"TASKBADGER_TOKEN": "token2",
77+
}, clear=True)
78+
def test_info_config_env_args(test_config):
79+
result = runner.invoke(app, ["-o", "org1", "-p", "project1", "info"])
80+
_check_output(result, "org1", "project1", "token2")
81+
82+
83+
def _check_output(result, org, project, token):
84+
assert result.exit_code == 0
85+
assert f"Organization slug: {org}" in result.stdout
86+
assert f"Project slug: {project}" in result.stdout
87+
assert f"Auth token: {token}" in result.stdout

0 commit comments

Comments
 (0)