-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cli.py
More file actions
621 lines (499 loc) · 21.5 KB
/
test_cli.py
File metadata and controls
621 lines (499 loc) · 21.5 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
import shutil
from pathlib import Path
from click.testing import CliRunner
from mcli.commands.admin.new_command import entry as new_command_entry
from mcli.commands.admin.rm_command import entry as rm_command_entry
from mcli.loader import RootCommand, discover_specs
from mcli.utils.metadata import Metadata
def _write_plugin(
base: Path,
command: str,
subcommand: str,
short_help: str = "Help",
output: str = "ok",
**kwargs: bool | None,
) -> None:
# Extract optional parameters from kwargs
hidden = kwargs.get("hidden", False)
enabled = kwargs.get("enabled", True)
command_hidden = kwargs.get("command_hidden")
command_enabled = kwargs.get("command_enabled")
target = base / command / subcommand
target.mkdir(parents=True, exist_ok=True)
(target / "entry.py").write_text(
f"import click\n\n@click.command()\ndef cli():\n click.echo({output!r})\n",
encoding="utf-8",
)
(target / "meta.yaml").write_text(
f"shortHelp: {short_help}\nhidden: {str(hidden).lower()}\nenabled: {str(enabled).lower()}\n",
encoding="utf-8",
)
# Optionally write command-level meta.yaml
if command_hidden is not None or command_enabled is not None:
cmd_hidden = command_hidden if command_hidden is not None else False
cmd_enabled = command_enabled if command_enabled is not None else True
group_meta_path = base / command / "meta.yaml"
if not group_meta_path.exists():
group_meta_path.write_text(
f"shortHelp: {command} commands\nhidden: {str(cmd_hidden).lower()}\nenabled: {str(cmd_enabled).lower()}\n",
encoding="utf-8",
)
def test_discovery_valid_plugins(tmp_path: Path) -> None:
commands_dir = tmp_path / "commands"
_write_plugin(commands_dir, "alpha", "bravo", "Alpha Bravo")
specs = discover_specs(commands_dir)
assert "alpha" in specs
assert "bravo" in specs["alpha"].subcommands
def test_root_help_includes_dev_and_dynamic(tmp_path: Path) -> None:
commands_dir = tmp_path / "commands"
_write_plugin(commands_dir, "compute", "add", "Add two integers.")
(commands_dir / "compute" / "meta.yaml").write_text(
"shortHelp: Compute related utilities.\nhidden: false\nenabled: true\n",
encoding="utf-8",
)
root = RootCommand(commands_dir)
runner = CliRunner()
result = runner.invoke(root, ["--help"])
assert result.exit_code == 0
# All commands appear in the Commands section.
assert "Commands:\n" in result.output
assert " compute" in result.output
assert "Compute related utilities." in result.output
def test_root_merges_builtin_and_user_commands(tmp_path: Path) -> None:
commands_dir = tmp_path / "commands"
user_commands_dir = tmp_path / ".mcli" / "commands"
_write_plugin(commands_dir, "compute", "add", "Add two integers.", output="built-in")
_write_plugin(user_commands_dir, "ops", "mul", "Multiply two integers.", output="user")
root = RootCommand(commands_dir, extra_commands_dirs=[user_commands_dir])
runner = CliRunner()
help_result = runner.invoke(root, ["--help"])
assert help_result.exit_code == 0
assert "compute" in help_result.output
assert "ops" in help_result.output
builtin_result = runner.invoke(root, ["compute", "add"])
assert builtin_result.exit_code == 0
assert builtin_result.output.strip() == "built-in"
user_result = runner.invoke(root, ["ops", "mul"])
assert user_result.exit_code == 0
assert user_result.output.strip() == "user"
def test_user_commands_extend_existing_groups_and_override_conflicts(tmp_path: Path) -> None:
commands_dir = tmp_path / "commands"
user_commands_dir = tmp_path / ".mcli" / "commands"
_write_plugin(commands_dir, "compute", "add", "Add two integers.", output="built-in add")
_write_plugin(user_commands_dir, "compute", "mul", "Multiply two integers.", output="user mul")
_write_plugin(user_commands_dir, "compute", "add", "Override add.", output="user add")
root = RootCommand(commands_dir, extra_commands_dirs=[user_commands_dir])
runner = CliRunner()
help_result = runner.invoke(root, ["compute", "--help"])
assert help_result.exit_code == 0
assert "add" in help_result.output
assert "mul" in help_result.output
add_result = runner.invoke(root, ["compute", "add"])
assert add_result.exit_code == 0
assert add_result.output.strip() == "user add"
mul_result = runner.invoke(root, ["compute", "mul"])
assert mul_result.exit_code == 0
assert mul_result.output.strip() == "user mul"
def test_hidden_command_not_shown_in_help(tmp_path: Path) -> None:
commands_dir = tmp_path / "commands"
_write_plugin(commands_dir, "visible", "cmd", "Visible command")
_write_plugin(commands_dir, "hidden", "cmd", "Hidden command", command_hidden=True)
root = RootCommand(commands_dir)
runner = CliRunner()
result = runner.invoke(root, ["--help"])
assert result.exit_code == 0
assert "visible" in result.output
assert "hidden" not in result.output
# But hidden command still works
result_hidden = runner.invoke(root, ["hidden", "cmd"])
assert result_hidden.exit_code == 0
assert "ok" in result_hidden.output
def test_disabled_command_is_hidden_and_fails(tmp_path: Path) -> None:
commands_dir = tmp_path / "commands"
_write_plugin(commands_dir, "working", "cmd", "Working command")
_write_plugin(commands_dir, "disabled", "cmd", "Disabled command", command_enabled=False)
root = RootCommand(commands_dir)
runner = CliRunner()
result = runner.invoke(root, ["--help"])
assert result.exit_code == 0
assert "working" in result.output
assert "disabled" not in result.output
# Disabled command should fail (Click returns exit code 2 for "no such command")
result_disabled = runner.invoke(root, ["disabled", "cmd"])
assert result_disabled.exit_code != 0
# Group is disabled so command won't be found
def test_hidden_subcommand_not_shown_but_works(tmp_path: Path) -> None:
commands_dir = tmp_path / "commands"
_write_plugin(commands_dir, "tools", "visible", "Visible subcommand")
_write_plugin(commands_dir, "tools", "hidden", "Hidden subcommand", hidden=True)
root = RootCommand(commands_dir)
runner = CliRunner()
result = runner.invoke(root, ["tools", "--help"])
assert result.exit_code == 0
assert "visible" in result.output
assert "hidden" not in result.output
# But hidden subcommand still works
result_hidden = runner.invoke(root, ["tools", "hidden"])
assert result_hidden.exit_code == 0
assert "ok" in result_hidden.output
def test_disabled_subcommand_fails_execution(tmp_path: Path) -> None:
commands_dir = tmp_path / "commands"
_write_plugin(commands_dir, "tools", "working", "Working subcommand")
_write_plugin(commands_dir, "tools", "disabled", "Disabled subcommand", enabled=False)
root = RootCommand(commands_dir)
runner = CliRunner()
# Disabled subcommand should not be shown
result = runner.invoke(root, ["tools", "--help"])
assert result.exit_code == 0
assert "working" in result.output
assert "disabled" not in result.output
# And should fail when executed
result_disabled = runner.invoke(root, ["tools", "disabled"])
assert result_disabled.exit_code == 1
assert "disabled" in result_disabled.output.lower()
def test_no_args_is_help_is_configurable_per_group_level(tmp_path: Path) -> None:
commands_dir = tmp_path / "commands"
tools_dir = commands_dir / "tools"
(tools_dir / "admin" / "show").mkdir(parents=True, exist_ok=True)
(tools_dir / "entry.py").write_text(
"import click\n\n@click.group()\ndef cli():\n pass\n",
encoding="utf-8",
)
(tools_dir / "meta.yaml").write_text(
"shortHelp: Tools commands\nhidden: false\nenabled: true\nno_args_is_help: true\n",
encoding="utf-8",
)
(tools_dir / "admin" / "entry.py").write_text(
"import click\n\n@click.group()\ndef cli():\n pass\n",
encoding="utf-8",
)
(tools_dir / "admin" / "meta.yaml").write_text(
"shortHelp: Admin tools\nhidden: false\nenabled: true\nno_args_is_help: false\n",
encoding="utf-8",
)
(tools_dir / "admin" / "show" / "entry.py").write_text(
"import click\n\n@click.command()\ndef cli():\n click.echo('ok')\n",
encoding="utf-8",
)
(tools_dir / "admin" / "show" / "meta.yaml").write_text(
"shortHelp: Show details\nhidden: false\nenabled: true\nno_args_is_help: false\n",
encoding="utf-8",
)
runner = CliRunner()
root = RootCommand(commands_dir)
top_level = runner.invoke(root, ["tools"])
assert top_level.exit_code != 0
assert "Commands:" in top_level.output
assert "admin" in top_level.output
sub_level = runner.invoke(root, ["tools", "admin"])
assert sub_level.exit_code != 0
assert "Missing command" in sub_level.output
assert "Try 'mcli tools admin --help' for help." in sub_level.output
(tools_dir / "admin" / "meta.yaml").write_text(
"shortHelp: Admin tools\nhidden: false\nenabled: true\nno_args_is_help: true\n",
encoding="utf-8",
)
refreshed_root = RootCommand(commands_dir)
refreshed_sub_level = runner.invoke(refreshed_root, ["tools", "admin"])
assert refreshed_sub_level.exit_code != 0
assert "Commands:" in refreshed_sub_level.output
assert "show" in refreshed_sub_level.output.lower()
assert "Try 'mcli tools admin --help' for help." not in refreshed_sub_level.output
def test_group_help_lists_subcommands_with_short_help() -> None:
commands_dir = Path(__file__).parent.parent / "src" / "mcli" / "commands"
root = RootCommand(commands_dir)
runner = CliRunner()
# Test that command groups show their subcommands correctly
result = runner.invoke(root, ["admin", "--help"])
assert result.exit_code == 0
assert "new-command" in result.output
assert "rm-command" in result.output
assert "rebrand" in result.output
assert "Create a new command plugin skeleton." in result.output
def test_scaffolder_creates_plugin(tmp_path: Path, monkeypatch) -> None:
commands_dir = tmp_path / "commands"
monkeypatch.setenv(Metadata.env_var("COMMANDS_DIR"), str(commands_dir))
# Copy admin command structure to test directory
real_commands = Path(__file__).parent.parent / "src" / "mcli" / "commands"
admin_src = real_commands / "admin"
admin_dst = commands_dir / "admin"
shutil.copytree(admin_src, admin_dst)
root = RootCommand(commands_dir)
runner = CliRunner()
result = runner.invoke(
root,
[
"admin",
"new-command",
"compute",
"--parent",
"math.ops",
"--short-help",
"Multiply two integers.",
],
)
assert result.exit_code == 0
assert (commands_dir / "math" / "meta.yaml").exists()
assert (commands_dir / "math" / "entry.py").exists()
assert (commands_dir / "math" / "ops" / "meta.yaml").exists()
assert (commands_dir / "math" / "ops" / "entry.py").exists()
assert (commands_dir / "math" / "ops" / "compute" / "entry.py").exists()
assert (commands_dir / "math" / "ops" / "compute" / "meta.yaml").exists()
template_dir = commands_dir / "admin" / "new_command" / ".template"
expected_entry = (
(template_dir / "entry.py")
.read_text(encoding="utf-8")
.replace(
"{{COMMAND_NAME}}",
"compute",
)
)
expected_meta = (
(template_dir / "meta.yaml")
.read_text(encoding="utf-8")
.replace(
"{{SHORT_HELP}}",
"Multiply two integers.",
)
.replace(
"{{COMMAND_NAME}}",
"compute",
)
)
generated_entry = (commands_dir / "math" / "ops" / "compute" / "entry.py").read_text(encoding="utf-8")
generated_meta = (commands_dir / "math" / "ops" / "compute" / "meta.yaml").read_text(encoding="utf-8")
assert generated_entry == expected_entry
assert generated_meta == expected_meta
def test_admin_new_command_defaults_to_user_commands_dir(tmp_path: Path, monkeypatch) -> None:
user_commands_dir = tmp_path / ".mcli" / "commands"
monkeypatch.delenv(Metadata.env_var("COMMANDS_DIR"), raising=False)
monkeypatch.setattr(new_command_entry.Metadata, "USER_COMMANDS_DIR", user_commands_dir)
assert new_command_entry._commands_dir() == user_commands_dir
def test_admin_new_command_user_flag_uses_user_commands_dir(tmp_path: Path, monkeypatch) -> None:
commands_dir = tmp_path / "commands"
user_commands_dir = tmp_path / ".mcli" / "commands"
monkeypatch.setenv(Metadata.env_var("COMMANDS_DIR"), str(commands_dir))
monkeypatch.setattr(new_command_entry.Metadata, "USER_COMMANDS_DIR", user_commands_dir)
real_commands = Path(__file__).parent.parent / "src" / "mcli" / "commands"
admin_src = real_commands / "admin"
admin_dst = commands_dir / "admin"
shutil.copytree(admin_src, admin_dst)
root = RootCommand(commands_dir, extra_commands_dirs=[user_commands_dir])
runner = CliRunner()
result = runner.invoke(
root,
[
"admin",
"new-command",
"compute",
"--short-help",
"Compute command.",
"--user",
],
)
assert result.exit_code == 0
assert (user_commands_dir / "compute" / "entry.py").exists()
assert (user_commands_dir / "compute" / "meta.yaml").exists()
assert not (commands_dir / "compute").exists()
def test_admin_rm_command_deletes_scaffolded_plugin(tmp_path: Path, monkeypatch) -> None:
commands_dir = tmp_path / "commands"
monkeypatch.setenv(Metadata.env_var("COMMANDS_DIR"), str(commands_dir))
real_commands = Path(__file__).parent.parent / "src" / "mcli" / "commands"
admin_src = real_commands / "admin"
admin_dst = commands_dir / "admin"
shutil.copytree(admin_src, admin_dst)
root = RootCommand(commands_dir)
runner = CliRunner()
create = runner.invoke(
root,
[
"admin",
"new-command",
"compute",
"--parent",
"math.ops",
"--short-help",
"Compute command.",
],
)
assert create.exit_code == 0
target_dir = commands_dir / "math" / "ops" / "compute"
result = runner.invoke(root, ["admin", "rm-command", "compute", "--parent", "math.ops", "--confirm"])
assert result.exit_code == 0
assert str(target_dir) in result.output
assert not target_dir.exists()
def test_admin_rm_command_user_flag_deletes_user_plugin(tmp_path: Path, monkeypatch) -> None:
commands_dir = tmp_path / "commands"
user_commands_dir = tmp_path / ".mcli" / "commands"
monkeypatch.setenv(Metadata.env_var("COMMANDS_DIR"), str(commands_dir))
monkeypatch.setattr(new_command_entry.Metadata, "USER_COMMANDS_DIR", user_commands_dir)
real_commands = Path(__file__).parent.parent / "src" / "mcli" / "commands"
admin_src = real_commands / "admin"
admin_dst = commands_dir / "admin"
shutil.copytree(admin_src, admin_dst)
root = RootCommand(commands_dir, extra_commands_dirs=[user_commands_dir])
runner = CliRunner()
create = runner.invoke(
root,
[
"admin",
"new-command",
"compute",
"--parent",
"ops",
"--short-help",
"Compute command.",
"--user",
],
)
assert create.exit_code == 0
target_dir = user_commands_dir / "ops" / "compute"
result = runner.invoke(root, ["admin", "rm-command", "compute", "--parent", "ops", "--user", "--confirm"])
assert result.exit_code == 0
assert str(target_dir) in result.output
assert not target_dir.exists()
def test_admin_rm_command_deletes_root_command_without_parent(tmp_path: Path, monkeypatch) -> None:
commands_dir = tmp_path / "commands"
user_commands_dir = tmp_path / ".mcli" / "commands"
monkeypatch.delenv(Metadata.env_var("COMMANDS_DIR"), raising=False)
monkeypatch.setattr(rm_command_entry.Metadata, "COMMANDS_DIR", commands_dir)
monkeypatch.setattr(rm_command_entry.Metadata, "USER_COMMANDS_DIR", user_commands_dir)
real_commands = Path(__file__).parent.parent / "src" / "mcli" / "commands"
admin_src = real_commands / "admin"
admin_dst = commands_dir / "admin"
shutil.copytree(admin_src, admin_dst)
target_dir = commands_dir / "compute"
target_dir.mkdir(parents=True)
(target_dir / "entry.py").write_text(
"import click\n\n@click.command()\ndef cli():\n click.echo('ok')\n",
encoding="utf-8",
)
(target_dir / "meta.yaml").write_text("short_help: Compute command.\n", encoding="utf-8")
root = RootCommand(commands_dir)
runner = CliRunner()
result = runner.invoke(root, ["admin", "rm-command", "compute", "--confirm"])
assert result.exit_code == 0
assert str(target_dir) in result.output
assert not target_dir.exists()
def test_admin_rm_command_requires_confirmation_without_flag(tmp_path: Path, monkeypatch) -> None:
commands_dir = tmp_path / "commands"
user_commands_dir = tmp_path / ".mcli" / "commands"
monkeypatch.delenv(Metadata.env_var("COMMANDS_DIR"), raising=False)
monkeypatch.setattr(rm_command_entry.Metadata, "COMMANDS_DIR", commands_dir)
monkeypatch.setattr(rm_command_entry.Metadata, "USER_COMMANDS_DIR", user_commands_dir)
real_commands = Path(__file__).parent.parent / "src" / "mcli" / "commands"
admin_src = real_commands / "admin"
admin_dst = commands_dir / "admin"
shutil.copytree(admin_src, admin_dst)
target_dir = commands_dir / "compute"
target_dir.mkdir(parents=True)
(target_dir / "entry.py").write_text(
"import click\n\n@click.command()\ndef cli():\n click.echo('ok')\n",
encoding="utf-8",
)
(target_dir / "meta.yaml").write_text("short_help: Compute command.\n", encoding="utf-8")
root = RootCommand(commands_dir)
runner = CliRunner()
result = runner.invoke(root, ["admin", "rm-command", "compute"], input="n\n")
assert result.exit_code != 0
assert "Delete command 'compute'" in result.output
assert target_dir.exists()
def test_scaffolder_upgrades_parent_command_to_group_when_child_is_added(tmp_path: Path, monkeypatch) -> None:
commands_dir = tmp_path / "commands"
monkeypatch.setenv(Metadata.env_var("COMMANDS_DIR"), str(commands_dir))
# Copy admin command structure to test directory
real_commands = Path(__file__).parent.parent / "src" / "mcli" / "commands"
admin_src = real_commands / "admin"
admin_dst = commands_dir / "admin"
shutil.copytree(admin_src, admin_dst)
root = RootCommand(commands_dir)
runner = CliRunner()
create_parent = runner.invoke(
root,
[
"admin",
"new-command",
"cicd",
"--short-help",
"CI/CD related commands.",
],
)
assert create_parent.exit_code == 0
parent_entry = commands_dir / "cicd" / "entry.py"
assert "@click.command()" in parent_entry.read_text(encoding="utf-8")
create_child = runner.invoke(
root,
[
"admin",
"new-command",
"create-stack",
"--parent",
"cicd",
"--short-help",
"Create a new stack",
],
)
assert create_child.exit_code == 0
parent_entry_text = parent_entry.read_text(encoding="utf-8")
assert "@click.group()" in parent_entry_text
refreshed_root = RootCommand(commands_dir)
run_child = runner.invoke(refreshed_root, ["cicd", "create-stack"])
assert run_child.exit_code == 0
assert "Usage:" in run_child.output
assert "Create a new stack" in run_child.output
def test_scaffolder_upgrades_intermediate_parent_for_dot_parent_chain(tmp_path: Path, monkeypatch) -> None:
commands_dir = tmp_path / "commands"
monkeypatch.setenv(Metadata.env_var("COMMANDS_DIR"), str(commands_dir))
# Copy admin command structure to test directory
real_commands = Path(__file__).parent.parent / "src" / "mcli" / "commands"
admin_src = real_commands / "admin"
admin_dst = commands_dir / "admin"
shutil.copytree(admin_src, admin_dst)
root = RootCommand(commands_dir)
runner = CliRunner()
first = runner.invoke(
root,
[
"admin",
"new-command",
"cicd",
"--short-help",
"CI/CD related commands.",
],
)
assert first.exit_code == 0
second = runner.invoke(
root,
[
"admin",
"new-command",
"stack",
"--parent",
"cicd",
"--short-help",
"Stack operations.",
],
)
assert second.exit_code == 0
stack_entry = commands_dir / "cicd" / "stack" / "entry.py"
assert "@click.command()" in stack_entry.read_text(encoding="utf-8")
third = runner.invoke(
root,
[
"admin",
"new-command",
"create",
"--parent",
"cicd.stack",
"--short-help",
"Create a new stack.",
],
)
assert third.exit_code == 0
stack_entry_text = stack_entry.read_text(encoding="utf-8")
assert "@click.group()" in stack_entry_text
refreshed_root = RootCommand(commands_dir)
run_nested = runner.invoke(refreshed_root, ["cicd", "stack", "create"])
assert run_nested.exit_code == 0
assert "Usage:" in run_nested.output
assert "Create a new stack." in run_nested.output