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

Commit

Permalink
done
Browse files Browse the repository at this point in the history
  • Loading branch information
Robpol86 committed Nov 29, 2015
1 parent 8d07a0b commit e0084c9
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 205 deletions.
10 changes: 4 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
import pytest


@pytest.fixture(scope='session')
def tmpdir_session(request, tmpdir_factory):
"""A tmpdir fixture for the session scope. Persists throughout the pytest session.
@pytest.fixture(scope='function')
def tempdir(tmpdir):
"""A tmpdir fixture with prepared source files.
:param request: pytest fixture.
:param tmpdir_factory: pytest fixture.
:param tmpdir: pytest fixture.
"""
tmpdir = tmpdir_factory.mktemp(request.session.name)
tmpdir.join('empty').ensure_dir()
tmpdir.join('sample.py').write(dedent("""\
#!/usr/bin/env python
Expand Down
189 changes: 0 additions & 189 deletions tests/test_config.py

This file was deleted.

8 changes: 4 additions & 4 deletions tests/test_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@


@pytest.mark.parametrize('stdin', ['', 'sample_unicode.py', 'sample.py'])
def test_direct(capsys, monkeypatch, tmpdir_session, stdin):
def test_direct(capsys, monkeypatch, tempdir, stdin):
"""Test by calling flake8.main.main() using the same running python process.
:param capsys: pytest fixture.
:param monkeypatch: pytest fixture.
:param tmpdir_session: conftest fixture.
:param tempdir: conftest fixture.
:param str stdin: Pipe this file to stdin of flake8.
"""
# Prepare.
monkeypatch.chdir(tmpdir_session.join('empty' if stdin else ''))
monkeypatch.chdir(tempdir.join('empty' if stdin else ''))
monkeypatch.setattr('sys.argv', ['flake8', '-' if stdin else '.', '-j1'])
if stdin:
monkeypatch.setattr('pep8.stdin_get_value', lambda: tmpdir_session.join(stdin).read())
monkeypatch.setattr('pep8.stdin_get_value', lambda: tempdir.join(stdin).read())

# Execute.
with pytest.raises(SystemExit):
Expand Down
159 changes: 159 additions & 0 deletions tests/test_explain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
"""Test against sample modules using the explain option in all supported config sources."""

import os
import re

import flake8.main
import pytest

EXPECTED = list()
EXPECTED.append("""\
./sample.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.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.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.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.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.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.
""")
EXPECTED.append("""\
./sample_unicode.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_unicode.py:15: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_unicode.py:15: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_unicode.py:24: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_unicode.py:24: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_unicode.py:24: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.
""")


@pytest.mark.parametrize('stdin', ['', 'sample_unicode.py', 'sample.py'])
@pytest.mark.parametrize('which_cfg', ['tox.ini', 'tox.ini flake8', 'setup.cfg', '.pep257'])
def test_direct(capsys, monkeypatch, tempdir, stdin, which_cfg):
"""Test by calling flake8.main.main() using the same running python process.
:param capsys: pytest fixture.
:param monkeypatch: pytest fixture.
:param tempdir: conftest fixture.
:param str stdin: Pipe this file to stdin of flake8.
:param str which_cfg: Which config file to test with.
"""
# Prepare.
monkeypatch.chdir(tempdir.join('empty' if stdin else ''))
monkeypatch.setattr('sys.argv', ['flake8', '-' if stdin else '.', '-j1'])
if stdin:
monkeypatch.setattr('pep8.stdin_get_value', lambda: tempdir.join(stdin).read())

# Write configuration.
cfg = which_cfg.split()
section = cfg[1] if len(cfg) > 1 else 'pep257'
key = 'show-pep257' if section == 'flake8' else 'explain'
tempdir.join('empty' if stdin else '', cfg[0]).write('[{0}]\n{1} = True\n'.format(section, key))

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

if stdin:
expected = EXPECTED[0 if stdin == 'sample.py' else 1].replace('./{0}:'.format(stdin), 'stdin:')
elif os.name == 'nt':
expected = '\n\n'.join(EXPECTED).replace('./sample', r'.\sample')
else:
expected = '\n\n'.join(EXPECTED)
out = re.sub(r'\n[\t ]+\n', r'\n\n', out)

assert expected.strip() == out.strip()

0 comments on commit e0084c9

Please sign in to comment.