Skip to content
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
1 change: 1 addition & 0 deletions changelog/13716.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed a bug where a nonsensical invocation like ``pytest x.py[a]`` (a file cannot be parametrized) was silently treated as ``pytest x.py``. This is now a usage error.
6 changes: 4 additions & 2 deletions src/_pytest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1066,9 +1066,11 @@ def resolve_collection_argument(
If the path doesn't exist, raise UsageError.
If the path is a directory and selection parts are present, raise UsageError.
"""
base, squacket, rest = str(arg).partition("[")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is unrelated, but arg is already an str, I snuck it in.

base, squacket, rest = arg.partition("[")
strpath, *parts = base.split("::")
if parts:
if squacket:
if not parts:
raise UsageError(f"path cannot contain [] parametrization: {arg}")
parts[-1] = f"{parts[-1]}{squacket}{rest}"
module_name = None
if as_pypath:
Expand Down
14 changes: 14 additions & 0 deletions testing/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,20 @@ def test_parametrized_name_with_colons(self, invocation_path: Path) -> None:
module_name=None,
)

@pytest.mark.parametrize(
"arg", ["x.py[a]", "x.py[a]::foo", "x/y.py[a]::foo::bar", "x.py[a]::foo[b]"]
)
def test_path_parametrization_not_allowed(
self, invocation_path: Path, arg: str
) -> None:
with pytest.raises(
UsageError, match=r"path cannot contain \[\] parametrization"
):
resolve_collection_argument(
invocation_path,
arg,
)

def test_does_not_exist(self, invocation_path: Path) -> None:
"""Given a file/module that does not exist raises UsageError."""
with pytest.raises(
Expand Down
Loading