forked from life4/flakehell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cli.py
More file actions
196 lines (163 loc) · 5.83 KB
/
test_cli.py
File metadata and controls
196 lines (163 loc) · 5.83 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# built-in
import subprocess
import sys
from io import BytesIO
from unittest.mock import patch, Mock
from pathlib import Path
from textwrap import dedent
# external
import pytest
# project
from flakehell._cli import main
# app
from .utils import chdir
def test_flake8helled_file():
"""Baseline behavior, when an actual filename is passed."""
cmd = [
sys.executable,
'-c',
'import sys; from flakehell import flake8_entrypoint; sys.exit(flake8_entrypoint())',
__file__,
]
result = subprocess.run(cmd)
assert result.returncode == 0
def test_flake8helled_stdin():
"""Problematic behavior from issue #44, `-` is passed as filename, together with --stdin-display-name."""
source_file = open(__file__, 'r')
cmd = [
sys.executable,
'-c',
'import sys; from flakehell import flake8_entrypoint; sys.exit(flake8_entrypoint())',
'--stdin-display-name',
__file__,
# '-' is not an existing filename, so snapshot cannot create a hexdigest of its content
# but otherwise it's fine for flake8 which knows to read stdin instead
'-',
]
result = subprocess.run(cmd, stdin=source_file)
assert result.returncode == 0
@pytest.mark.parametrize('flag', [
'--help',
'help',
'commands',
])
def test_help(flag, capsys):
result = main([flag])
assert result == (0, '')
captured = capsys.readouterr()
assert captured.err == ''
for name in ('baseline', 'code', 'codes', 'lint', 'missed', 'plugins'):
assert name in captured.out
def test_version(capsys):
result = main(['--version'])
assert result == (0, '')
captured = capsys.readouterr()
assert captured.err == ''
assert 'FlakeHell' in captured.out
assert 'Flake8' in captured.out
def test_lint_help(capsys):
result = main(['lint', '--help'])
assert result == (0, '')
captured = capsys.readouterr()
assert captured.err == ''
# flake8 options
assert '-h, --help' in captured.out
assert '--builtins' in captured.out
assert '--isort-show-traceback' in captured.out
# ignored flake8 options
assert '--per-file-ignores' not in captured.out
assert '--enable-extensions' not in captured.out
# flakehell options
assert '--baseline' in captured.out
def test_exceptions(capsys, tmp_path: Path):
text = """
[tool.flakehell.plugins]
pyflakes = ["+*"]
[tool.flakehell.exceptions."tests/"]
pyflakes = ["-F401"]
"""
(tmp_path / 'pyproject.toml').write_text(dedent(text))
(tmp_path / 'example.py').write_text('import sys\na')
(tmp_path / 'tests').mkdir()
(tmp_path / 'tests' / 'test_example.py').write_text('import sys\na')
with chdir(tmp_path):
result = main(['lint', '--format', 'default'])
assert result == (1, '')
captured = capsys.readouterr()
assert captured.err == ''
exp = """
./example.py:1:1: F401 'sys' imported but unused
./example.py:2:1: F821 undefined name 'a'
./tests/test_example.py:2:1: F821 undefined name 'a'
"""
assert captured.out.strip() == dedent(exp).strip()
@patch('sys.stdin.buffer', BytesIO(b'import sys\na\n'))
@patch('sys.stdin', Mock())
def test_exceptions_stdin(capsys, tmp_path: Path):
# write config
text = """
[tool.flakehell.plugins]
pyflakes = ["+*"]
[tool.flakehell.exceptions."example.py"]
pyflakes = ["-F401"]
"""
(tmp_path / 'pyproject.toml').write_text(dedent(text))
# make fake `stdin` with source file content
# code_path = tmp_path / 'example.py'
# code_path.write_text('import sys\na')
# call the command with passing matching `--stdin-display-name`
cmd = ['lint', '--format', 'default', '--stdin-display-name', 'example.py', '--', '-']
with chdir(tmp_path):
# with code_path.open('r') as stream:
result = main(cmd)
assert result == (1, '')
captured = capsys.readouterr()
assert captured.err == ''
assert captured.out.strip() == "example.py:2:1: F821 undefined name 'a'"
def test_baseline(capsys, tmp_path: Path):
code_path = tmp_path / 'example.py'
code_path.write_text('a\nb\n')
with chdir(tmp_path):
result = main(['baseline', str(code_path)])
assert result == (0, '')
captured = capsys.readouterr()
assert captured.err == ''
hashes = captured.out.strip().split()
assert len(hashes) == 2
line_path = tmp_path / 'baseline.txt'
line_path.write_text(hashes[0])
with chdir(tmp_path):
result = main([
'lint',
'--baseline', str(line_path),
'--format', 'default',
str(code_path),
])
assert result == (1, '')
captured = capsys.readouterr()
assert captured.err == ''
assert captured.out.strip() == "{}:2:1: F821 undefined name 'b'".format(str(code_path))
def test_ignore_file_by_top_level_noqa(capsys, tmp_path: Path):
(tmp_path / 'example1.py').write_text('import sys\n')
(tmp_path / 'example2.py').write_text('# flake8: noqa\nimport sys\n')
with chdir(tmp_path):
result = main(['lint', '--format', 'default'])
assert result == (1, '')
captured = capsys.readouterr()
assert captured.err == ''
exp = "./example1.py:1:1: F401 'sys' imported but unused"
assert captured.out.strip() == exp
def test_exclude_file(capsys, tmp_path: Path):
(tmp_path / 'checked.py').write_text('import sys\n')
(tmp_path / 'ignored').mkdir()
(tmp_path / 'ignored' / 'first.py').write_text('import sys\n')
(tmp_path / 'ignored' / 'second.py').write_text('invalid syntax!')
with chdir(tmp_path):
result = main(['lint', '--format', 'default', '--exclude', 'ignored'])
assert result == (1, '')
captured = capsys.readouterr()
assert captured.err == ''
exp = """
./checked.py:1:1: F401 'sys' imported but unused
"""
assert captured.out.strip() == dedent(exp).strip()