Search in the list of arguments for a valid ini-file for pytest, and return a tuple of (rootdir, inifile, cfg-dict, ignored-config-files), where ignored-config-files is a list of config basenames found that contain pytest configuration but were ignored.
(
invocation_dir: Path,
args: Iterable[Path],
)
| 156 | |
| 157 | |
| 158 | def locate_config( |
| 159 | invocation_dir: Path, |
| 160 | args: Iterable[Path], |
| 161 | ) -> tuple[Path | None, Path | None, ConfigDict, Sequence[str]]: |
| 162 | """Search in the list of arguments for a valid ini-file for pytest, |
| 163 | and return a tuple of (rootdir, inifile, cfg-dict, ignored-config-files), where |
| 164 | ignored-config-files is a list of config basenames found that contain |
| 165 | pytest configuration but were ignored.""" |
| 166 | config_names = [ |
| 167 | "pytest.toml", |
| 168 | ".pytest.toml", |
| 169 | "pytest.ini", |
| 170 | ".pytest.ini", |
| 171 | "pyproject.toml", |
| 172 | "tox.ini", |
| 173 | "setup.cfg", |
| 174 | ] |
| 175 | args = [x for x in args if not str(x).startswith("-")] |
| 176 | if not args: |
| 177 | args = [invocation_dir] |
| 178 | found_pyproject_toml: Path | None = None |
| 179 | ignored_config_files: list[str] = [] |
| 180 | |
| 181 | for arg in args: |
| 182 | argpath = absolutepath(arg) |
| 183 | for base in (argpath, *argpath.parents): |
| 184 | for config_name in config_names: |
| 185 | p = base / config_name |
| 186 | if p.is_file(): |
| 187 | if p.name == "pyproject.toml" and found_pyproject_toml is None: |
| 188 | found_pyproject_toml = p |
| 189 | ini_config = load_config_dict_from_file(p) |
| 190 | if ini_config is not None: |
| 191 | index = config_names.index(config_name) |
| 192 | for remainder in config_names[index + 1 :]: |
| 193 | p2 = base / remainder |
| 194 | if ( |
| 195 | p2.is_file() |
| 196 | and load_config_dict_from_file(p2) is not None |
| 197 | ): |
| 198 | ignored_config_files.append(remainder) |
| 199 | return base, p, ini_config, ignored_config_files |
| 200 | if found_pyproject_toml is not None: |
| 201 | return found_pyproject_toml.parent, found_pyproject_toml, {}, [] |
| 202 | return None, None, {}, [] |
| 203 | |
| 204 | |
| 205 | def get_common_ancestor( |