-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cli_config.py
More file actions
139 lines (112 loc) · 3.78 KB
/
test_cli_config.py
File metadata and controls
139 lines (112 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import os
from pathlib import Path
from unittest import mock
import pytest
import tomlkit
from typer.testing import CliRunner
from taskbadger.cli_main import app
from taskbadger.config import Config, write_config
runner = CliRunner()
@pytest.fixture(autouse=True)
def mock_config_location():
config_path = Path(__file__).parent / "_mock_config"
with mock.patch("taskbadger.config._get_config_path", return_value=config_path):
yield config_path
if config_path.exists():
os.remove(config_path)
@pytest.fixture()
def _mock_config(mock_config_location):
config = Config(
organization_slug="test_org",
project_slug="test_project",
token="test_token",
tags={"env": "prod", "host": "localhost"},
)
write_config(config)
def test_info_blank():
result = runner.invoke(app, ["info"])
_check_output(result, "-", "-", "-")
def test_info_args():
result = runner.invoke(app, ["-o", "org1", "-p", "project1", "info"])
_check_output(result, "org1", "project1", "-")
@mock.patch.dict(
os.environ,
{
"TASKBADGER_ORG": "org2",
"TASKBADGER_PROJECT": "project2",
"TASKBADGER_API_KEY": "123",
},
clear=True,
)
def test_info_env():
result = runner.invoke(app, ["info"])
_check_output(result, "org2", "project2", "123")
@mock.patch.dict(
os.environ,
{
"TASKBADGER_ORG": "org2",
"TASKBADGER_PROJECT": "project2",
},
clear=True,
)
def test_info_args_trump_env():
result = runner.invoke(app, ["-o", "org1", "-p", "project1", "info"])
_check_output(result, "org1", "project1", "-")
@pytest.mark.usefixtures("_mock_config")
def test_info_config():
result = runner.invoke(app, ["info"])
_check_output(result, "test_org", "test_project", "test_token", tags={"env": "prod", "host": "localhost"})
@mock.patch.dict(
os.environ,
{
"TASKBADGER_ORG": "org2",
"TASKBADGER_PROJECT": "project2",
"TASKBADGER_API_KEY": "token2",
},
clear=True,
)
@pytest.mark.usefixtures("_mock_config")
def test_info_config_env():
result = runner.invoke(app, ["info"])
_check_output(result, "org2", "project2", "token2", tags={"env": "prod", "host": "localhost"})
@pytest.mark.usefixtures("_mock_config")
def test_info_config_args():
result = runner.invoke(app, ["-o", "org1", "-p", "project1", "info"])
_check_output(result, "org1", "project1", "test_token", tags={"env": "prod", "host": "localhost"})
@mock.patch.dict(
os.environ,
{
"TASKBADGER_ORG": "org2",
"TASKBADGER_PROJECT": "project2",
"TASKBADGER_API_KEY": "token2",
},
clear=True,
)
@pytest.mark.usefixtures("_mock_config")
def test_info_config_env_args():
result = runner.invoke(app, ["-o", "org1", "-p", "project1", "info"])
_check_output(result, "org1", "project1", "token2", tags={"env": "prod", "host": "localhost"})
def test_configure(mock_config_location):
result = runner.invoke(app, ["configure"], input="a-token\nan-org\na-project")
assert result.exit_code == 0
assert mock_config_location.is_file()
with mock_config_location.open("rt", encoding="utf-8") as fp:
raw_config = tomlkit.load(fp)
config_dict = raw_config.unwrap()
assert config_dict == {
"defaults": {
"org": "an-org",
"project": "a-project",
},
"auth": {"token": "a-token"},
}
def _check_output(result, org, project, token, tags=None):
assert result.exit_code == 0
assert f"Organization slug: {org}" in result.stdout
assert f"Project slug: {project}" in result.stdout
assert f"Auth token: {token}" in result.stdout
if tags:
for key, value in tags.items():
assert f"{key}: {value}" in result.stdout
else:
assert "Tags:" not in result.stdout