Skip to content

Commit

Permalink
Allow specifying stdout and stderr to session.run.
Browse files Browse the repository at this point in the history
  • Loading branch information
s0undt3ch authored and theacodes committed Jun 17, 2019
1 parent 0bac34f commit 096886a
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 5 deletions.
7 changes: 5 additions & 2 deletions nox/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ def run(
path=None,
success_codes=None,
log=True,
external=False
external=False,
**popen_kws
):
"""Run a command-line program."""

Expand Down Expand Up @@ -105,7 +106,9 @@ def run(
env = _clean_env(env)

try:
return_code, output = popen([cmd_path] + list(args), silent=silent, env=env)
return_code, output = popen(
[cmd_path] + list(args), silent=silent, env=env, **popen_kws
)

if return_code not in success_codes:
logger.error(
Expand Down
9 changes: 6 additions & 3 deletions nox/popen.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@
import sys


def popen(args, env=None, silent=False):
stdout = None
def popen(args, env=None, silent=False, stdout=None, stderr=subprocess.STDOUT):
if silent and stdout is not None:
raise ValueError(
"Can not specify silent and stdout; passing a custom stdout always silences the commands output in Nox's log."
)

if silent:
stdout = subprocess.PIPE

proc = subprocess.Popen(args, env=env, stdout=stdout, stderr=subprocess.STDOUT)
proc = subprocess.Popen(args, env=env, stdout=stdout, stderr=stderr)

try:
out, err = proc.communicate()
Expand Down
92 changes: 92 additions & 0 deletions tests/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,95 @@ def test_interrupt():
with mock.patch("subprocess.Popen", return_value=mock_proc):
with pytest.raises(KeyboardInterrupt):
nox.command.run([PYTHON, "-c" "123"])


def test_custom_stdout(capsys, tmpdir):
with open(str(tmpdir / "out.txt"), "w+b") as stdout:
nox.command.run(
[
PYTHON,
"-c",
'import sys; sys.stdout.write("out");'
'sys.stderr.write("err"); sys.exit(0)',
],
stdout=stdout,
)
out, err = capsys.readouterr()
assert not out
assert "out" not in err
assert "err" not in err
stdout.seek(0)
tempfile_contents = stdout.read().decode("utf-8")
assert "out" in tempfile_contents
assert "err" in tempfile_contents


def test_custom_stdout_silent_flag(capsys, tmpdir):
with open(str(tmpdir / "out.txt"), "w+b") as stdout:
with pytest.raises(ValueError, match="silent"):
nox.command.run([PYTHON, "-c", 'print("hi")'], stdout=stdout, silent=True)


def test_custom_stdout_failed_command(capsys, tmpdir):
with open(str(tmpdir / "out.txt"), "w+b") as stdout:
with pytest.raises(nox.command.CommandFailed):
nox.command.run(
[
PYTHON,
"-c",
'import sys; sys.stdout.write("out");'
'sys.stderr.write("err"); sys.exit(1)',
],
stdout=stdout,
)
out, err = capsys.readouterr()
assert not out
assert "out" not in err
assert "err" not in err
stdout.seek(0)
tempfile_contents = stdout.read().decode("utf-8")
assert "out" in tempfile_contents
assert "err" in tempfile_contents


def test_custom_stderr(capsys, tmpdir):
with open(str(tmpdir / "err.txt"), "w+b") as stderr:
nox.command.run(
[
PYTHON,
"-c",
'import sys; sys.stdout.write("out");'
'sys.stderr.write("err"); sys.exit(0)',
],
stderr=stderr,
)
out, err = capsys.readouterr()
assert not err
assert "out" not in out
assert "err" not in out
stderr.seek(0)
tempfile_contents = stderr.read().decode("utf-8")
assert "out" not in tempfile_contents
assert "err" in tempfile_contents


def test_custom_stderr_failed_command(capsys, tmpdir):
with open(str(tmpdir / "out.txt"), "w+b") as stderr:
with pytest.raises(nox.command.CommandFailed):
nox.command.run(
[
PYTHON,
"-c",
'import sys; sys.stdout.write("out");'
'sys.stderr.write("err"); sys.exit(1)',
],
stderr=stderr,
)
out, err = capsys.readouterr()
assert not err
assert "out" not in out
assert "err" not in out
stderr.seek(0)
tempfile_contents = stderr.read().decode("utf-8")
assert "out" not in tempfile_contents
assert "err" in tempfile_contents

0 comments on commit 096886a

Please sign in to comment.