Skip to content

Commit 46e6fb1

Browse files
authored
Consider paths and pathlists relative to cwd in case of an absent ini-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
1 parent fe7907d commit 46e6fb1

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

changelog/11311.improvement.rst

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
When using ``--override-ini`` for paths in invocations without a configuration file defined, the current working directory is used
2+
as the relative directory.
3+
4+
Previoulsy this would raise an :class:`AssertionError`.

src/_pytest/config/__init__.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -1563,9 +1563,11 @@ def _getini(self, name: str):
15631563
# in this case, we already have a list ready to use.
15641564
#
15651565
if type == "paths":
1566-
# TODO: This assert is probably not valid in all cases.
1567-
assert self.inipath is not None
1568-
dp = self.inipath.parent
1566+
dp = (
1567+
self.inipath.parent
1568+
if self.inipath is not None
1569+
else self.invocation_params.dir
1570+
)
15691571
input_values = shlex.split(value) if isinstance(value, str) else value
15701572
return [dp / x for x in input_values]
15711573
elif type == "args":

src/_pytest/config/argparsing.py

+7
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,16 @@ def addini(
198198
* ``paths``: a list of :class:`pathlib.Path`, separated as in a shell
199199
* ``pathlist``: a list of ``py.path``, separated as in a shell
200200
201+
For ``paths`` and ``pathlist`` types, they are considered relative to the ini-file.
202+
In case the execution is happening without an ini-file defined,
203+
they will be considered relative to the current working directory (for example with ``--override-ini``).
204+
201205
.. versionadded:: 7.0
202206
The ``paths`` variable type.
203207
208+
.. versionadded:: 8.1
209+
Use the current working directory to resolve ``paths`` and ``pathlist`` in the absence of an ini-file.
210+
204211
Defaults to ``string`` if ``None`` or not passed.
205212
:param default:
206213
Default value if no ini-file option exists but is queried.

testing/test_config.py

+12
Original file line numberDiff line numberDiff line change
@@ -1874,6 +1874,18 @@ def test():
18741874
assert "ERROR:" not in result.stderr.str()
18751875
result.stdout.fnmatch_lines(["collected 1 item", "*= 1 passed in *="])
18761876

1877+
def test_override_ini_without_config_file(self, pytester: Pytester) -> None:
1878+
pytester.makepyfile(**{"src/override_ini_without_config_file.py": ""})
1879+
pytester.makepyfile(
1880+
**{
1881+
"tests/test_override_ini_without_config_file.py": (
1882+
"import override_ini_without_config_file\ndef test(): pass"
1883+
),
1884+
}
1885+
)
1886+
result = pytester.runpytest("--override-ini", "pythonpath=src")
1887+
assert result.parseoutcomes() == {"passed": 1}
1888+
18771889

18781890
def test_help_via_addopts(pytester: Pytester) -> None:
18791891
pytester.makeini(

0 commit comments

Comments
 (0)