From 7d192afeaf68061beb3c61bc707c195f3f7357dc Mon Sep 17 00:00:00 2001 From: Abdelrahman Elbehery Date: Mon, 5 Sep 2022 21:39:18 +0200 Subject: [PATCH] fix for #10273 resolve_collection_argument no longer return an empty string when selection separator is specified to test name --- src/_pytest/main.py | 14 ++++++++------ testing/test_main.py | 8 +++++++- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/_pytest/main.py b/src/_pytest/main.py index 61fb7eaa4..2a8b06fce 100644 --- a/src/_pytest/main.py +++ b/src/_pytest/main.py @@ -877,14 +877,16 @@ 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("[") - strpath, *parts = base.split("::") - if parts: + strpath, selection, rest = arg.partition("::") + test_name, squacket, rest = rest.partition("[") + parts = [] + + if test_name: + parts = test_name.split("::") parts[-1] = f"{parts[-1]}{squacket}{rest}" if as_pypath: strpath = search_pypath(strpath) - fspath = invocation_path / strpath - fspath = absolutepath(fspath) + fspath = absolutepath(invocation_path / strpath) if not fspath.exists(): msg = ( "module or package not found: {arg} (missing __init__.py?)" @@ -892,7 +894,7 @@ def resolve_collection_argument( else "file or directory not found: {arg}" ) raise UsageError(msg.format(arg=arg)) - if parts and fspath.is_dir(): + if selection and fspath.is_dir(): msg = ( "package argument cannot contain :: selection parts: {arg}" if as_pypath diff --git a/testing/test_main.py b/testing/test_main.py index 715976267..7a40b8262 100644 --- a/testing/test_main.py +++ b/testing/test_main.py @@ -142,7 +142,7 @@ class TestResolveCollectionArgument: ) assert resolve_collection_argument(invocation_path, "src/pkg/test.py::") == ( invocation_path / "src/pkg/test.py", - [""], + [], ) assert resolve_collection_argument( invocation_path, "src/pkg/test.py::foo::bar" @@ -201,6 +201,12 @@ class TestResolveCollectionArgument: ): resolve_collection_argument(invocation_path, "foobar") + with pytest.raises( + UsageError, + match=re.escape("file or directory not found: src/pkg/test.py[a::b]"), + ): + resolve_collection_argument(invocation_path, "src/pkg/test.py[a::b]") + with pytest.raises( UsageError, match=re.escape(