Skip to content

Commit

Permalink
Merge pull request #2804 from nicoddemus/terminal-reporter-tw-2803
Browse files Browse the repository at this point in the history
Deprecate TerminalReporter._tw
  • Loading branch information
RonnyPfannschmidt committed Sep 28, 2017
2 parents c0c859c + 3b30c93 commit d132c50
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 56 deletions.
2 changes: 1 addition & 1 deletion _pytest/config.py
Expand Up @@ -931,7 +931,7 @@ def warn(self, code, message, fslocation=None, nodeid=None):
fslocation=fslocation, nodeid=nodeid))

def get_terminal_writer(self):
return self.pluginmanager.get_plugin("terminalreporter")._tw
return self.pluginmanager.get_plugin("terminalreporter").writer

def pytest_cmdline_parse(self, pluginmanager, args):
# REF1 assert self == pluginmanager.config, (self, pluginmanager.config)
Expand Down
2 changes: 1 addition & 1 deletion _pytest/debugging.py
Expand Up @@ -83,7 +83,7 @@ def _enter_pdb(node, excinfo, rep):
# XXX we re-use the TerminalReporter's terminalwriter
# because this seems to avoid some encoding related troubles
# for not completely clear reasons.
tw = node.config.pluginmanager.getplugin("terminalreporter")._tw
tw = node.config.pluginmanager.getplugin("terminalreporter").writer
tw.line()
tw.sep(">", "traceback")
rep.toterminal(tw)
Expand Down
2 changes: 1 addition & 1 deletion _pytest/helpconfig.py
Expand Up @@ -107,7 +107,7 @@ def pytest_cmdline_main(config):

def showhelp(config):
reporter = config.pluginmanager.get_plugin('terminalreporter')
tw = reporter._tw
tw = reporter.writer
tw.write(config._parser.optparser.format_help())
tw.line()
tw.line()
Expand Down
6 changes: 3 additions & 3 deletions _pytest/pastebin.py
Expand Up @@ -25,15 +25,15 @@ def pytest_configure(config):
if tr is not None:
# pastebin file will be utf-8 encoded binary file
config._pastebinfile = tempfile.TemporaryFile('w+b')
oldwrite = tr._tw.write
oldwrite = tr.writer.write

def tee_write(s, **kwargs):
oldwrite(s, **kwargs)
if isinstance(s, six.text_type):
s = s.encode('utf-8')
config._pastebinfile.write(s)

tr._tw.write = tee_write
tr.writer.write = tee_write


def pytest_unconfigure(config):
Expand All @@ -45,7 +45,7 @@ def pytest_unconfigure(config):
del config._pastebinfile
# undo our patching in the terminal reporter
tr = config.pluginmanager.getplugin('terminalreporter')
del tr._tw.__dict__['write']
del tr.writer.__dict__['write']
# write summary
tr.write_sep("=", "Sending information to Paste Service")
pastebinurl = create_new_paste(sessionlog)
Expand Down
4 changes: 2 additions & 2 deletions _pytest/skipping.py
Expand Up @@ -295,9 +295,9 @@ def pytest_terminal_summary(terminalreporter):
show_simple(terminalreporter, lines, 'passed', "PASSED %s")

if lines:
tr._tw.sep("=", "short test summary info")
tr.writer.sep("=", "short test summary info")
for line in lines:
tr._tw.line(line)
tr.writer.line(line)


def show_simple(terminalreporter, lines, stat, format):
Expand Down
110 changes: 62 additions & 48 deletions _pytest/terminal.py
Expand Up @@ -5,15 +5,18 @@
from __future__ import absolute_import, division, print_function

import itertools
from _pytest.main import EXIT_OK, EXIT_TESTSFAILED, EXIT_INTERRUPTED, \
EXIT_USAGEERROR, EXIT_NOTESTSCOLLECTED
import pytest
import py
import six
import platform
import sys
import time
import platform
import warnings

import py
import six

import pluggy
import pytest
from _pytest.main import EXIT_OK, EXIT_TESTSFAILED, EXIT_INTERRUPTED, \
EXIT_USAGEERROR, EXIT_NOTESTSCOLLECTED


