Skip to content
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

Highlight the path of file location in error report #1778

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,4 @@ Trevor Bekolay
Wouter van Ackooy
Bernard Pratz
Javier Romero
Xuecong Liao
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
2.9.3.dev
=========

* Highlight path of the file location in the error report.

**Bug Fixes**

* Add an 'E' to the first line of error messages from FixtureLookupErrorRepr.
Expand Down
3 changes: 2 additions & 1 deletion _pytest/_code/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,8 @@ def toterminal(self, tw):
i = msg.find("\n")
if i != -1:
msg = msg[:i]
tw.line("%s:%s: %s" %(self.path, self.lineno, msg))
tw.write(self.path, bold=True, red=True)
tw.line(":%s: %s" % (self.lineno, msg))

class ReprLocals(TerminalRepr):
def __init__(self, lines):
Expand Down
106 changes: 68 additions & 38 deletions testing/code/test_excinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,23 @@
pytest_version_info = tuple(map(int, pytest.__version__.split(".")[:3]))

class TWMock:
WRITE = object()

def __init__(self):
self.lines = []
self.is_writing = False
def sep(self, sep, line=None):
self.lines.append((sep, line))
def write(self, msg, **kw):
self.lines.append((TWMock.WRITE, msg))
def line(self, line, **kw):
self.lines.append(line)
def markup(self, text, **kw):
return text
def get_write_msg(self, idx):
flag, msg = self.lines[idx]
assert flag == TWMock.WRITE
return msg

fullwidth = 80

Expand Down Expand Up @@ -755,14 +764,18 @@ def f():
assert tw.lines[0] == " def f():"
assert tw.lines[1] == "> g(3)"
assert tw.lines[2] == ""
assert tw.lines[3].endswith("mod.py:5: ")
assert tw.lines[4] == ("_ ", None)
assert tw.lines[5] == ""
assert tw.lines[6] == " def g(x):"
assert tw.lines[7] == "> raise ValueError(x)"
assert tw.lines[8] == "E ValueError: 3"
assert tw.lines[9] == ""
assert tw.lines[10].endswith("mod.py:3: ValueError")
line = tw.get_write_msg(3)
assert line.endswith("mod.py")
assert tw.lines[4] == (":5: ")
assert tw.lines[5] == ("_ ", None)
assert tw.lines[6] == ""
assert tw.lines[7] == " def g(x):"
assert tw.lines[8] == "> raise ValueError(x)"
assert tw.lines[9] == "E ValueError: 3"
assert tw.lines[10] == ""
line = tw.get_write_msg(11)
assert line.endswith("mod.py")
assert tw.lines[12] == ":3: ValueError"

def test_toterminal_long_missing_source(self, importasmod, tmpdir):
mod = importasmod("""
Expand All @@ -781,13 +794,17 @@ def f():
tw.lines.pop(0)
assert tw.lines[0] == "> ???"
assert tw.lines[1] == ""
assert tw.lines[2].endswith("mod.py:5: ")
assert tw.lines[3] == ("_ ", None)
assert tw.lines[4] == ""
assert tw.lines[5] == "> ???"
assert tw.lines[6] == "E ValueError: 3"
assert tw.lines[7] == ""
assert tw.lines[8].endswith("mod.py:3: ValueError")
line = tw.get_write_msg(2)
assert line.endswith("mod.py")
assert tw.lines[3] == ":5: "
assert tw.lines[4] == ("_ ", None)
assert tw.lines[5] == ""
assert tw.lines[6] == "> ???"
assert tw.lines[7] == "E ValueError: 3"
assert tw.lines[8] == ""
line = tw.get_write_msg(9)
assert line.endswith("mod.py")
assert tw.lines[10] == ":3: ValueError"

def test_toterminal_long_incomplete_source(self, importasmod, tmpdir):
mod = importasmod("""
Expand All @@ -806,13 +823,17 @@ def f():
tw.lines.pop(0)
assert tw.lines[0] == "> ???"
assert tw.lines[1] == ""
assert tw.lines[2].endswith("mod.py:5: ")
assert tw.lines[3] == ("_ ", None)
assert tw.lines[4] == ""
assert tw.lines[5] == "> ???"
assert tw.lines[6] == "E ValueError: 3"
assert tw.lines[7] == ""
assert tw.lines[8].endswith("mod.py:3: ValueError")
line = tw.get_write_msg(2)
assert line.endswith("mod.py")
assert tw.lines[3] == ":5: "
assert tw.lines[4] == ("_ ", None)
assert tw.lines[5] == ""
assert tw.lines[6] == "> ???"
assert tw.lines[7] == "E ValueError: 3"
assert tw.lines[8] == ""
line = tw.get_write_msg(9)
assert line.endswith("mod.py")
assert tw.lines[10] == ":3: ValueError"

def test_toterminal_long_filenames(self, importasmod):
mod = importasmod("""
Expand All @@ -826,15 +847,18 @@ def f():
try:
repr = excinfo.getrepr(abspath=False)
repr.toterminal(tw)
line = tw.lines[-1]
x = py.path.local().bestrelpath(path)
if len(x) < len(str(path)):
assert line == "mod.py:3: ValueError"
msg = tw.get_write_msg(-2)
assert msg == "mod.py"
assert tw.lines[-1] == ":3: ValueError"

repr = excinfo.getrepr(abspath=True)
repr.toterminal(tw)
msg = tw.get_write_msg(-2)
assert msg == path
line = tw.lines[-1]
assert line == "%s:3: ValueError" %(path,)
assert line == ":3: ValueError"
finally:
old.chdir()

Expand Down Expand Up @@ -896,19 +920,25 @@ def i():
assert tw.lines[1] == " def f():"
assert tw.lines[2] == "> g()"
assert tw.lines[3] == ""
assert tw.lines[4].endswith("mod.py:3: ")
assert tw.lines[5] == ("_ ", None)
assert tw.lines[6].endswith("in g")
assert tw.lines[7] == " h()"
assert tw.lines[8].endswith("in h")
assert tw.lines[9] == " i()"
assert tw.lines[10] == ("_ ", None)
assert tw.lines[11] == ""
assert tw.lines[12] == " def i():"
assert tw.lines[13] == "> raise ValueError()"
assert tw.lines[14] == "E ValueError"
assert tw.lines[15] == ""
assert tw.lines[16].endswith("mod.py:9: ValueError")
msg = tw.get_write_msg(4)
assert msg.endswith("mod.py")
assert tw.lines[5] == ":3: "
assert tw.lines[6] == ("_ ", None)
tw.get_write_msg(7)
assert tw.lines[8].endswith("in g")
assert tw.lines[9] == " h()"
tw.get_write_msg(10)
assert tw.lines[11].endswith("in h")
assert tw.lines[12] == " i()"
assert tw.lines[13] == ("_ ", None)
assert tw.lines[14] == ""
assert tw.lines[15] == " def i():"
assert tw.lines[16] == "> raise ValueError()"
assert tw.lines[17] == "E ValueError"
assert tw.lines[18] == ""
msg = tw.get_write_msg(19)
msg.endswith("mod.py")
assert tw.lines[20] == ":9: ValueError"


@pytest.mark.parametrize("style", ["short", "long"])
Expand Down