-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathtest_config_command.py
More file actions
276 lines (232 loc) · 10.9 KB
/
test_config_command.py
File metadata and controls
276 lines (232 loc) · 10.9 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
"""Tests for the apm config command."""
import os
import sys
import tempfile
from pathlib import Path
from unittest.mock import MagicMock, patch
import pytest
from click.testing import CliRunner
from apm_cli.commands.config import config
class TestConfigShow:
"""Tests for `apm config` (show current configuration)."""
def setup_method(self):
self.runner = CliRunner()
self.original_dir = os.getcwd()
def teardown_method(self):
try:
os.chdir(self.original_dir)
except (FileNotFoundError, OSError):
pass
def test_config_show_outside_project(self):
"""Show config when not in an APM project directory."""
with tempfile.TemporaryDirectory() as tmp_dir:
os.chdir(tmp_dir)
try:
with patch("apm_cli.commands.config.get_version", return_value="1.2.3"):
result = self.runner.invoke(config, [])
finally:
os.chdir(self.original_dir)
assert result.exit_code == 0
def test_config_show_inside_project(self):
"""Show config when apm.yml is present."""
with tempfile.TemporaryDirectory() as tmp_dir:
os.chdir(tmp_dir)
try:
apm_yml = Path(tmp_dir) / "apm.yml"
apm_yml.write_text("name: myproject\nversion: '0.1'\n")
with (
patch("apm_cli.commands.config.get_version", return_value="1.2.3"),
patch(
"apm_cli.commands.config._load_apm_config",
return_value={
"name": "myproject",
"version": "0.1",
"entrypoint": "main.md",
},
),
):
result = self.runner.invoke(config, [])
finally:
os.chdir(self.original_dir)
assert result.exit_code == 0
def test_config_show_inside_project_with_compilation(self):
"""Show config when apm.yml has compilation settings."""
with tempfile.TemporaryDirectory() as tmp_dir:
os.chdir(tmp_dir)
try:
apm_yml = Path(tmp_dir) / "apm.yml"
apm_yml.write_text("name: myproject\ncompilation:\n output: AGENTS.md\n")
apm_config = {
"name": "myproject",
"version": "0.1",
"compilation": {
"output": "AGENTS.md",
"chatmode": "copilot",
"resolve_links": False,
},
"dependencies": {"mcp": ["server1", "server2"]},
}
with (
patch("apm_cli.commands.config.get_version", return_value="1.2.3"),
patch(
"apm_cli.commands.config._load_apm_config", return_value=apm_config
),
):
result = self.runner.invoke(config, [])
finally:
os.chdir(self.original_dir)
assert result.exit_code == 0
def test_config_show_rich_import_error_fallback(self):
"""Fallback plain-text display when Rich (rich.table.Table) is unavailable."""
import rich.table
mock_table_cls = MagicMock(side_effect=ImportError("no rich"))
with tempfile.TemporaryDirectory() as tmp_dir:
os.chdir(tmp_dir)
try:
with (
patch("apm_cli.commands.config.get_version", return_value="0.9.0"),
patch.object(rich.table, "Table", side_effect=ImportError("no rich")),
):
result = self.runner.invoke(config, [])
finally:
os.chdir(self.original_dir)
assert result.exit_code == 0
def test_config_show_fallback_inside_project(self):
"""Fallback display inside a project directory when console/table unavailable."""
import rich.table
with tempfile.TemporaryDirectory() as tmp_dir:
os.chdir(tmp_dir)
try:
apm_yml = Path(tmp_dir) / "apm.yml"
apm_yml.write_text("name: proj\n")
apm_config = {
"name": "proj",
"version": "1.0",
"entrypoint": None,
"dependencies": {"mcp": []},
}
with (
patch("apm_cli.commands.config.get_version", return_value="0.9.0"),
patch(
"apm_cli.commands.config._load_apm_config", return_value=apm_config
),
patch.object(rich.table, "Table", side_effect=ImportError("no rich")),
):
result = self.runner.invoke(config, [])
finally:
os.chdir(self.original_dir)
assert result.exit_code == 0
class TestConfigSet:
"""Tests for `apm config set <key> <value>`."""
def setup_method(self):
self.runner = CliRunner()
def test_set_auto_integrate_true(self):
"""Enable auto-integration."""
with patch("apm_cli.config.set_auto_integrate") as mock_set:
result = self.runner.invoke(config, ["set", "auto-integrate", "true"])
assert result.exit_code == 0
mock_set.assert_called_once_with(True)
def test_set_auto_integrate_yes(self):
"""Enable auto-integration with 'yes' alias."""
with patch("apm_cli.config.set_auto_integrate") as mock_set:
result = self.runner.invoke(config, ["set", "auto-integrate", "yes"])
assert result.exit_code == 0
mock_set.assert_called_once_with(True)
def test_set_auto_integrate_one(self):
"""Enable auto-integration with '1' alias."""
with patch("apm_cli.config.set_auto_integrate") as mock_set:
result = self.runner.invoke(config, ["set", "auto-integrate", "1"])
assert result.exit_code == 0
mock_set.assert_called_once_with(True)
def test_set_auto_integrate_false(self):
"""Disable auto-integration."""
with patch("apm_cli.config.set_auto_integrate") as mock_set:
result = self.runner.invoke(config, ["set", "auto-integrate", "false"])
assert result.exit_code == 0
mock_set.assert_called_once_with(False)
def test_set_auto_integrate_no(self):
"""Disable auto-integration with 'no' alias."""
with patch("apm_cli.config.set_auto_integrate") as mock_set:
result = self.runner.invoke(config, ["set", "auto-integrate", "no"])
assert result.exit_code == 0
mock_set.assert_called_once_with(False)
def test_set_auto_integrate_zero(self):
"""Disable auto-integration with '0' alias."""
with patch("apm_cli.config.set_auto_integrate") as mock_set:
result = self.runner.invoke(config, ["set", "auto-integrate", "0"])
assert result.exit_code == 0
mock_set.assert_called_once_with(False)
def test_set_auto_integrate_invalid_value(self):
"""Reject an invalid value for auto-integrate."""
result = self.runner.invoke(config, ["set", "auto-integrate", "maybe"])
assert result.exit_code == 1
def test_set_unknown_key(self):
"""Reject an unknown configuration key."""
result = self.runner.invoke(config, ["set", "nonexistent", "value"])
assert result.exit_code == 1
def test_set_auto_integrate_case_insensitive(self):
"""Value comparison is case-insensitive."""
with patch("apm_cli.config.set_auto_integrate") as mock_set:
result = self.runner.invoke(config, ["set", "auto-integrate", "TRUE"])
assert result.exit_code == 0
mock_set.assert_called_once_with(True)
class TestConfigGet:
"""Tests for `apm config get [key]`."""
def setup_method(self):
self.runner = CliRunner()
def test_get_auto_integrate(self):
"""Get the auto-integrate setting."""
with patch("apm_cli.config.get_auto_integrate", return_value=True):
result = self.runner.invoke(config, ["get", "auto-integrate"])
assert result.exit_code == 0
assert "auto-integrate: True" in result.output
def test_get_auto_integrate_disabled(self):
"""Get auto-integrate when disabled."""
with patch("apm_cli.config.get_auto_integrate", return_value=False):
result = self.runner.invoke(config, ["get", "auto-integrate"])
assert result.exit_code == 0
assert "auto-integrate: False" in result.output
def test_get_unknown_key(self):
"""Reject an unknown key."""
result = self.runner.invoke(config, ["get", "nonexistent"])
assert result.exit_code == 1
def test_get_all_config(self):
"""Show all config when no key is provided."""
with patch("apm_cli.config.get_auto_integrate", return_value=True):
result = self.runner.invoke(config, ["get"])
assert result.exit_code == 0
assert "auto-integrate: True" in result.output
# Internal keys must not appear - users cannot set them via apm config set
assert "default_client" not in result.output
def test_get_all_config_fresh_install(self):
"""auto-integrate is shown even on a fresh install with no key in the file."""
with patch("apm_cli.config.get_auto_integrate", return_value=True):
result = self.runner.invoke(config, ["get"])
assert result.exit_code == 0
assert "auto-integrate: True" in result.output
class TestAutoIntegrateFunctions:
"""Tests for get_auto_integrate and set_auto_integrate in apm_cli.config."""
def test_get_auto_integrate_default(self):
"""Default value is True when not set."""
import apm_cli.config as cfg_module
with patch.object(cfg_module, "get_config", return_value={}):
assert cfg_module.get_auto_integrate() is True
def test_get_auto_integrate_false(self):
"""Returns False when set to False."""
import apm_cli.config as cfg_module
with patch.object(
cfg_module, "get_config", return_value={"auto_integrate": False}
):
assert cfg_module.get_auto_integrate() is False
def test_set_auto_integrate_calls_update_config(self):
"""set_auto_integrate delegates to update_config."""
import apm_cli.config as cfg_module
with patch.object(cfg_module, "update_config") as mock_update:
cfg_module.set_auto_integrate(True)
mock_update.assert_called_once_with({"auto_integrate": True})
def test_set_auto_integrate_false_calls_update_config(self):
"""set_auto_integrate(False) passes False to update_config."""
import apm_cli.config as cfg_module
with patch.object(cfg_module, "update_config") as mock_update:
cfg_module.set_auto_integrate(False)
mock_update.assert_called_once_with({"auto_integrate": False})