Skip to content

Commit

Permalink
different approach to circumvent pytests capturing to interfere with …
Browse files Browse the repository at this point in the history
…the tee test.

hopefully this will also work on windows
  • Loading branch information
Qwlouse committed Jan 4, 2017
1 parent 6a9ad6e commit c30a06a
Showing 1 changed file with 24 additions and 27 deletions.
51 changes: 24 additions & 27 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,32 +154,29 @@ def test_convert_camel_case_to_snake_case(name, expected):
assert convert_camel_case_to_snake_case(name) == expected


def test_tee_output():
def test_tee_output(capsys):
from sacred.optional import libc

# dirty hack to work around py.test also hijacking the stdout/stderr
stdout = sys.__stdout__
stderr = sys.__stderr__

try:
print('before (stdout)', file=stdout)
print('before (stderr)', file=stderr)
with tempfile.NamedTemporaryFile(delete=False) as f, tee_output(f):
print("captured stdout", file=stdout)
print("captured stderr", file=stderr)
libc.puts(b'stdout from C')
os.system('echo and this is from echo')

print('after (stdout)', file=stdout)
print('after (stderr)', file=stderr)

with open(f.name, 'r') as f:
lines = set(f.readlines())
assert lines == {
"captured stdout\n",
"captured stderr\n",
"stdout from C\n",
"and this is from echo\n"}
finally:
print('deleting', f.name)
os.remove(f.name)
with capsys.disabled():
try:
print('before (stdout)')
print('before (stderr)')
with tempfile.NamedTemporaryFile(delete=False) as f, tee_output(f):
print("captured stdout")
print("captured stderr")
libc.puts(b'stdout from C')
os.system('echo and this is from echo')

print('after (stdout)')
print('after (stderr)')

with open(f.name, 'r') as f:
lines = set(f.readlines())
assert lines == {
"captured stdout\n",
"captured stderr\n",
"stdout from C\n",
"and this is from echo\n"}
finally:
print('deleting', f.name)
os.remove(f.name)

0 comments on commit c30a06a

Please sign in to comment.