Copy file from project's directory into the testdir. :param name: The name of the file to copy. :return: Path to the copied directory (inside ``self.path``). :rtype: pathlib.Path
(self, name: str | None = None)
| 945 | return p |
| 946 | |
| 947 | def copy_example(self, name: str | None = None) -> Path: |
| 948 | """Copy file from project's directory into the testdir. |
| 949 | |
| 950 | :param name: |
| 951 | The name of the file to copy. |
| 952 | :return: |
| 953 | Path to the copied directory (inside ``self.path``). |
| 954 | :rtype: pathlib.Path |
| 955 | """ |
| 956 | example_dir_ = self._request.config.getini("pytester_example_dir") |
| 957 | if example_dir_ is None: |
| 958 | raise ValueError("pytester_example_dir is unset, can't copy examples") |
| 959 | example_dir: Path = self._request.config.rootpath / example_dir_ |
| 960 | |
| 961 | for extra_element in self._request.node.iter_markers("pytester_example_path"): |
| 962 | assert extra_element.args |
| 963 | example_dir = example_dir.joinpath(*extra_element.args) |
| 964 | |
| 965 | if name is None: |
| 966 | func_name = self._name |
| 967 | maybe_dir = example_dir / func_name |
| 968 | maybe_file = example_dir / (func_name + ".py") |
| 969 | |
| 970 | if maybe_dir.is_dir(): |
| 971 | example_path = maybe_dir |
| 972 | elif maybe_file.is_file(): |
| 973 | example_path = maybe_file |
| 974 | else: |
| 975 | raise LookupError( |
| 976 | f"{func_name} can't be found as module or package in {example_dir}" |
| 977 | ) |
| 978 | else: |
| 979 | example_path = example_dir.joinpath(name) |
| 980 | |
| 981 | if example_path.is_dir() and not example_path.joinpath("__init__.py").is_file(): |
| 982 | shutil.copytree(example_path, self.path, symlinks=True, dirs_exist_ok=True) |
| 983 | return self.path |
| 984 | elif example_path.is_file(): |
| 985 | result = self.path.joinpath(example_path.name) |
| 986 | shutil.copy(example_path, result) |
| 987 | return result |
| 988 | else: |
| 989 | raise LookupError( |
| 990 | f'example "{example_path}" is not found as a file or directory' |
| 991 | ) |
| 992 | |
| 993 | def getnode(self, config: Config, arg: str | os.PathLike[str]) -> Collector | Item: |
| 994 | """Get the collection node of a file. |
no test coverage detected