Skip to content

Commit 12e6095

Browse files
Merge pull request #2655 from nicoddemus/terminal-collecting-glitch
Fix small terminal glitch when collecting a single test item
2 parents 8969bd4 + ad52f71 commit 12e6095

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

_pytest/terminal.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,22 @@ def write_line(self, line, **markup):
180180
self._tw.line(line, **markup)
181181

182182
def rewrite(self, line, **markup):
183+
"""
184+
Rewinds the terminal cursor to the beginning and writes the given line.
185+
186+
:kwarg erase: if True, will also add spaces until the full terminal width to ensure
187+
previous lines are properly erased.
188+
189+
The rest of the keyword arguments are markup instructions.
190+
"""
191+
erase = markup.pop('erase', False)
192+
if erase:
193+
fill_count = self._tw.fullwidth - len(line)
194+
fill = ' ' * fill_count
195+
else:
196+
fill = ''
183197
line = str(line)
184-
self._tw.write("\r" + line, **markup)
198+
self._tw.write("\r" + line + fill, **markup)
185199

186200
def write_sep(self, sep, title=None, **markup):
187201
self.ensure_newline()
@@ -292,12 +306,9 @@ def report_collect(self, final=False):
292306
if skipped:
293307
line += " / %d skipped" % skipped
294308
if self.isatty:
309+
self.rewrite(line, bold=True, erase=True)
295310
if final:
296-
line += " \n"
297-
# Rewrite with empty line so we will not see the artifact of
298-
# previous write
299-
self.rewrite('')
300-
self.rewrite(line, bold=True)
311+
self.write('\n')
301312
else:
302313
self.write_line(line)
303314

changelog/2579.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed small terminal glitch when collecting a single test item.

testing/test_terminal.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,16 @@ def test_foobar():
214214
result = testdir.runpytest()
215215
result.stdout.fnmatch_lines(['collected 1 item'])
216216

217+
def test_rewrite(self, testdir, monkeypatch):
218+
config = testdir.parseconfig()
219+
f = py.io.TextIO()
220+
monkeypatch.setattr(f, 'isatty', lambda *args: True)
221+
tr = TerminalReporter(config, f)
222+
tr.writer.fullwidth = 10
223+
tr.write('hello')
224+
tr.rewrite('hey', erase=True)
225+
assert f.getvalue() == 'hello' + '\r' + 'hey' + (7 * ' ')
226+
217227

218228
class TestCollectonly(object):
219229
def test_collectonly_basic(self, testdir):

0 commit comments

Comments
 (0)