forked from kandji-inc/kst
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_show_cmd.py
More file actions
97 lines (68 loc) · 3.06 KB
/
test_show_cmd.py
File metadata and controls
97 lines (68 loc) · 3.06 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
import random
import pytest
from typer.testing import CliRunner
from kst import app
runner = CliRunner()
@pytest.mark.parametrize(["extra_args", "expected_return"], [pytest.param(["--help"], 0, id="--help"), pytest.param([], 2, id="no args")])
def test_help(extra_args: list[str], expected_return: int):
result = runner.invoke(app, ["script", "show", *extra_args])
# Check that the command ran successfully or returned a usage error for no
# args, respectively
assert result.exit_code == expected_return
# Check that the help message contains the expected content
assert "Usage: kst script show [OPTIONS] SCRIPT" in result.stdout
@pytest.mark.parametrize(
("by_id", "pass_remote", "by_parent"),
[
pytest.param(True, False, False, id="id-local"),
pytest.param(False, False, False, id="path-local"),
pytest.param(False, False, True, id="parent-local"),
pytest.param(True, True, False, id="id-remote"),
pytest.param(False, True, False, id="path-remote"),
pytest.param(False, True, True, id="parent-remote"),
],
)
@pytest.mark.usefixtures("kst_repo_cd")
def test_show_script(scripts_lrc, patch_scripts_endpoints, by_id: bool, pass_remote: bool, by_parent: bool):
local, remote, _ = scripts_lrc
cmd = ["script", "show"]
if pass_remote:
cmd.append("--remote")
repo = remote if pass_remote else local
# only use scripts which are in local repo since path could be passed to remote
script = random.choice([script for script in repo.values() if script.id in local])
# get the local script for script path option
local_script = local[script.id]
assert local_script.info_path is not None
cmd.append(
script.id
if by_id
else str(local_script.info_path)
if by_parent
else str(local_script.info_path.resolve().parent)
)
# sanity check for called_dict
assert all(v == 0 for v in patch_scripts_endpoints.values())
result = runner.invoke(app, cmd)
# only git should be called and only if remote is passed
assert all(v == 0 for k, v in patch_scripts_endpoints.items() if k not in {"list"})
assert patch_scripts_endpoints["list"] == (1 if pass_remote else 0)
# Check that the command ran successfully
assert result.exit_code == 0
# Check that the output contains the expected content
assert script.id in result.stdout
assert script.name in result.stdout
def test_show_from_outside_repo(scripts_lrc):
local, _, _ = scripts_lrc
script = random.choice(list(local.values()))
result = runner.invoke(app, ["script", "show", "--repo", str(local.root), script.id])
assert result.exit_code == 0
assert script.id in result.stdout
assert script.name in result.stdout
def test_show_from_outside_repo_with_path(scripts_lrc):
local, _, _ = scripts_lrc
script = random.choice(list(local.values()))
result = runner.invoke(app, ["script", "show", str(script.info_path.parent)])
assert result.exit_code == 0
assert script.id in result.stdout
assert script.name in result.stdout