Skip to content

Commit a6dd90a

Browse files
committed
python: use invocation dir instead of cwd in fixture-printing code
We should aim to remove all `cwd()` calls except one, otherwise things will go bad if the working directory changes. Use the invocation dir instead.
1 parent 676f38d commit a6dd90a

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

src/_pytest/python.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1525,14 +1525,13 @@ def _ascii_escaped_by_config(val: Union[str, bytes], config: Optional[Config]) -
15251525
return val if escape_option else ascii_escaped(val) # type: ignore
15261526

15271527

1528-
def _pretty_fixture_path(func) -> str:
1529-
cwd = Path.cwd()
1530-
loc = Path(getlocation(func, str(cwd)))
1528+
def _pretty_fixture_path(invocation_dir: Path, func) -> str:
1529+
loc = Path(getlocation(func, invocation_dir))
15311530
prefix = Path("...", "_pytest")
15321531
try:
15331532
return str(prefix / loc.relative_to(_PYTEST_DIR))
15341533
except ValueError:
1535-
return bestrelpath(cwd, loc)
1534+
return bestrelpath(invocation_dir, loc)
15361535

15371536

15381537
def show_fixtures_per_test(config):
@@ -1545,19 +1544,19 @@ def _show_fixtures_per_test(config: Config, session: Session) -> None:
15451544
import _pytest.config
15461545

15471546
session.perform_collect()
1548-
curdir = Path.cwd()
1547+
invocation_dir = config.invocation_params.dir
15491548
tw = _pytest.config.create_terminal_writer(config)
15501549
verbose = config.getvalue("verbose")
15511550

15521551
def get_best_relpath(func) -> str:
1553-
loc = getlocation(func, str(curdir))
1554-
return bestrelpath(curdir, Path(loc))
1552+
loc = getlocation(func, invocation_dir)
1553+
return bestrelpath(invocation_dir, Path(loc))
15551554

15561555
def write_fixture(fixture_def: fixtures.FixtureDef[object]) -> None:
15571556
argname = fixture_def.argname
15581557
if verbose <= 0 and argname.startswith("_"):
15591558
return
1560-
prettypath = _pretty_fixture_path(fixture_def.func)
1559+
prettypath = _pretty_fixture_path(invocation_dir, fixture_def.func)
15611560
tw.write(f"{argname}", green=True)
15621561
tw.write(f" -- {prettypath}", yellow=True)
15631562
tw.write("\n")
@@ -1601,7 +1600,7 @@ def _showfixtures_main(config: Config, session: Session) -> None:
16011600
import _pytest.config
16021601

16031602
session.perform_collect()
1604-
curdir = Path.cwd()
1603+
invocation_dir = config.invocation_params.dir
16051604
tw = _pytest.config.create_terminal_writer(config)
16061605
verbose = config.getvalue("verbose")
16071606

@@ -1615,15 +1614,15 @@ def _showfixtures_main(config: Config, session: Session) -> None:
16151614
if not fixturedefs:
16161615
continue
16171616
for fixturedef in fixturedefs:
1618-
loc = getlocation(fixturedef.func, str(curdir))
1617+
loc = getlocation(fixturedef.func, invocation_dir)
16191618
if (fixturedef.argname, loc) in seen:
16201619
continue
16211620
seen.add((fixturedef.argname, loc))
16221621
available.append(
16231622
(
16241623
len(fixturedef.baseid),
16251624
fixturedef.func.__module__,
1626-
_pretty_fixture_path(fixturedef.func),
1625+
_pretty_fixture_path(invocation_dir, fixturedef.func),
16271626
fixturedef.argname,
16281627
fixturedef,
16291628
)

0 commit comments

Comments
 (0)