Skip to content

Commit

Permalink
added a test for stdout capturing and fixed two problems with the win…
Browse files Browse the repository at this point in the history
…dows tests
  • Loading branch information
Qwlouse committed May 7, 2017
1 parent 520f21d commit 3e335de
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
9 changes: 5 additions & 4 deletions sacred/stdout_capturing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
from __future__ import division, print_function, unicode_literals
import os
import sys
from io import StringIO
import subprocess
from threading import Timer
from contextlib import contextmanager
import wrapt
from sacred.optional import libc
from tempfile import NamedTemporaryFile
from sacred.settings import SETTINGS
from sacred.utils import FileNotFoundError
from sacred.utils import FileNotFoundError, StringIO


__sacred__ = True # marks files that should be filtered from stack traces
Expand Down Expand Up @@ -110,11 +109,13 @@ def tee_output_fd():
['tee', '-a', '/dev/stderr'], preexec_fn=os.setsid,
stdin=subprocess.PIPE, stderr=target_fd, stdout=2)
except (FileNotFoundError, OSError):
# No tee found in this operating system. Trying to use a python
# implementation of tee. However this is slow and error-prone.
tee_stdout = subprocess.Popen(
[sys.executable, "-m", "sacred.pytee"], preexec_fn=os.setsid,
[sys.executable, "-m", "sacred.pytee"],
stdin=subprocess.PIPE, stderr=target_fd)
tee_stderr = subprocess.Popen(
[sys.executable, "-m", "sacred.pytee"], preexec_fn=os.setsid,
[sys.executable, "-m", "sacred.pytee"],
stdin=subprocess.PIPE, stdout=target_fd)

flush()
Expand Down
12 changes: 9 additions & 3 deletions sacred/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
class FileNotFoundError(IOError):
def __init__(self, msg):
super(FileNotFoundError, self).__init__(errno.ENOENT, msg)
from StringIO import StringIO
else:
# Reassign so that we can import it from here
FileNotFoundError = FileNotFoundError
from io import StringIO

NO_LOGGER = logging.getLogger('ignore')
NO_LOGGER.disabled = 1
Expand Down Expand Up @@ -303,7 +305,8 @@ def apply_backspaces_and_linefeeds(text):
Interpret text like a terminal by removing backspace and linefeed
characters and applying them line by line.
If final line ends with a carriage it keeps it to be concatanable with next output chunk
If final line ends with a carriage it keeps it to be concatenable with next
output chunk.
"""
orig_lines = text.split('\n')
orig_lines_len = len(orig_lines)
Expand All @@ -312,12 +315,15 @@ def apply_backspaces_and_linefeeds(text):
chars, cursor = [], 0
orig_line_len = len(orig_line)
for orig_char_idx, orig_char in enumerate(orig_line):
if orig_char == '\r' and (orig_char_idx != orig_line_len - 1 or orig_line_idx != orig_lines_len - 1):
if orig_char == '\r' and (orig_char_idx != orig_line_len - 1 or
orig_line_idx != orig_lines_len - 1):
cursor = 0
elif orig_char == '\b':
cursor = max(0, cursor - 1)
else:
if orig_char == '\r' and orig_char_idx == orig_line_len - 1 and orig_line_idx == orig_lines_len - 1:
if (orig_char == '\r' and
orig_char_idx == orig_line_len - 1 and
orig_line_idx == orig_lines_len - 1):
cursor = len(chars)
if cursor == len(chars):
chars.append(orig_char)
Expand Down
15 changes: 15 additions & 0 deletions tests/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,21 @@ def test_unobserved_run_doesnt_emit(run):
assert not observer.failed_event.called


@pytest.mark.parametrize("capture_mode", ["no", "sys", "fd"])
def test_stdout_capturing(run, capsys, capture_mode):
def print_mock_progress():
for i in range(10):
print(i, end="")
sys.stdout.flush()

run.main_function.side_effect = print_mock_progress
run.capture_mode = capture_mode
with capsys.disabled():
run()
if capture_mode != "no":
assert run.captured_out == '0123456789'


def test_captured_out_filter(run, capsys):
def print_mock_progress():
sys.stdout.write('progress 0')
Expand Down

0 comments on commit 3e335de

Please sign in to comment.