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.
This commit is contained in:
parent
676f38d04a
commit
a6dd90a414
|
@ -1525,14 +1525,13 @@ def _ascii_escaped_by_config(val: Union[str, bytes], config: Optional[Config]) -
|
||||||
return val if escape_option else ascii_escaped(val) # type: ignore
|
return val if escape_option else ascii_escaped(val) # type: ignore
|
||||||
|
|
||||||
|
|
||||||
def _pretty_fixture_path(func) -> str:
|
def _pretty_fixture_path(invocation_dir: Path, func) -> str:
|
||||||
cwd = Path.cwd()
|
loc = Path(getlocation(func, invocation_dir))
|
||||||
loc = Path(getlocation(func, str(cwd)))
|
|
||||||
prefix = Path("...", "_pytest")
|
prefix = Path("...", "_pytest")
|
||||||
try:
|
try:
|
||||||
return str(prefix / loc.relative_to(_PYTEST_DIR))
|
return str(prefix / loc.relative_to(_PYTEST_DIR))
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return bestrelpath(cwd, loc)
|
return bestrelpath(invocation_dir, loc)
|
||||||
|
|
||||||
|
|
||||||
def show_fixtures_per_test(config):
|
def show_fixtures_per_test(config):
|
||||||
|
@ -1545,19 +1544,19 @@ def _show_fixtures_per_test(config: Config, session: Session) -> None:
|
||||||
import _pytest.config
|
import _pytest.config
|
||||||
|
|
||||||
session.perform_collect()
|
session.perform_collect()
|
||||||
curdir = Path.cwd()
|
invocation_dir = config.invocation_params.dir
|
||||||
tw = _pytest.config.create_terminal_writer(config)
|
tw = _pytest.config.create_terminal_writer(config)
|
||||||
verbose = config.getvalue("verbose")
|
verbose = config.getvalue("verbose")
|
||||||
|
|
||||||
def get_best_relpath(func) -> str:
|
def get_best_relpath(func) -> str:
|
||||||
loc = getlocation(func, str(curdir))
|
loc = getlocation(func, invocation_dir)
|
||||||
return bestrelpath(curdir, Path(loc))
|
return bestrelpath(invocation_dir, Path(loc))
|
||||||
|
|
||||||
def write_fixture(fixture_def: fixtures.FixtureDef[object]) -> None:
|
def write_fixture(fixture_def: fixtures.FixtureDef[object]) -> None:
|
||||||
argname = fixture_def.argname
|
argname = fixture_def.argname
|
||||||
if verbose <= 0 and argname.startswith("_"):
|
if verbose <= 0 and argname.startswith("_"):
|
||||||
return
|
return
|
||||||
prettypath = _pretty_fixture_path(fixture_def.func)
|
prettypath = _pretty_fixture_path(invocation_dir, fixture_def.func)
|
||||||
tw.write(f"{argname}", green=True)
|
tw.write(f"{argname}", green=True)
|
||||||
tw.write(f" -- {prettypath}", yellow=True)
|
tw.write(f" -- {prettypath}", yellow=True)
|
||||||
tw.write("\n")
|
tw.write("\n")
|
||||||
|
@ -1601,7 +1600,7 @@ def _showfixtures_main(config: Config, session: Session) -> None:
|
||||||
import _pytest.config
|
import _pytest.config
|
||||||
|
|
||||||
session.perform_collect()
|
session.perform_collect()
|
||||||
curdir = Path.cwd()
|
invocation_dir = config.invocation_params.dir
|
||||||
tw = _pytest.config.create_terminal_writer(config)
|
tw = _pytest.config.create_terminal_writer(config)
|
||||||
verbose = config.getvalue("verbose")
|
verbose = config.getvalue("verbose")
|
||||||
|
|
||||||
|
@ -1615,7 +1614,7 @@ def _showfixtures_main(config: Config, session: Session) -> None:
|
||||||
if not fixturedefs:
|
if not fixturedefs:
|
||||||
continue
|
continue
|
||||||
for fixturedef in fixturedefs:
|
for fixturedef in fixturedefs:
|
||||||
loc = getlocation(fixturedef.func, str(curdir))
|
loc = getlocation(fixturedef.func, invocation_dir)
|
||||||
if (fixturedef.argname, loc) in seen:
|
if (fixturedef.argname, loc) in seen:
|
||||||
continue
|
continue
|
||||||
seen.add((fixturedef.argname, loc))
|
seen.add((fixturedef.argname, loc))
|
||||||
|
@ -1623,7 +1622,7 @@ def _showfixtures_main(config: Config, session: Session) -> None:
|
||||||
(
|
(
|
||||||
len(fixturedef.baseid),
|
len(fixturedef.baseid),
|
||||||
fixturedef.func.__module__,
|
fixturedef.func.__module__,
|
||||||
_pretty_fixture_path(fixturedef.func),
|
_pretty_fixture_path(invocation_dir, fixturedef.func),
|
||||||
fixturedef.argname,
|
fixturedef.argname,
|
||||||
fixturedef,
|
fixturedef,
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue