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

Commit

Permalink
no subprocess
Browse files Browse the repository at this point in the history
  • Loading branch information
Robpol86 committed Jul 30, 2016
1 parent 8fe78d5 commit 3963215
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 13 deletions.
38 changes: 32 additions & 6 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,37 @@
"""Non-fixture code shared among test modules."""

import os
import subprocess

try:
from subprocess import check_output, STDOUT
except ImportError:
from subprocess32 import check_output, STDOUT

def check_output(command, cwd=None, env=None, stdin=None):
"""Basically subprocess.check_output since it doesn't exist in Python 2.6.
assert check_output
assert STDOUT
:raise CalledProcessError: Command exits non-zero.
:param iter command: Command to run.
:param str cwd: Current working directory.
:param dict env: Set environment variables.
:param file stdin: Pipe handle to subprocess' stdin.
:return: Command output.
:rtype: str
"""
stdout = subprocess.STDOUT
pipe = subprocess.PIPE

# Setup env.
environ = os.environ.copy()
if env:
environ.update(env)

# Run command.
with open(os.devnull) as null:
proc = subprocess.Popen(command, cwd=cwd, env=environ, stdout=pipe, stderr=stdout, stdin=stdin or null)
output = proc.communicate()[0].decode('utf-8') # Blocks until command exits.

# Raise if non-zero exit.
if proc.poll() != 0:
raise subprocess.CalledProcessError(proc.poll(), command, output=output)

return output
4 changes: 2 additions & 2 deletions tests/test_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import flake8.main
import pytest

from tests import check_output, STDOUT
from tests import check_output

EXPECTED = """\
./sample.py:1:1: D100 Missing docstring in public module
Expand Down Expand Up @@ -72,7 +72,7 @@ def test_subprocess(tmpdir, stdin):
command = [find_executable('flake8'), '--exit-zero', '-' if stdin else '.']
environ = os.environ.copy()
environ['COV_CORE_DATAFILE'] = '' # Disable pytest-cov's subprocess coverage feature. Doesn't work right now.
out = check_output(command, stderr=STDOUT, cwd=cwd, stdin=stdin_handle, env=environ).decode('utf-8')
out = check_output(command, cwd=cwd, stdin=stdin_handle, env=environ)

# Clean.
if stdin:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_explain.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import flake8.main
import pytest

from tests import check_output, STDOUT
from tests import check_output

EXPECTED = list()
EXPECTED.append("""\
Expand Down Expand Up @@ -187,7 +187,7 @@ def test_subprocess(tmpdir, stdin, which_cfg):
command = [find_executable('flake8'), '--exit-zero', '-' if stdin else '.']
environ = os.environ.copy()
environ['COV_CORE_DATAFILE'] = '' # Disable pytest-cov's subprocess coverage feature. Doesn't work right now.
out = check_output(command, stderr=STDOUT, cwd=cwd, stdin=stdin_handle, env=environ).decode('utf-8')
out = check_output(command, cwd=cwd, stdin=stdin_handle, env=environ)

# Clean.
if stdin:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_ignore.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import flake8.main
import pytest

from tests import check_output, STDOUT
from tests import check_output

EXPECTED = """\
./sample.py:1:1: D100 Missing docstring in public module
Expand Down Expand Up @@ -86,7 +86,7 @@ def test_subprocess(tmpdir, ignore, stdin, which_cfg):
command = [find_executable('flake8'), '--exit-zero', '-' if stdin else '.']
environ = os.environ.copy()
environ['COV_CORE_DATAFILE'] = '' # Disable pytest-cov's subprocess coverage feature. Doesn't work right now.
out = check_output(command, stderr=STDOUT, cwd=cwd, stdin=stdin_handle, env=environ).decode('utf-8')
out = check_output(command, cwd=cwd, stdin=stdin_handle, env=environ)

# Clean.
if stdin:
Expand Down
1 change: 0 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ commands =
deps =
flake8==2.5.4
pytest-cov==2.2.1
py26: subprocess32==3.2.7
usedevelop = True

[testenv:py35x64]
Expand Down

0 comments on commit 3963215

Please sign in to comment.