|
| 1 | +"""Fixtures used in all tests for cookiecutter-robust-python.""" |
| 2 | +import json |
| 3 | +import os |
| 4 | +from pathlib import Path |
| 5 | +from typing import Any, Generator |
| 6 | + |
| 7 | +import platformdirs |
| 8 | +import pytest |
| 9 | +from _pytest.fixtures import FixtureRequest |
| 10 | +from _pytest.tmpdir import TempPathFactory |
| 11 | +from cookiecutter.main import cookiecutter |
| 12 | + |
| 13 | + |
| 14 | +pytest_plugins: list[str] = ["pytester"] |
| 15 | + |
| 16 | + |
| 17 | +REPO_FOLDER: Path = Path(__file__).parent.parent |
| 18 | +COOKIECUTTER_FOLDER: Path = REPO_FOLDER / "{{cookiecutter.project_name}}" |
| 19 | +HOOKS_FOLDER: Path = REPO_FOLDER / "hooks" |
| 20 | +SCRIPTS_FOLDER: Path = REPO_FOLDER / "scripts" |
| 21 | + |
| 22 | +COOKIECUTTER_JSON_PATH: Path = COOKIECUTTER_FOLDER / "cookiecutter.json" |
| 23 | +COOKIECUTTER_JSON: dict[str, Any] = json.loads(COOKIECUTTER_JSON_PATH.read_text()) |
| 24 | + |
| 25 | + |
| 26 | +@pytest.fixture(scope="session") |
| 27 | +def robust_python_demo_path(tmp_path_factory: TempPathFactory) -> Path: |
| 28 | + """Creates a temporary example python project for testing against and returns its Path.""" |
| 29 | + demos_path: Path = tmp_path_factory.mktemp("demos") |
| 30 | + cookiecutter( |
| 31 | + str(REPO_FOLDER), |
| 32 | + no_input=True, |
| 33 | + overwrite_if_exists=True, |
| 34 | + output_dir=demos_path, |
| 35 | + extra_context={ |
| 36 | + "project_name": "robust-python-demo", |
| 37 | + "add_rust_extension": False |
| 38 | + } |
| 39 | + ) |
| 40 | + return demos_path / "robust-python-demo" |
| 41 | + |
| 42 | + |
| 43 | +@pytest.fixture(scope="session") |
| 44 | +def robust_maturin_demo_path(tmp_path_factory: TempPathFactory) -> Path: |
| 45 | + """Creates a temporary example maturin project for testing against and returns its Path.""" |
| 46 | + demos_path: Path = tmp_path_factory.mktemp("demos") |
| 47 | + cookiecutter( |
| 48 | + str(REPO_FOLDER), |
| 49 | + no_input=True, |
| 50 | + overwrite_if_exists=True, |
| 51 | + output_dir=demos_path, |
| 52 | + extra_context={ |
| 53 | + "project_name": "robust-maturin-demo", |
| 54 | + "add_rust_extension": True |
| 55 | + } |
| 56 | + ) |
| 57 | + return demos_path / "robust-maturin-demo" |
| 58 | + |
| 59 | + |
| 60 | +@pytest.fixture(scope="function") |
| 61 | +def inside_robust_python_demo(robust_python_demo_path: Path) -> Generator[Path, None, None]: |
| 62 | + """Changes the current working directory to the robust-python-demo project.""" |
| 63 | + original_path: Path = Path.cwd() |
| 64 | + os.chdir(robust_python_demo_path) |
| 65 | + yield robust_python_demo_path |
| 66 | + os.chdir(original_path) |
| 67 | + |
| 68 | + |
0 commit comments