-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtest_cli.py
More file actions
122 lines (111 loc) · 3.68 KB
/
test_cli.py
File metadata and controls
122 lines (111 loc) · 3.68 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
import mock
import pytest
from codemodder.cli import parse_args
from codemodder import __VERSION__
from .conftest import CODEMOD_NAMES
class TestParseArgs:
@mock.patch("codemodder.cli.logger.error")
def test_no_args(self, error_logger):
with pytest.raises(SystemExit) as err:
parse_args([])
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)
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)
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)
for print_call in mock_print.call_args_list:
assert print_call[0][0].startswith("pixee:python/")
assert print_call[0][0].endswith(CODEMOD_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",
]
)
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",
]
)
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",
)