-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli_test.py
More file actions
95 lines (68 loc) · 3.16 KB
/
cli_test.py
File metadata and controls
95 lines (68 loc) · 3.16 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
from __future__ import annotations
from pathlib import Path
from typing import NoReturn
import click
from click.testing import CliRunner
import cli as cli_mod
def test_cli_run_invokes_manifest_runner(monkeypatch) -> None:
runner = CliRunner()
called: dict[str, object] = {}
def fake_run_from_manifest_path(manifest_path: Path, *, target: str | None = None, dry_run: bool = False):
called["manifest_path"] = manifest_path
called["target"] = target
called["dry_run"] = dry_run
return {"ok": True}
monkeypatch.setattr(cli_mod, "run_from_manifest_path", fake_run_from_manifest_path)
with runner.isolated_filesystem():
p = Path("m.yml")
p.write_text("name: test\n", encoding="utf-8")
res = runner.invoke(cli_mod.cli, ["run", str(p), "--dry-run"])
assert res.exit_code == 0, res.output
manifest_path = called["manifest_path"]
assert isinstance(manifest_path, Path)
assert manifest_path.name == "m.yml"
assert called["target"] is None
assert called["dry_run"] is True
def test_cli_unknown_command_is_treated_as_run(monkeypatch) -> None:
runner = CliRunner()
called: dict[str, object] = {}
def fake_run_from_manifest_path(manifest_path: Path, *, target: str | None = None, dry_run: bool = False):
called["manifest_path"] = manifest_path
called["target"] = target
called["dry_run"] = dry_run
return {"ok": True}
monkeypatch.setattr(cli_mod, "run_from_manifest_path", fake_run_from_manifest_path)
with runner.isolated_filesystem():
p = Path("m.yml")
p.write_text("name: test\n", encoding="utf-8")
# First token is not a known subcommand => CarambaCLI should interpret it as `run <token> ...`.
res = runner.invoke(cli_mod.cli, [str(p), "--dry-run"])
assert res.exit_code == 0, res.output
manifest_path = called["manifest_path"]
assert isinstance(manifest_path, Path)
assert manifest_path.name == "m.yml"
assert called["dry_run"] is True
def test_cli_group_dry_run_option_is_inherited_by_run_command(monkeypatch) -> None:
runner = CliRunner()
called: dict[str, object] = {}
def fake_run_from_manifest_path(manifest_path: Path, *, target: str | None = None, dry_run: bool = False):
called["manifest_path"] = manifest_path
called["target"] = target
called["dry_run"] = dry_run
return {"ok": True}
monkeypatch.setattr(cli_mod, "run_from_manifest_path", fake_run_from_manifest_path)
with runner.isolated_filesystem():
p = Path("m.yml")
p.write_text("name: test\n", encoding="utf-8")
res = runner.invoke(cli_mod.cli, ["--dry-run", "run", str(p)])
assert res.exit_code == 0, res.output
assert called["dry_run"] is True
def test_cli_main_returns_zero_on_help() -> None:
# Uses click's help path; should not raise or exit the interpreter.
exit_code = cli_mod.main(["--help"])
assert exit_code == 0
def test_cli_main_returns_one_on_click_abort(monkeypatch) -> None:
def boom(*_args, **_kwargs) -> NoReturn:
raise click.Abort()
monkeypatch.setattr(cli_mod.cli, "main", boom)
assert cli_mod.main(["--help"]) == 1