def pytest_addoption(parser):
Expand Down Expand Up @@ -136,13 +139,22 @@ def __init__(self, config, file=None):
self.startdir = py.path.local()
if file is None:
file = sys.stdout
self._tw = self.writer = _pytest.config.create_terminal_writer(config,
file)
self._writer = _pytest.config.create_terminal_writer(config, file)
self.currentfspath = None
self.reportchars = getreportopt(config)
self.hasmarkup = self._tw.hasmarkup
self.hasmarkup = self.writer.hasmarkup
self.isatty = file.isatty()

@property
def writer(self):
return self._writer

@property
def _tw(self):
warnings.warn(DeprecationWarning('TerminalReporter._tw is deprecated, use TerminalReporter.writer instead'),
stacklevel=2)
return self.writer

def hasopt(self, char):
char = {'xfailed': 'x', 'skipped': 's'}.get(char, char)
return char in self.reportchars
Expand All @@ -152,32 +164,32 @@ def write_fspath_result(self, nodeid, res):
if fspath != self.currentfspath:
self.currentfspath = fspath
fspath = self.startdir.bestrelpath(fspath)
self._tw.line()
self._tw.write(fspath + " ")
self._tw.write(res)
self.writer.line()
self.writer.write(fspath + " ")
self.writer.write(res)

def write_ensure_prefix(self, prefix, extra="", **kwargs):
if self.currentfspath != prefix:
self._tw.line()
self.writer.line()
self.currentfspath = prefix
self._tw.write(prefix)
self.writer.write(prefix)
if extra:
self._tw.write(extra, **kwargs)
self.writer.write(extra, **kwargs)
self.currentfspath = -2

def ensure_newline(self):
if self.currentfspath:
self._tw.line()
self.writer.line()
self.currentfspath = None

def write(self, content, **markup):
self._tw.write(content, **markup)
self.writer.write(content, **markup)

def write_line(self, line, **markup):
if not isinstance(line, six.text_type):
line = six.text_type(line, errors="replace")
self.ensure_newline()
self._tw.line(line, **markup)
self.writer.line(line, **markup)

def rewrite(self, line, **markup):
"""
Expand All @@ -190,22 +202,22 @@ def rewrite(self, line, **markup):
"""
erase = markup.pop('erase', False)
if erase:
fill_count = self._tw.fullwidth - len(line)
fill_count = self.writer.fullwidth - len(line)
fill = ' ' * fill_count
else:
fill = ''
line = str(line)
self._tw.write("\r" + line + fill, **markup)
self.writer.write("\r" + line + fill, **markup)

def write_sep(self, sep, title=None, **markup):
self.ensure_newline()
self._tw.sep(sep, title, **markup)
self.writer.sep(sep, title, **markup)

def section(self, title, sep="=", **kw):
self._tw.sep(sep, title, **kw)
self.writer.sep(sep, title, **kw)

def line(self, msg, **kw):
self._tw.line(msg, **kw)
self.writer.line(msg, **kw)

def pytest_internalerror(self, excrepr):
for line in six.text_type(excrepr).split("\n"):
Expand Down Expand Up @@ -252,7 +264,7 @@ def pytest_runtest_logreport(self, report):
if not hasattr(rep, 'node') and self.showfspath:
self.write_fspath_result(rep.nodeid, letter)
else:
self._tw.write(letter)
self.writer.write(letter)
else:
if isinstance(word, tuple):
word, markup = word
Expand All @@ -263,16 +275,18 @@ def pytest_runtest_logreport(self, report):
markup = {'red': True}
elif rep.skipped:
markup = {'yellow': True}
else:
markup = {}
line = self._locationline(rep.nodeid, *rep.location)
if not hasattr(rep, 'node'):
self.write_ensure_prefix(line, word, **markup)
# self._tw.write(word, **markup)
# self.writer.write(word, **markup)
else:
self.ensure_newline()
if hasattr(rep, 'node'):
self._tw.write("[%s] " % rep.node.gateway.id)
self._tw.write(word, **markup)
self._tw.write(" " + line)
self.writer.write("[%s] " % rep.node.gateway.id)
self.writer.write(word, **markup)
self.writer.write(" " + line)
self.currentfspath = -2

