Skip to content

Commit

Permalink
Add support for flake8 version >= 5.0.0. Fix test case to run with re…
Browse files Browse the repository at this point in the history
…cent vesions of flake8, fix summary line to show hte installed flake8 pluings
  • Loading branch information
skadyan authored and lordmauve committed Nov 30, 2022
1 parent 1bd0718 commit b935f0b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
12 changes: 11 additions & 1 deletion flake8_html/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from operator import attrgetter
from collections import namedtuple, Counter

import flake8
from pygments import highlight
from collections import defaultdict
from pygments.lexers import PythonLexer
Expand Down Expand Up @@ -278,14 +279,23 @@ def write_index(self):
key=lambda e: (e.highest_sev, -e.error_count)
),
now=datetime.datetime.now(),
versions=self.option_manager.generate_versions(),
versions=self._get_plugin_version_summary(),
highest_sev=highest_sev,
title=self.options.htmltitle,
)
indexfile = os.path.join(self.outdir, 'index.html')
with codecs.open(indexfile, 'w', encoding='utf8') as f:
f.write(rendered)

def _get_plugin_version_summary(self):
option_manager = self.option_manager
if flake8.__version_info__ < (5, 0, 0):
return option_manager.generate_versions()
else:
# Below return info prefixed with "Installed with:". This is different from
# option_manager.generate_versions() but using this as it is a cleaner way
return option_manager.parser.epilog

@classmethod
def add_options(cls, options):
"""Add a --htmldir option to the OptionsManager."""
Expand Down
6 changes: 5 additions & 1 deletion tests/test_flake8_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import shutil
import contextlib

import flake8
import pytest
from flake8.main.cli import main

Expand Down Expand Up @@ -50,7 +51,10 @@ def chdir(dest):
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:
main(['--exit-zero', '--format=html', '--htmldir=%s' % tmpdir, '.'])
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))
Expand Down

0 comments on commit b935f0b

Please sign in to comment.