-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathtest_fixer_library.py
45 lines (35 loc) · 1.59 KB
/
test_fixer_library.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
from pathlib import Path
import subprocess
import re
import sys
import pytest
basepath = Path(__file__).parent.parent / 'source'
all_rst_files = [str(p.relative_to(basepath))
for p in basepath.glob('**/*.rst')]
# Fixer names are in the format "<module>.fixes.fix_<name>" but we test also
# for typos like "<module>.fixes.ifx_<name>".
FIXER_RE = re.compile(r'\w+\.fixes\.\w+')
@pytest.fixture(scope='module')
def fixer_names():
"""Get list of available fixers"""
# use subprocess -- modernize doesn't have public fixer list API
proc = subprocess.run([sys.executable, '-m', 'modernize', '-l'],
stdout=subprocess.PIPE, encoding='ascii', check=True)
# In modernize 0.6.1, the output has changed to list additional information
# This takes the first part only and works with older modernize as well
lines = [l.strip().split()[0] for l in proc.stdout.splitlines() if l]
# first line is a header, all others should be fixers
assert not FIXER_RE.fullmatch(lines[0])
fixers = lines[1:]
assert all([FIXER_RE.fullmatch(f) for f in fixers])
return fixers
@pytest.mark.parametrize('filename', all_rst_files)
def test_fixers_exist(filename, fixer_names):
"""Test that all mentioned fixers are correctly named and available"""
path = basepath / filename
with path.open() as f:
for lineno, line in enumerate(f, start=1):
for match in FIXER_RE.finditer(line):
found_text = match.group(0)
print(f'{filename}:{lineno}:{line.strip()}')
assert found_text in fixer_names