Skip to content

Commit e0564f0

Browse files
committed
test write config
1 parent c19053e commit e0564f0

2 files changed

Lines changed: 33 additions & 12 deletions

File tree

taskbadger/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ def monitor(ctx: typer.Context, name: str):
3434
@app.command()
3535
def configure(ctx: typer.Context):
3636
config = ctx.meta["tb_config"]
37-
config.token = typer.prompt(f"Token", default=config.token)
3837
config.organization_slug = typer.prompt(f"Organization slug", default=config.organization_slug)
3938
config.project_slug = typer.prompt(f"Project slug", default=config.project_slug)
39+
config.token = typer.prompt(f"Token", default=config.token)
4040
path = write_config(config)
4141
print(f"Config written to [green]{path}[/green]")
4242

tests/test_cli_config.py

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from unittest import mock
44

55
import pytest
6+
import tomlkit
67
from typer.testing import CliRunner
78

89
from taskbadger.cli import app
@@ -12,13 +13,18 @@
1213

1314

1415
@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)
16+
def mock_config_location():
17+
config_path = Path(__file__).parent / "_mock_config"
18+
with mock.patch("taskbadger.config._get_config_path", return_value=config_path):
19+
yield config_path
20+
if config_path.exists():
21+
os.remove(config_path)
22+
23+
24+
@pytest.fixture
25+
def mock_config(mock_config_location):
26+
config = Config(organization_slug="test_org", project_slug="test_project", token="test_token")
27+
write_config(config)
2228

2329

2430
def test_info_blank():
@@ -50,7 +56,7 @@ def test_info_args_trump_env():
5056
_check_output(result, "org1", "project1", "-")
5157

5258

53-
def test_info_config(test_config):
59+
def test_info_config(mock_config):
5460
result = runner.invoke(app, ["info"])
5561
_check_output(result, "test_org", "test_project", "test_token")
5662

@@ -60,12 +66,12 @@ def test_info_config(test_config):
6066
"TASKBADGER_PROJECT": "project2",
6167
"TASKBADGER_TOKEN": "token2",
6268
}, clear=True)
63-
def test_info_config_env(test_config):
69+
def test_info_config_env(mock_config):
6470
result = runner.invoke(app, ["info"])
6571
_check_output(result, "org2", "project2", "token2")
6672

6773

68-
def test_info_config_args(test_config):
74+
def test_info_config_args(mock_config):
6975
result = runner.invoke(app, ["-o", "org1", "-p", "project1", "info"])
7076
_check_output(result, "org1", "project1", "test_token")
7177

@@ -75,11 +81,26 @@ def test_info_config_args(test_config):
7581
"TASKBADGER_PROJECT": "project2",
7682
"TASKBADGER_TOKEN": "token2",
7783
}, clear=True)
78-
def test_info_config_env_args(test_config):
84+
def test_info_config_env_args(mock_config):
7985
result = runner.invoke(app, ["-o", "org1", "-p", "project1", "info"])
8086
_check_output(result, "org1", "project1", "token2")
8187

8288

89+
def test_configure(mock_config_location):
90+
result = runner.invoke(app, ["configure"], input="an-org\na-project\na-token")
91+
assert result.exit_code == 0
92+
assert mock_config_location.is_file()
93+
with mock_config_location.open("rt", encoding="utf-8") as fp:
94+
raw_config = tomlkit.load(fp)
95+
config_dict = raw_config.unwrap()
96+
assert config_dict == {
97+
"defaults": {
98+
"org": "an-org",
99+
"project": "a-project",
100+
},
101+
"auth": {"token": "a-token"}
102+
}
103+
83104
def _check_output(result, org, project, token):
84105
assert result.exit_code == 0
85106
assert f"Organization slug: {org}" in result.stdout

0 commit comments

Comments
 (0)