Skip to content

Commit

Permalink
Allow configuration of line length
Browse files Browse the repository at this point in the history
Add the ability to configure the maximum line length, but adding a
flake8-max-line-length configuration item to the [pytest] section
of a configuration file.
  • Loading branch information
tholo committed Feb 12, 2016
1 parent 023e433 commit 243a992
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 15 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
0.2
---

- Added ability to override maximum line length

0.1
----------------------------------------------
---

- initial release
11 changes: 11 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ flake8.
Configuring FLAKE8 options per project and file
-----------------------------------------------

Maximum line length can be configured for the whole project
by adding a ``flake8-max-line-length`` option to your ``setup.cfg``
or ``tox.ini`` file like this::

# content of setup.cfg
[pytest]
flake8-max-line-length = 99

Note that the default will be what naturally comes with flake8_
(which it turn gets its default from pep8_).

You may configure flake8-checking options for your project
by adding an ``flake8-ignore`` entry to your ``setup.cfg``
or ``tox.ini`` file like this::
Expand Down
29 changes: 20 additions & 9 deletions pytest_flake8.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
"""py.test plugin to test with flake8."""

import os
import re

from flake8.engine import get_style_guide

import py

import pytest
import os
from flake8.engine import get_style_guide

__version__ = '0.1'
__version__ = '0.2'

HISTKEY = "flake8/mtimes"

Expand All @@ -22,13 +25,17 @@ def pytest_addoption(parser):
help="each line specifies a glob pattern and whitespace "
"separated FLAKE8 errors or warnings which will be ignored, "
"example: *.py W293")
parser.addini(
"flake8-max-line-length",
help="maximum line length")


def pytest_sessionstart(session):
"""Start a new session."""
config = session.config
if config.option.flake8:
config._flake8ignore = Ignorer(config.getini("flake8-ignore"))
config._flake8maxlen = config.getini("flake8-max-line-length")
config._flake8mtimes = config.cache.get(HISTKEY, {})


Expand All @@ -38,7 +45,7 @@ def pytest_collect_file(path, parent):
if config.option.flake8 and path.ext == '.py':
flake8ignore = config._flake8ignore(path)
if flake8ignore is not None:
return Flake8Item(path, parent, flake8ignore)
return Flake8Item(path, parent, flake8ignore, config._flake8maxlen)


def pytest_sessionfinish(session):
Expand All @@ -49,16 +56,16 @@ def pytest_sessionfinish(session):


class Flake8Error(Exception):

""" indicates an error during flake8 checks. """


class Flake8Item(pytest.Item, pytest.File):

def __init__(self, path, parent, flake8ignore):
def __init__(self, path, parent, flake8ignore, maxlength):
super(Flake8Item, self).__init__(path, parent)
self.add_marker("flake8")
self.flake8ignore = flake8ignore
self.maxlength = maxlength

def setup(self):
flake8mtimes = self.config._flake8mtimes
Expand All @@ -70,7 +77,7 @@ def setup(self):
def runtest(self):
call = py.io.StdCapture.call
found_errors, out, err = call(
check_file, self.fspath, self.flake8ignore)
check_file, self.fspath, self.flake8ignore, self.maxlength)
if found_errors:
raise Flake8Error(out, err)
# update mtime only if test passed
Expand Down Expand Up @@ -121,9 +128,13 @@ def __call__(self, path):
return l


def check_file(path, flake8ignore):
def check_file(path, flake8ignore, maxlength):
"""Run flake8 over a single file, and return the number of failures."""
flake8_style = get_style_guide(parse_argv=False)
if maxlength:
flake8_style = get_style_guide(
parse_argv=False, paths=['--max-line-length', maxlength])
else:
flake8_style = get_style_guide(parse_argv=False)
options = flake8_style.options

if options.install_hook:
Expand Down
12 changes: 8 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@

setup(
name='pytest-flake8',
version='0.1',
version='0.2',
description='pytest plugin to check FLAKE8 requirements',
long_description=open("README.rst").read(),
classifiers=[
"Development Status :: 3 - Alpha",
"Development Status :: 4 - Beta",
"Intended Audience :: Developers",
"License :: OSI Approved :: BSD License",
"Programming Language :: Python",
"Programming Language :: Python :: 2",
"Programming Language :: Python :: 2.6",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.4",
"Programming Language :: Python :: 3.5",
"Topic :: Software Development",
"Topic :: Software Development :: Quality Assurance",
"Topic :: Software Development :: Testing",
Expand All @@ -31,8 +35,8 @@
'pytest11': ['flake8 = pytest_flake8'],
},
install_requires=[
'flake8>=2.3',
'pytest>=2.4.2',
'flake8>=2.5',
'pytest>=2.8',
'pytest-cache',
],
)
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tox]
envlist=py27,py27-pytesttrunk,py33,py-xdist,py34
envlist=py27,py27-pytesttrunk,py33,py-xdist,py34,py35

[testenv]
deps=pytest
Expand Down

0 comments on commit 243a992

Please sign in to comment.