Skip to content

Commit 7f601a9

Browse files
authored
terminal: pytest_report_header: display cwd if != rootdir (#250)
1 parent e6934d4 commit 7f601a9

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

src/_pytest/terminal.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -732,13 +732,17 @@ def _write_report_lines_from_hooks(self, lines):
732732
for line in collapse(lines):
733733
self.write_line(line)
734734

735-
def pytest_report_header(self, config):
736-
line = "rootdir: %s" % _shorten_path(Path(config.rootdir))
735+
def pytest_report_header(self, config: Config) -> List[str]:
736+
rootdir = _shorten_path(Path(str(config.rootdir)))
737+
line = "rootdir: {}".format(rootdir)
737738

738739
if config.inifile:
739740
line += ", inifile: {}".format(
740-
_shorten_path(Path(config.rootdir.bestrelpath(config.inifile)))
741+
_shorten_path(Path(config.rootdir.bestrelpath(config.inifile))) # type: ignore # (currently wrong)
741742
)
743+
cwd = _shorten_path(Path.cwd())
744+
if rootdir != cwd:
745+
line += ", cwd: {}".format(cwd)
742746

743747
testpaths = config.getini("testpaths")
744748
if testpaths and config.args == testpaths:

testing/test_session.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,7 @@ def test_rootdir_option_arg(testdir: Testdir, monkeypatch, path: str) -> None:
341341
monkeypatch.setenv("PY_ROOTDIR_PATH", str(testdir.tmpdir))
342342
path = path.format(relative=str(testdir.tmpdir), environment="$PY_ROOTDIR_PATH")
343343

344-
rootdir = testdir.mkdir("root")
345-
rootdir.mkdir("tests")
344+
testdir.mkdir("root")
346345
testdir.makepyfile(
347346
"""
348347
import os
@@ -354,7 +353,7 @@ def test_one():
354353
)
355354

356355
expected = [
357-
"*rootdir: ~/root",
356+
"*rootdir: ~/root, cwd: ~",
358357
"test_rootdir_option_arg.py *",
359358
"*1 passed*",
360359
]

testing/test_terminal.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ def test_passes():
663663
if request.config.pluginmanager.list_plugin_distinfo():
664664
result.stdout.fnmatch_lines(["plugins: *"])
665665

666-
def test_header(self, testdir):
666+
def test_header(self, testdir: Testdir) -> None:
667667
testdir.tmpdir.join("tests").ensure_dir()
668668
testdir.tmpdir.join("gui").ensure_dir()
669669

@@ -692,6 +692,11 @@ def test_header(self, testdir):
692692
result = testdir.runpytest("tests")
693693
result.stdout.fnmatch_lines(["rootdir: ~, inifile: tox.ini"])
694694

695+
# Reports cwd if != rootdir.
696+
testdir.makefile("ini", **{"tests/pytest": ""})
697+
result = testdir.runpytest("tests")
698+
result.stdout.fnmatch_lines(["rootdir: ~/tests, inifile: pytest.ini, cwd: ~"])
699+
695700
def test_showlocals(self, testdir):
696701
p1 = testdir.makepyfile(
697702
"""

0 commit comments

Comments
 (0)