Parse path arguments optionally containing selection parts and return (fspath, names). Command-line arguments can point to files and/or directories, and optionally contain parts for specific tests selection, for example: "pkg/tests/test_foo.py::TestClass::test_foo" This functi
(
invocation_path: Path,
arg: str,
arg_index: int,
*,
as_pypath: bool = False,
consider_namespace_packages: bool = False,
)
| 1085 | |
| 1086 | |
| 1087 | def resolve_collection_argument( |
| 1088 | invocation_path: Path, |
| 1089 | arg: str, |
| 1090 | arg_index: int, |
| 1091 | *, |
| 1092 | as_pypath: bool = False, |
| 1093 | consider_namespace_packages: bool = False, |
| 1094 | ) -> CollectionArgument: |
| 1095 | """Parse path arguments optionally containing selection parts and return (fspath, names). |
| 1096 | |
| 1097 | Command-line arguments can point to files and/or directories, and optionally contain |
| 1098 | parts for specific tests selection, for example: |
| 1099 | |
| 1100 | "pkg/tests/test_foo.py::TestClass::test_foo" |
| 1101 | |
| 1102 | This function ensures the path exists, and returns a resolved `CollectionArgument`: |
| 1103 | |
| 1104 | CollectionArgument( |
| 1105 | path=Path("/full/path/to/pkg/tests/test_foo.py"), |
| 1106 | parts=["TestClass", "test_foo"], |
| 1107 | module_name=None, |
| 1108 | ) |
| 1109 | |
| 1110 | When as_pypath is True, expects that the command-line argument actually contains |
| 1111 | module paths instead of file-system paths: |
| 1112 | |
| 1113 | "pkg.tests.test_foo::TestClass::test_foo[a,b]" |
| 1114 | |
| 1115 | In which case we search sys.path for a matching module, and then return the *path* to the |
| 1116 | found module, which may look like this: |
| 1117 | |
| 1118 | CollectionArgument( |
| 1119 | path=Path("/home/u/myvenv/lib/site-packages/pkg/tests/test_foo.py"), |
| 1120 | parts=["TestClass", "test_foo"], |
| 1121 | parametrization="[a,b]", |
| 1122 | module_name="pkg.tests.test_foo", |
| 1123 | ) |
| 1124 | |
| 1125 | If the path doesn't exist, raise UsageError. |
| 1126 | If the path is a directory and selection parts are present, raise UsageError. |
| 1127 | """ |
| 1128 | base, squacket, rest = arg.partition("[") |
| 1129 | strpath, *parts = base.split("::") |
| 1130 | if squacket and not parts: |
| 1131 | raise UsageError(f"path cannot contain [] parametrization: {arg}") |
| 1132 | parametrization = f"{squacket}{rest}" if squacket else None |
| 1133 | module_name = None |
| 1134 | if as_pypath: |
| 1135 | pyarg_strpath = search_pypath( |
| 1136 | strpath, consider_namespace_packages=consider_namespace_packages |
| 1137 | ) |
| 1138 | if pyarg_strpath is not None: |
| 1139 | module_name = strpath |
| 1140 | strpath = pyarg_strpath |
| 1141 | fspath = invocation_path / strpath |
| 1142 | fspath = absolutepath(fspath) |
| 1143 | if not safe_exists(fspath): |
| 1144 | msg = ( |