Skip to content
This repository has been archived by the owner on Dec 13, 2020. It is now read-only.

Commit

Permalink
Testing explain option, more shields tweaks.
Browse files Browse the repository at this point in the history
Tweaking shields now that I'm not using rst tables.
Implemented explain option similar to pep8's.
  • Loading branch information
Robpol86 committed Apr 5, 2015
1 parent 8036d9d commit 580edd2
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 11 deletions.
8 changes: 4 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@ validating docstrings.
* Python 2.6, 2.7, PyPy, PyPy3, 3.3, and 3.4 supported on Linux and OS X.
* Python 2.7, 3.3, and 3.4 supported on Windows (both 32 and 64 bit versions of Python).

.. image:: https://img.shields.io/appveyor/ci/Robpol86/flake8-pep257.svg?style=flat-square&label=AppVeyor
.. image:: https://img.shields.io/appveyor/ci/Robpol86/flake8-pep257/master.svg?style=flat-square&label=AppVeyor%20CI
:target: https://ci.appveyor.com/project/Robpol86/flake8-pep257
:alt: Build Status Windows

.. image:: https://img.shields.io/travis/Robpol86/flake8-pep257/master.svg?style=flat-square&label=Travis%20CI
:target: https://travis-ci.org/Robpol86/flake8-pep257
:alt: Build Status

.. image:: https://img.shields.io/codecov/c/github/Robpol86/flake8-pep257/master.svg?style=flat-square
.. image:: https://img.shields.io/codecov/c/github/Robpol86/flake8-pep257/master.svg?style=flat-square&label=Codecov
:target: https://codecov.io/github/Robpol86/flake8-pep257
:alt: Coverage Status

.. image:: https://img.shields.io/pypi/v/flake8-pep257.svg?style=flat-square
.. image:: https://img.shields.io/pypi/v/flake8-pep257.svg?style=flat-square&label=Latest
:target: https://pypi.python.org/pypi/flake8-pep257/
:alt: Latest Version

.. image:: https://img.shields.io/pypi/dm/flake8-pep257.svg?style=flat-square
.. image:: https://img.shields.io/pypi/dm/flake8-pep257.svg?style=flat-square&label=PyPI%20Downloads
:target: https://pypi.python.org/pypi/flake8-pep257/
:alt: Downloads

Expand Down
11 changes: 5 additions & 6 deletions flake8_pep257.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,14 @@ def __init__(self, tree, filename):
@classmethod
def add_options(cls, parser):
"""Add options to flake8."""
options = dict((o.get_opt_string(), o) for o in pep257.get_option_parser().option_list)
option_explain = options.pop('--explain')
parser.add_options([option_explain, ])
parser.config_options.append('explain')
parser.add_option('--show-pep257', action='store_true', help='show explanation of each PEP 257 error')
parser.config_options.append('show-pep257')

@classmethod
def parse_options(cls, options):
"""Read parsed options from flake8."""
# Handle flake8 options.
cls.options['explain'] = bool(options.explain)
cls.options['explain'] = bool(options.show_pep257)
cls.options['ignore'] = options.ignore
cls.options['show-source'] = options.show_source

Expand All @@ -106,5 +104,6 @@ def run(self):
continue
lineno = error.line
offset = 0 # Column number starting from 0.
text = '{0} {1}'.format(error.code, error.message.split(': ', 1)[1])
explanation = error.explanation if pep257.Error.explain else ''
text = '{0} {1}{2}'.format(error.code, error.message.split(': ', 1)[1], explanation)
yield lineno, offset, text, Main
89 changes: 88 additions & 1 deletion tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import os
import sys
from textwrap import dedent

import flake8.main
import pytest
Expand Down Expand Up @@ -47,7 +48,7 @@ def test_ignore(tmpdir, capsys, sample_module, monkeypatch, stdin, which_cfg):
@pytest.mark.parametrize('which_cfg', ['tox.ini', 'tox.ini flake8', 'setup.cfg', '.pep257'])
@pytest.mark.parametrize('stdin', [True, False])
def test_ignore_short(tmpdir, capsys, sample_module, monkeypatch, stdin, which_cfg):
"""Test ignore setting in all supported config sources."""
"""Test broad ignore settings in all supported config sources."""
sys.argv = ['flake8', '-' if stdin else '.', '-j1']
os.chdir(str(tmpdir.ensure('project_dir', dir=True)))

