Skip to content

Fix small terminal glitch when collecting a single test item #2655

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions _pytest/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,22 @@ def write_line(self, line, **markup):
self._tw.line(line, **markup)

def rewrite(self, line, **markup):
"""
Rewinds the terminal cursor to the beginning and writes the given line.

:kwarg erase: if True, will also add spaces until the full terminal width to ensure
previous lines are properly erased.

The rest of the keyword arguments are markup instructions.
"""
erase = markup.pop('erase', False)
if erase:
fill_count = self._tw.fullwidth - len(line)
fill = ' ' * fill_count
else:
fill = ''
line = str(line)
self._tw.write("\r" + line, **markup)
self._tw.write("\r" + line + fill, **markup)

def write_sep(self, sep, title=None, **markup):
self.ensure_newline()
Expand Down Expand Up @@ -292,12 +306,9 @@ def report_collect(self, final=False):
if skipped:
line += " / %d skipped" % skipped
if self.isatty:
self.rewrite(line, bold=True, erase=True)
if final:
line += " \n"
# Rewrite with empty line so we will not see the artifact of
# previous write
self.rewrite('')
self.rewrite(line, bold=True)
self.write('\n')
else:
self.write_line(line)

Expand Down
1 change: 1 addition & 0 deletions changelog/2579.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed small terminal glitch when collecting a single test item.
10 changes: 10 additions & 0 deletions testing/test_terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,16 @@ def test_foobar():
result = testdir.runpytest()
result.stdout.fnmatch_lines(['collected 1 item'])

def test_rewrite(self, testdir, monkeypatch):
config = testdir.parseconfig()
f = py.io.TextIO()
monkeypatch.setattr(f, 'isatty', lambda *args: True)
tr = TerminalReporter(config, f)
tr.writer.fullwidth = 10
tr.write('hello')
tr.rewrite('hey', erase=True)
assert f.getvalue() == 'hello' + '\r' + 'hey' + (7 * ' ')


class TestCollectonly(object):
def test_collectonly_basic(self, testdir):
Expand Down