Skip to content
Closed
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 AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ Harald Armin Massa
Harshna
Henk-Jaap Wagenaar
Henry Schreiner
Himanshu
Holger Kohr
Hugo van Kemenade
Hui Wang (coldnight)
Expand Down
1 change: 1 addition & 0 deletions changelog/15217.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed test collection for Python files whose names contain square brackets.
16 changes: 12 additions & 4 deletions src/_pytest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1152,11 +1152,19 @@ 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 = arg.partition("[")
strpath, *parts = base.split("::")
if squacket and not parts:
selection_start = arg.find("::")
strpath = arg if selection_start == -1 else arg[:selection_start]
if "[" in strpath and not safe_exists(absolutepath(invocation_path / strpath)):
raise UsageError(f"path cannot contain [] parametrization: {arg}")
parametrization = f"{squacket}{rest}" if squacket else None

path_and_parts = arg
parametrization = None
if selection_start != -1:
parametrization_start = arg.find("[", selection_start + 2)
if parametrization_start != -1:
path_and_parts = arg[:parametrization_start]
parametrization = arg[parametrization_start:]
strpath, *parts = path_and_parts.split("::")
module_name = None
if as_pypath:
pyarg_strpath = search_pypath(
Expand Down
17 changes: 17 additions & 0 deletions testing/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,23 @@ def test_file(self, invocation_path: Path) -> None:
original_index=40,
)

def test_file_with_brackets_in_name(
self, invocation_path: Path, pytester: Pytester
) -> None:
path = invocation_path / "src/pkg/test_[helper].py"
path.write_text("def test_example(): pass\n", encoding="utf-8")

assert resolve_collection_argument(invocation_path, str(path), 0) == (
CollectionArgument(
path=path,
parts=[],
parametrization=None,
module_name=None,
original_index=0,
)
)
pytester.runpytest(str(path)).assert_outcomes(passed=1)

def test_dir(self, invocation_path: Path) -> None:
"""Directory and parts."""
assert resolve_collection_argument(
Expand Down