From 8fe78d52ca4bffc10254ad2f274ef227e1dcbb42 Mon Sep 17 00:00:00 2001 From: Robpol86 Date: Sat, 30 Jul 2016 12:27:39 -0700 Subject: [PATCH] use tmpdir --- tests/conftest.py | 13 ++++++++----- tests/test_default.py | 16 ++++++++-------- tests/test_explain.py | 20 ++++++++++---------- tests/test_ignore.py | 20 ++++++++++---------- 4 files changed, 36 insertions(+), 33 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 6f8682a..9446871 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,18 +1,21 @@ # coding=utf-8 -"""Plugins for pytest.""" +"""pytest fixtures for this directory.""" from textwrap import dedent import pytest -@pytest.fixture(scope='function') -def tempdir(tmpdir): - """A tmpdir fixture with prepared source files. +@pytest.fixture(autouse=True) +def sample_code(tmpdir): + """Sample source files. :param tmpdir: pytest fixture. + + :return: tmpdir fixture. + :rtype: py.path """ - tmpdir.join('empty').ensure_dir() + tmpdir.ensure_dir('empty') tmpdir.join('sample.py').write(dedent("""\ #!/usr/bin/env python import sys diff --git a/tests/test_default.py b/tests/test_default.py index f5378af..b06290d 100644 --- a/tests/test_default.py +++ b/tests/test_default.py @@ -25,19 +25,19 @@ @pytest.mark.parametrize('stdin', ['', 'sample_unicode.py', 'sample.py']) -def test_direct(capsys, monkeypatch, tempdir, stdin): +def test_direct(capsys, monkeypatch, tmpdir, stdin): """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 tmpdir: pytest fixture. :param str stdin: Pipe this file to stdin of flake8. """ # Prepare. - monkeypatch.chdir(tempdir.join('empty' if stdin else '')) + monkeypatch.chdir(tmpdir.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()) + monkeypatch.setattr('pep8.stdin_get_value', lambda: tmpdir.join(stdin).read()) # Execute. with pytest.raises(SystemExit): @@ -58,15 +58,15 @@ def test_direct(capsys, monkeypatch, tempdir, stdin): @pytest.mark.parametrize('stdin', ['', 'sample_unicode.py', 'sample.py']) -def test_subprocess(tempdir, stdin): +def test_subprocess(tmpdir, stdin): """Test by calling flake8 through subprocess using a dedicated python process. - :param tempdir: conftest fixture. + :param tmpdir: pytest fixture. :param str stdin: Pipe this file to stdin of flake8. """ # Prepare. - cwd = str(tempdir.join('empty' if stdin else '')) - stdin_handle = tempdir.join(stdin).open() if stdin else None + cwd = str(tmpdir.join('empty' if stdin else '')) + stdin_handle = tmpdir.join(stdin).open() if stdin else None # Execute. command = [find_executable('flake8'), '--exit-zero', '-' if stdin else '.'] diff --git a/tests/test_explain.py b/tests/test_explain.py index b620b37..87e941b 100644 --- a/tests/test_explain.py +++ b/tests/test_explain.py @@ -125,26 +125,26 @@ @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): +def test_direct(capsys, monkeypatch, tmpdir, 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 tmpdir: pytest 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.chdir(tmpdir.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()) + monkeypatch.setattr('pep8.stdin_get_value', lambda: tmpdir.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)) + tmpdir.join('empty' if stdin else '', cfg[0]).write('[{0}]\n{1} = True\n'.format(section, key)) # Execute. with pytest.raises(SystemExit): @@ -166,22 +166,22 @@ def test_direct(capsys, monkeypatch, tempdir, stdin, which_cfg): @pytest.mark.parametrize('stdin', ['', 'sample_unicode.py', 'sample.py']) @pytest.mark.parametrize('which_cfg', ['tox.ini', 'tox.ini flake8', 'setup.cfg', '.pep257']) -def test_subprocess(tempdir, stdin, which_cfg): +def test_subprocess(tmpdir, stdin, which_cfg): """Test by calling flake8 through subprocess using a dedicated python process. - :param tempdir: conftest fixture. + :param tmpdir: pytest fixture. :param str stdin: Pipe this file to stdin of flake8. :param str which_cfg: Which config file to test with. """ # Prepare. - cwd = str(tempdir.join('empty' if stdin else '')) - stdin_handle = tempdir.join(stdin).open() if stdin else None + cwd = str(tmpdir.join('empty' if stdin else '')) + stdin_handle = tmpdir.join(stdin).open() if stdin else None # 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)) + tmpdir.join('empty' if stdin else '', cfg[0]).write('[{0}]\n{1} = True\n'.format(section, key)) # Execute. command = [find_executable('flake8'), '--exit-zero', '-' if stdin else '.'] diff --git a/tests/test_ignore.py b/tests/test_ignore.py index ea61614..c2340f2 100644 --- a/tests/test_ignore.py +++ b/tests/test_ignore.py @@ -23,26 +23,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, tempdir, ignore, stdin, which_cfg): +def test_direct(capsys, monkeypatch, tmpdir, 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 tempdir: conftest fixture. + :param tmpdir: pytest 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(tempdir.join('empty' if stdin else '')) + monkeypatch.chdir(tmpdir.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()) + monkeypatch.setattr('pep8.stdin_get_value', lambda: tmpdir.join(stdin).read()) # Write configuration. cfg = which_cfg.split() section = cfg[1] if len(cfg) > 1 else 'pep257' - tempdir.join('empty' if stdin else '', cfg[0]).write('[{0}]\nignore = {1}\n'.format(section, ignore)) + tmpdir.join('empty' if stdin else '', cfg[0]).write('[{0}]\nignore = {1}\n'.format(section, ignore)) # Execute. with pytest.raises(SystemExit): @@ -65,22 +65,22 @@ def test_direct(capsys, monkeypatch, tempdir, ignore, stdin, which_cfg): @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_subprocess(tempdir, ignore, stdin, which_cfg): +def test_subprocess(tmpdir, ignore, stdin, which_cfg): """Test by calling flake8 through subprocess using a dedicated python process. - :param tempdir: conftest fixture. + :param tmpdir: pytest 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. - cwd = str(tempdir.join('empty' if stdin else '')) - stdin_handle = tempdir.join(stdin).open() if stdin else None + cwd = str(tmpdir.join('empty' if stdin else '')) + stdin_handle = tmpdir.join(stdin).open() if stdin else None # Write configuration. cfg = which_cfg.split() section = cfg[1] if len(cfg) > 1 else 'pep257' - tempdir.join('empty' if stdin else '', cfg[0]).write('[{0}]\nignore = {1}\n'.format(section, ignore)) + tmpdir.join('empty' if stdin else '', cfg[0]).write('[{0}]\nignore = {1}\n'.format(section, ignore)) # Execute. command = [find_executable('flake8'), '--exit-zero', '-' if stdin else '.']