def pytest_collection(self):
Expand Down Expand Up @@ -358,9 +372,9 @@ def pytest_collection_finish(self, session):
if self.config.option.collectonly:
self._printcollecteditems(session.items)
if self.stats.get('failed'):
self._tw.sep("!", "collection failures")
self.writer.sep("!", "collection failures")
for rep in self.stats.get('failed'):
rep.toterminal(self._tw)
rep.toterminal(self.writer)
return 1
return 0
lines = self.config.hook.pytest_report_collectionfinish(
Expand All @@ -378,12 +392,12 @@ def _printcollecteditems(self, items):
name = item.nodeid.split('::', 1)[0]
counts[name] = counts.get(name, 0) + 1
for name, count in sorted(counts.items()):
self._tw.line("%s: %d" % (name, count))
self.writer.line("%s: %d" % (name, count))
else:
for item in items:
nodeid = item.nodeid
nodeid = nodeid.replace("::()::", "::")
self._tw.line(nodeid)
self.writer.line(nodeid)
return
stack = []
indent = ""
Expand All @@ -398,13 +412,13 @@ def _printcollecteditems(self, items):
# if col.name == "()":
# continue
indent = (len(stack) - 1) * " "
self._tw.line("%s%s" % (indent, col))
self.writer.line("%s%s" % (indent, col))

@pytest.hookimpl(hookwrapper=True)
def pytest_sessionfinish(self, exitstatus):
outcome = yield
outcome.get_result()
self._tw.line("")
self.writer.line("")
summary_exit_codes = (
EXIT_OK, EXIT_TESTSFAILED, EXIT_INTERRUPTED, EXIT_USAGEERROR,
EXIT_NOTESTSCOLLECTED)
Expand Down Expand Up @@ -434,10 +448,10 @@ def _report_keyboardinterrupt(self):
self.write_sep("!", msg)
if "KeyboardInterrupt" in msg:
if self.config.option.fulltrace:
excrepr.toterminal(self._tw)
excrepr.toterminal(self.writer)
else:
self._tw.line("to show a full traceback on KeyboardInterrupt use --fulltrace", yellow=True)
excrepr.reprcrash.toterminal(self._tw)
self.writer.line("to show a full traceback on KeyboardInterrupt use --fulltrace", yellow=True)
excrepr.reprcrash.toterminal(self.writer)

def _locationline(self, nodeid, fspath, lineno, domain):
def mkrel(nodeid):
Expand Down Expand Up @@ -493,14 +507,14 @@ def summary_warnings(self):
grouped = itertools.groupby(all_warnings, key=lambda wr: wr.get_location(self.config))

self.write_sep("=", "warnings summary", yellow=True, bold=False)
for location, warnings in grouped:
self._tw.line(str(location) or '<undetermined location>')
for w in warnings:
for location, warning_records in grouped:
self.writer.line(str(location) or '<undetermined location>')
for w in warning_records:
lines = w.message.splitlines()
indented = '\n'.join(' ' + x for x in lines)
self._tw.line(indented)
self._tw.line()
self._tw.line('-- Docs: http://doc.pytest.org/en/latest/warnings.html')
self.writer.line(indented)
self.writer.line()
self.writer.line('-- Docs: http://doc.pytest.org/en/latest/warnings.html')

def summary_passes(self):
if self.config.option.tbstyle != "no":
Expand All @@ -517,10 +531,10 @@ def summary_passes(self):
def print_teardown_sections(self, rep):
for secname, content in rep.sections:
if 'teardown' in secname:
self._tw.sep('-', secname)
self.writer.sep('-', secname)
if content[-1:] == "\n":
content = content[:-1]
self._tw.line(content)
self.writer.line(content)

def summary_failures(self):
if self.config.option.tbstyle != "no":
Expand Down Expand Up @@ -560,12 +574,12 @@ def summary_errors(self):
self._outrep_summary(rep)

def _outrep_summary(self, rep):
rep.toterminal(self._tw)
rep.toterminal(self.writer)
for secname, content in rep.sections:
self._tw.sep("-", secname)
self.writer.sep("-", secname)
if content[-1:] == "\n":
content = content[:-1]
self._tw.line(content)
self.writer.line(content)

def summary_stats(self):
session_duration = time.time() - self._sessionstarttime
Expand Down
1 change: 1 addition & 0 deletions changelog/2803.removal
@@ -0,0 +1 @@
``TerminalReporter._tw`` has been deprecated in favor of ``TerminalReporter.writer`` and will be removed in a future version. Also, ``TerminalReporter.writer`` is now read-only.

0 comments on commit d132c50

Please sign in to comment.