-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtest_cli.py
More file actions
142 lines (127 loc) · 4.32 KB
/
test_cli.py
File metadata and controls
142 lines (127 loc) · 4.32 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
import mock
import pytest
from codemodder.cli import parse_args
from codemodder import __version__
from codemodder.registry import load_registered_codemods
class TestParseArgs:
@classmethod
def setup_class(cls):
cls.registry = load_registered_codemods()
@mock.patch("codemodder.cli.logger.error")
def test_no_args(self, error_logger, mocker):
with pytest.raises(SystemExit) as err:
parse_args([], mocker.MagicMock())
assert err.value.args[0] == 3
error_logger.assert_called()
assert error_logger.call_args_list[0][0] == (
"CLI error: %s",
"the following arguments are required: directory, --output",
)
@pytest.mark.parametrize(
"cli_args",
[
["--help"],
[
"tests/samples/",
"--output",
"here.txt",
"--codemod-include=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"],
[
"tests/samples/",
"--output",
"here.txt",
"--codemod-include=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"],
[
"tests/samples/",
"--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("codemodder.cli.logger.error")
def test_bad_output_format(self, error_logger):
with pytest.raises(SystemExit) as err:
parse_args(
[
"tests/samples/",
"--output",
"here.txt",
"--output-format",
"hello",
],
self.registry,
)
assert err.value.args[0] == 3
error_logger.assert_called()
assert error_logger.call_args_list[0][0] == (
"CLI error: %s",
"argument --output-format: invalid choice: 'hello' (choose from 'codetf', 'diff')",
)
@mock.patch("codemodder.cli.logger.error")
def test_bad_option(self, error_logger):
with pytest.raises(SystemExit) as err:
parse_args(
[
"tests/samples/",
"--output",
"here.txt",
"--codemod=url-sandbox",
"--path-exclude",
"*request.py",
],
self.registry,
)
assert err.value.args[0] == 3
error_logger.assert_called()
assert error_logger.call_args_list[0][0] == (
"CLI error: %s",
"ambiguous option: --codemod=url-sandbox could match --codemod-exclude, --codemod-include",
)
@pytest.mark.parametrize("codemod", ["secure-random", "pixee:python/secure-random"])
def test_codemod_name_or_id(self, codemod):
parse_args(
[
"tests/samples/",
"--output",
"here.txt",
f"--codemod-include={codemod}",
],
self.registry,
)