Expand Down Expand Up @@ -77,3 +78,89 @@ def test_ignore_short(tmpdir, capsys, sample_module, monkeypatch, stdin, which_c
expected = expected.replace('./sample_module.py:', r'.\sample_module.py:')

assert expected == out


@pytest.mark.parametrize('which_cfg', ['tox.ini', 'tox.ini flake8', 'setup.cfg', '.pep257'])
@pytest.mark.parametrize('stdin', [True, False])
def test_explain(tmpdir, capsys, sample_module, monkeypatch, stdin, which_cfg):
"""Test explain setting in all supported config sources."""
sys.argv = ['flake8', '-' if stdin else '.', '-j1']
os.chdir(str(tmpdir.ensure('project_dir', dir=True)))

if stdin:
monkeypatch.setattr('pep8.stdin_get_value', lambda: sample_module)
else:
with open('sample_module.py', 'w') as f:
f.write(sample_module)

cfg = which_cfg.split()
section = cfg[1] if len(cfg) > 1 else 'pep257'
with open(cfg[0], 'w') as f:
f.write('[{0}]\n{1} = True\n'.format(section, 'show-pep257' if section == 'flake8' else 'explain'))

with pytest.raises(SystemExit):
flake8.main.main()
out, err = capsys.readouterr()
assert not err

expected = dedent("""\
./sample_module.py:1:1: D100 Missing docstring in public module
All modules should normally have docstrings. [...] all functions and
classes exported by a module should also have docstrings. Public
methods (including the __init__ constructor) should also have
docstrings.
Note: Public (exported) definitions are either those with names listed
in __all__ variable (if present), or those that do not start
with a single underscore.
./sample_module.py:5:1: D300 Use \"\"\"triple double quotes\"\"\" (found \'\'\'-quotes)
For consistency, always use \"\"\"triple double quotes\"\"\" around
docstrings. Use r\"\"\"raw triple double quotes\"\"\" if you use any
backslashes in your docstrings. For Unicode docstrings, use
u\"\"\"Unicode triple-quoted strings\"\"\".
Note: Exception to this is made if the docstring contains
\"\"\" quotes in its body.
./sample_module.py:5:1: D401 First line should be in imperative mood (\'Print\', not \'Prints\')
[Docstring] prescribes the function or method's effect as a command:
("Do this", "Return that"), not as a description; e.g. don't write
"Returns the pathname ...".
./sample_module.py:14:1: D203 1 blank line required before class docstring (found 0)
Insert a blank line before and after all docstrings (one-line or
multi-line) that document a class -- generally speaking, the class's
methods are separated from each other by a single blank line, and the
docstring needs to be offset from the first method by a blank line;
for symmetry, put a blank line between the class header and the
docstring.
./sample_module.py:14:1: D204 1 blank line required after class docstring (found 0)
Insert a blank line before and after all docstrings (one-line or
multi-line) that document a class -- generally speaking, the class's
methods are separated from each other by a single blank line, and the
docstring needs to be offset from the first method by a blank line;
for symmetry, put a blank line between the class header and the
docstring.
./sample_module.py:14:1: D300 Use \"\"\"triple double quotes\"\"\" (found \'\'\'-quotes)
For consistency, always use \"\"\"triple double quotes\"\"\" around
docstrings. Use r\"\"\"raw triple double quotes\"\"\" if you use any
backslashes in your docstrings. For Unicode docstrings, use
u\"\"\"Unicode triple-quoted strings\"\"\".
Note: Exception to this is made if the docstring contains
\"\"\" quotes in its body.
""")
if stdin:
expected = expected.replace('./sample_module.py:', 'stdin:')
elif os.name == 'nt':
expected = expected.replace('./sample_module.py:', r'.\sample_module.py:')

assert ''.join(l.rstrip() for l in expected.splitlines()) == ''.join(l.rstrip() for l in out.splitlines())

0 comments on commit 580edd2

Please sign in to comment.