Skip to content

Commit d2d57e3

Browse files
authored
Merge pull request #13717 from bluetech/squaket
main: reject arguments like `pytest x.py[1]` instead of ignoring the `[]`
2 parents 6ab228b + bcd4f9b commit d2d57e3

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

changelog/13716.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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.

src/_pytest/main.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,9 +1066,11 @@ def resolve_collection_argument(
10661066
If the path doesn't exist, raise UsageError.
10671067
If the path is a directory and selection parts are present, raise UsageError.
10681068
"""
1069-
base, squacket, rest = str(arg).partition("[")
1069+
base, squacket, rest = arg.partition("[")
10701070
strpath, *parts = base.split("::")
1071-
if parts:
1071+
if squacket:
1072+
if not parts:
1073+
raise UsageError(f"path cannot contain [] parametrization: {arg}")
10721074
parts[-1] = f"{parts[-1]}{squacket}{rest}"
10731075
module_name = None
10741076
if as_pypath:

testing/test_main.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,20 @@ def test_parametrized_name_with_colons(self, invocation_path: Path) -> None:
220220
module_name=None,
221221
)
222222

223+
@pytest.mark.parametrize(
224+
"arg", ["x.py[a]", "x.py[a]::foo", "x/y.py[a]::foo::bar", "x.py[a]::foo[b]"]
225+
)
226+
def test_path_parametrization_not_allowed(
227+
self, invocation_path: Path, arg: str
228+
) -> None:
229+
with pytest.raises(
230+
UsageError, match=r"path cannot contain \[\] parametrization"
231+
):
232+
resolve_collection_argument(
233+
invocation_path,
234+
arg,
235+
)
236+
223237
def test_does_not_exist(self, invocation_path: Path) -> None:
224238
"""Given a file/module that does not exist raises UsageError."""
225239
with pytest.raises(

0 commit comments

Comments
 (0)