/
test_pseudo_python_cli.py
93 lines (78 loc) · 2.44 KB
/
test_pseudo_python_cli.py
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
import shlex
import pytest
from julia.pseudo_python_cli import parse_args
def make_dict(**kwargs):
ns = parse_args([])
return dict(vars(ns), **kwargs)
@pytest.mark.parametrize("args, desired", [
("-m json.tool -h", make_dict(module="json.tool", args=["-h"])),
("-mjson.tool -h", make_dict(module="json.tool", args=["-h"])),
("-imjson.tool -h",
make_dict(interactive=True, module="json.tool", args=["-h"])),
("-m ipykernel install --user --name NAME --display-name DISPLAY_NAME",
make_dict(module="ipykernel",
args=shlex.split("install --user --name NAME"
" --display-name DISPLAY_NAME"))),
("-m ipykernel_launcher -f FILE",
make_dict(module="ipykernel_launcher",
args=shlex.split("-f FILE"))),
("-", make_dict(script="-")),
("- a", make_dict(script="-", args=["a"])),
("script", make_dict(script="script")),
("script a", make_dict(script="script", args=["a"])),
("script -m", make_dict(script="script", args=["-m"])),
("script -c 1", make_dict(script="script", args=["-c", "1"])),
("script -h 1", make_dict(script="script", args=["-h", "1"])),
])
def test_valid_args(args, desired):
ns = parse_args(shlex.split(args))
actual = vars(ns)
assert actual == desired
@pytest.mark.parametrize("args", [
"-m",
"-c",
"-i -m",
"-h -m",
"-V -m",
])
def test_invalid_args(args, capsys):
with pytest.raises(SystemExit) as exc_info:
parse_args(shlex.split(args))
assert exc_info.value.code != 0
captured = capsys.readouterr()
assert "usage:" in captured.err
assert not captured.out
@pytest.mark.parametrize("args", [
"-h",
"-i --help",
"-h -i",
"-hi",
"-ih",
"-Vh",
"-hV",
"-h -m json.tool",
"-h -mjson.tool",
])
def test_help_option(args, capsys):
with pytest.raises(SystemExit) as exc_info:
parse_args(shlex.split(args))
assert exc_info.value.code == 0
captured = capsys.readouterr()
assert "usage:" in captured.out
assert not captured.err
@pytest.mark.parametrize("args", [
"-V",
"--version",
"-V -i",
"-Vi",
"-iV",
"-V script",
"-V script -h",
])
def test_version_option(args, capsys):
with pytest.raises(SystemExit) as exc_info:
parse_args(shlex.split(args))
assert exc_info.value.code == 0
captured = capsys.readouterr()
assert "Python " in captured.out
assert not captured.err