forked from lordmauve/flake8-html
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_flake8_html.py
62 lines (51 loc) · 1.81 KB
/
test_flake8_html.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Tests for the flake8 HTML plugin."""
import codecs
import os.path
import tempfile
import shutil
import contextlib
import flake8
import pytest
from flake8.main.cli import main
def create_tmpdir(filename, content):
"""Create a temporary directory, plus a file with the given content."""
tmpdir = tempfile.mkdtemp()
pyfile = os.path.join(tmpdir, filename)
with codecs.open(pyfile, 'w', encoding='utf8') as f:
f.write(content)
return tmpdir
@pytest.fixture()
def bad_sourcedir(tmpdir_factory):
"""A fixture to create a Python file with errors."""
tmpdir = create_tmpdir('bad.py', 'import os\n')
yield tmpdir
shutil.rmtree(tmpdir)
@pytest.fixture()
def good_sourcedir(tmpdir_factory):
"""A fixture to create a Python file without errors."""
tmpdir = create_tmpdir('good.py', '"""Example of addition."""\n\n1 + 1\n')
yield tmpdir
shutil.rmtree(tmpdir)
@contextlib.contextmanager
def chdir(dest):
"""Chdir into the given directory within a context."""
cwd = os.getcwd()
os.chdir(dest)
try:
yield
finally:
os.chdir(cwd)
def test_report(bad_sourcedir, tmpdir):
"""Test that a report on a bad file creates the expected files."""
with chdir(bad_sourcedir), pytest.raises(SystemExit) as excinfo:
code = main(['--exit-zero', '--format=html', '--htmldir=%s' % tmpdir, '.'])
# earlier flake8 version were raising the SystemExit, now raise it explicitly to cover recent versions too
if flake8.__version_info__ >= (5, 0, 0):
raise SystemExit(code)
assert excinfo.value.code == 0
names = ('index.html', 'styles.css', 'bad.report.html', 'bad.source.html')
written = os.listdir(str(tmpdir))
for n in names:
assert n in written, "File %s not written" % n