-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cli.py
More file actions
127 lines (94 loc) · 2.63 KB
/
Copy pathtest_cli.py
File metadata and controls
127 lines (94 loc) · 2.63 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
# type: ignore
import os
from unittest.mock import call, MagicMock
# mock
import builtins
# test
from .cli import cli
from .utils import colors
def test_cli_should_display_help_on_help_command():
# given
argv = ['cli.py', 'help']
env = os.environ
# mock
builtins.print = MagicMock()
# when
cli(argv, env)
builtins.print.assert_called_once_with("""
Execute script through unshell runtime
Usage:
unshell COMMAND [SCRIPT_PATH] [ARGS...]
Commands:
help Print this help message
run run a script through unshell runtime
""")
def test_cli_should_display_help_if_called_with_nothing():
# given
argv = ['cli.py']
env = os.environ
# mock
builtins.print = MagicMock()
# when
cli(argv, env)
# then
builtins.print.assert_called_once()
def test_cli_should_display_error_if_called_with_no_script():
# given
argv = ['cli.py', 'run']
env = os.environ
# mock
builtins.print = MagicMock()
# when
try:
cli(argv, env)
except Exception as err:
# then
assert str(err) == f"{colors.red('✘')} unshell: Invalid script or script path"
def test_cli_should_display_help_on_invalid_command():
# given
argv = ['cli.py', 'invalid', 'foo']
env = os.environ
# mock
builtins.print = MagicMock()
# when
cli(argv, env)
# then
builtins.print.assert_called_once()
def test_cli_should_display_error_on_unresolvable_script():
# given
argv = ['cli.py', 'run', 'unresolvable']
env = os.environ
# when
try:
cli(argv, env)
except Exception as err:
# then
assert str(err) == f"{colors.red('✘')} unshell: Invalid script or script path"
def test_cli_should_display_error_on_errored_script():
# given
abs_test_path = os.path.dirname(os.path.abspath(__file__))
scriptPath = f"{abs_test_path}/../fixtures/scripts/notCompatibleCmd.py"
argv = ['cli.py', 'run', scriptPath]
env = os.environ
# when
try:
cli(argv, env)
except Exception as err:
assert f"{colors.red('✘')} unshell: something went wrong" in str(err)
def test_cli_should_execute_script_on_run_command():
# given
abs_test_path = os.path.dirname(os.path.abspath(__file__))
scriptPath = f"{abs_test_path}/../fixtures/scripts/yieldAndReturnCommand.py"
argv = ['cli.py', 'run', scriptPath]
env = os.environ
# mock
builtins.print = MagicMock()
# when
cli(argv, env)
# then
assert builtins.print.mock_calls == [
call("• echo hello"),
call("➜ hello\n"),
call("• echo world"),
call("➜ world\n"),
]