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

Commit

Permalink
use tmpdir
Browse files Browse the repository at this point in the history
  • Loading branch information
Robpol86 committed Jul 30, 2016
1 parent 1de039e commit 8fe78d5
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 33 deletions.
13 changes: 8 additions & 5 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
16 changes: 8 additions & 8 deletions tests/test_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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 '.']
Expand Down
20 changes: 10 additions & 10 deletions tests/test_explain.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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 '.']
Expand Down
20 changes: 10 additions & 10 deletions tests/test_ignore.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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 '.']
Expand Down

0 comments on commit 8fe78d5

Please sign in to comment.