Skip to content

Consider paths and pathlists relative to cwd in case of an absent ini-file #11963

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changelog/11311.improvement.rst
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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