From e0084c9475208164dab55a186ad3392c81db5f4d Mon Sep 17 00:00:00 2001 From: Robpol86 Date: Sat, 28 Nov 2015 19:08:48 -0600 Subject: [PATCH] done --- tests/conftest.py | 10 +-- tests/test_config.py | 189 ------------------------------------------ tests/test_default.py | 8 +- tests/test_explain.py | 159 +++++++++++++++++++++++++++++++++++ tests/test_ignore.py | 12 +-- 5 files changed, 173 insertions(+), 205 deletions(-) delete mode 100644 tests/test_config.py create mode 100644 tests/test_explain.py diff --git a/tests/conftest.py b/tests/conftest.py index 1d7b047..6f8682a 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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 diff --git a/tests/test_config.py b/tests/test_config.py deleted file mode 100644 index 2a90772..0000000 --- a/tests/test_config.py +++ /dev/null @@ -1,189 +0,0 @@ -"""Basic tests.""" - -import os -from textwrap import dedent - -import flake8.main -import pytest - - -@pytest.mark.parametrize('which_cfg', ['tox.ini', 'tox.ini flake8', 'setup.cfg', '.pep257']) -@pytest.mark.parametrize('stdin', [True, False]) -def test_ignore(capsys, monkeypatch, tmpdir, sample_module, stdin, which_cfg): - """Test ignore setting in all supported config sources. - - :param capsys: pytest fixture. - :param monkeypatch: pytest fixture. - :param tmpdir: pytest fixture. - :param sample_module: conftest fixture. - :param bool stdin: Use stdin source instead of file. - :param str which_cfg: Which config file to test with. - """ - monkeypatch.chdir(tmpdir) - monkeypatch.setattr('sys.argv', ['flake8', '-' if stdin else '.', '-j1']) - - 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}]\nignore = D203,D204\n'.format(section)) - - with pytest.raises(SystemExit): - flake8.main.main() - out, err = capsys.readouterr() - assert not err - - expected = ( - './sample_module.py:1:1: D100 Missing docstring in public module\n' - './sample_module.py:5:1: D300 Use """triple double quotes""" (found \'\'\'-quotes)\n' - './sample_module.py:5:1: D401 First line should be in imperative mood (\'Print\', not \'Prints\')\n' - './sample_module.py:14:1: D300 Use """triple double quotes""" (found \'\'\'-quotes)\n' - ) - if stdin: - expected = expected.replace('./sample_module.py:', 'stdin:') - elif os.name == 'nt': - 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_ignore_short(capsys, monkeypatch, tmpdir, sample_module, stdin, which_cfg): - """Test broad ignore settings in all supported config sources. - - :param capsys: pytest fixture. - :param monkeypatch: pytest fixture. - :param tmpdir: pytest fixture. - :param sample_module: conftest fixture. - :param bool stdin: Use stdin source instead of file. - :param str which_cfg: Which config file to test with. - """ - monkeypatch.chdir(tmpdir) - monkeypatch.setattr('sys.argv', ['flake8', '-' if stdin else '.', '-j1']) - - 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}]\nignore = D2,D3\n'.format(section)) - - with pytest.raises(SystemExit): - flake8.main.main() - out, err = capsys.readouterr() - assert not err - - expected = ( - './sample_module.py:1:1: D100 Missing docstring in public module\n' - './sample_module.py:5:1: D401 First line should be in imperative mood (\'Print\', not \'Prints\')\n' - ) - if stdin: - expected = expected.replace('./sample_module.py:', 'stdin:') - elif os.name == 'nt': - 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(capsys, monkeypatch, tmpdir, sample_module, stdin, which_cfg): - """Test explain setting in all supported config sources. - - :param capsys: pytest fixture. - :param monkeypatch: pytest fixture. - :param tmpdir: pytest fixture. - :param sample_module: conftest fixture. - :param bool stdin: Use stdin source instead of file. - :param str which_cfg: Which config file to test with. - """ - monkeypatch.chdir(tmpdir) - monkeypatch.setattr('sys.argv', ['flake8', '-' if stdin else '.', '-j1']) - - 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()) diff --git a/tests/test_default.py b/tests/test_default.py index 84f356f..d003b03 100644 --- a/tests/test_default.py +++ b/tests/test_default.py @@ -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): diff --git a/tests/test_explain.py b/tests/test_explain.py new file mode 100644 index 0000000..bef60ab --- /dev/null +++ b/tests/test_explain.py @@ -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() diff --git a/tests/test_ignore.py b/tests/test_ignore.py index 5b84c5c..422e2d5 100644 --- a/tests/test_ignore.py +++ b/tests/test_ignore.py @@ -1,4 +1,4 @@ -"""Test against sample modules using the ignore options in all supported config sources.""" +"""Test against sample modules using the ignore option in all supported config sources.""" import os @@ -20,26 +20,26 @@ @pytest.mark.parametrize('ignore', ['D203,D204', 'D2']) @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, tmpdir_session, ignore, stdin, which_cfg): +def test_direct(capsys, monkeypatch, tempdir, ignore, stdin, which_cfg): """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 ignore: Config value for ignore option. :param str stdin: Pipe this file to stdin of flake8. :param str which_cfg: Which config file to test with. """ # 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()) # Write configuration. cfg = which_cfg.split() section = cfg[1] if len(cfg) > 1 else 'pep257' - tmpdir_session.join('empty' if stdin else '', cfg[0]).write('[{0}]\nignore = {1}\n'.format(section, ignore)) + tempdir.join('empty' if stdin else '', cfg[0]).write('[{0}]\nignore = {1}\n'.format(section, ignore)) # Execute. with pytest.raises(SystemExit):