-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtest_cli.py
More file actions
155 lines (136 loc) · 4.62 KB
/
test_cli.py
File metadata and controls
155 lines (136 loc) · 4.62 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
import json
import mock
import pytest
from codemodder import __version__
from codemodder.cli import parse_args
from codemodder.registry import DEFAULT_EXCLUDED_CODEMODS, load_registered_codemods
from core_codemods import registry as core_registry
class TestParseArgs:
@classmethod
def setup_class(cls):
cls.registry = load_registered_codemods()
def test_no_args(self, mocker, caplog):
with pytest.raises(SystemExit) as err:
parse_args([], mocker.MagicMock())
assert err.value.args[0] == 3
assert (
"CLI error: the following arguments are required: directory"
in caplog.messages
)
@pytest.mark.parametrize(
"cli_args",
[
["--help"],
[
"some/path",
"--output",
"here.txt",
"--codemod-include=pixee:python/url-sandbox",
"--help",
],
],
)
@mock.patch("argparse.ArgumentParser.print_help")
def test_help_is_printed(self, mock_print_help, cli_args):
with pytest.raises(SystemExit) as err:
parse_args(cli_args, self.registry)
mock_print_help.assert_called()
assert err.value.args[0] == 0
@pytest.mark.parametrize(
"cli_args",
[
["--version"],
[
"some/path",
"--output",
"here.txt",
"--codemod-include=pixee:python/url-sandbox",
"--version",
],
],
)
@mock.patch("argparse.ArgumentParser._print_message")
def test_version_is_printed(self, mock_print_msg, cli_args):
with pytest.raises(SystemExit) as err:
parse_args(cli_args, self.registry)
assert mock_print_msg.call_args_list[0][0][0].strip() == __version__
assert err.value.args[0] == 0
@pytest.mark.parametrize(
"cli_args",
[
["--list"],
[
"some/path",
"--output",
"here.txt",
"--list",
],
],
)
@mock.patch("builtins.print")
def test_list_prints_codemod_metadata(self, mock_print, cli_args):
with pytest.raises(SystemExit) as err:
parse_args(cli_args, self.registry)
assert len(mock_print.call_args_list) == len(self.registry.codemods)
printed_names = [call[0][0] for call in mock_print.call_args_list]
assert sorted(self.registry.ids) == sorted(printed_names)
assert err.value.args[0] == 0
@mock.patch("builtins.print")
def test_describe_prints_codemod_metadata(self, mock_print):
with pytest.raises(SystemExit) as err:
parse_args(
["--describe"],
self.registry,
)
assert err.value.args[0] == 0
assert mock_print.call_count == 1
results = json.loads(mock_print.call_args_list[0][0][0])
assert len(results["results"]) == len(core_registry.codemods) - len(
DEFAULT_EXCLUDED_CODEMODS
)
@pytest.mark.xfail(reason="Not working in CI for some reason")
def test_bad_output_format(self, caplog):
with pytest.raises(SystemExit) as err:
parse_args(
[
"some/path",
"--output",
"here.txt",
"--output-format",
"hello",
],
self.registry,
)
assert err.value.args[0] == 3
assert (
"CLI error: argument --output-format: invalid choice: 'hello' (choose from 'codetf', 'diff')"
in caplog.messages
)
def test_bad_option(self, caplog):
with pytest.raises(SystemExit) as err:
parse_args(
[
"some/path",
"--output",
"here.txt",
"--codemod=url-sandbox",
"--path-exclude",
"*request.py",
],
self.registry,
)
assert err.value.args[0] == 3
msg = caplog.messages[0]
assert "CLI error: ambiguous option: " in msg
assert " could match --codemod-exclude, --codemod-include" in msg
@pytest.mark.parametrize("codemod", ["secure-random", "pixee:python/secure-random"])
def test_codemod_name_or_id(self, codemod):
parse_args(
[
"some/path",
"--output",
"here.txt",
f"--codemod-include={codemod}",
],
self.registry,
)