Skip to content

Commit

Permalink
Consider paths and pathlists relative to cwd in case of an absent ini…
Browse files Browse the repository at this point in the history
…-file (#11963)

Previously this would trigger an `AssertionError`.

While it could be considered a bug-fix, but given it now can be relied upon, it is probably better to consider it an improvement.

Fix #11311
  • Loading branch information
nicoddemus committed Feb 14, 2024
1 parent fe7907d commit 46e6fb1
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
4 changes: 4 additions & 0 deletions changelog/11311.improvement.rst
@@ -0,0 +1,4 @@
When using ``--override-ini`` for paths in invocations without a configuration file defined, the current working directory is used
as the relative directory.

Previoulsy this would raise an :class:`AssertionError`.
8 changes: 5 additions & 3 deletions src/_pytest/config/__init__.py
Expand Up @@ -1563,9 +1563,11 @@ def _getini(self, name: str):
# in this case, we already have a list ready to use.
#
if type == "paths":
# TODO: This assert is probably not valid in all cases.
assert self.inipath is not None
dp = self.inipath.parent
dp = (
self.inipath.parent
if self.inipath is not None
else self.invocation_params.dir
)
input_values = shlex.split(value) if isinstance(value, str) else value
return [dp / x for x in input_values]
elif type == "args":
Expand Down
7 changes: 7 additions & 0 deletions src/_pytest/config/argparsing.py
Expand Up @@ -198,9 +198,16 @@ def addini(
* ``paths``: a list of :class:`pathlib.Path`, separated as in a shell
* ``pathlist``: a list of ``py.path``, separated as in a shell
For ``paths`` and ``pathlist`` types, they are considered relative to the ini-file.
In case the execution is happening without an ini-file defined,
they will be considered relative to the current working directory (for example with ``--override-ini``).
.. versionadded:: 7.0
The ``paths`` variable type.
.. versionadded:: 8.1
Use the current working directory to resolve ``paths`` and ``pathlist`` in the absence of an ini-file.
Defaults to ``string`` if ``None`` or not passed.
:param default:
Default value if no ini-file option exists but is queried.
Expand Down
12 changes: 12 additions & 0 deletions testing/test_config.py
Expand Up @@ -1874,6 +1874,18 @@ def test():
assert "ERROR:" not in result.stderr.str()
result.stdout.fnmatch_lines(["collected 1 item", "*= 1 passed in *="])

def test_override_ini_without_config_file(self, pytester: Pytester) -> None:
pytester.makepyfile(**{"src/override_ini_without_config_file.py": ""})
pytester.makepyfile(
**{
"tests/test_override_ini_without_config_file.py": (
"import override_ini_without_config_file\ndef test(): pass"
),
}
)
result = pytester.runpytest("--override-ini", "pythonpath=src")
assert result.parseoutcomes() == {"passed": 1}


def test_help_via_addopts(pytester: Pytester) -> None:
pytester.makeini(
Expand Down

0 comments on commit 46e6fb1

Please sign in to comment.