| 1 |
# SPDX-FileCopyrightText: 2021-present Ofek Lev <[email protected]> |
| 2 |
# |
| 3 |
# SPDX-License-Identifier: MIT |
| 4 |
import os |
| 5 |
import shutil |
| 6 |
import sys |
| 7 |
from pathlib import Path |
| 8 |
from tempfile import TemporaryDirectory |
| 9 |
from typing import Generator |
| 10 |
|
| 11 |
import pytest |
| 12 |
|
| 13 |
|
| 14 |
@pytest.fixture(scope='session') |
| 15 |
def compiled_extension() -> str: |
| 16 |
return '.pyd' if sys.platform == 'win32' else '.so' |
| 17 |
|
| 18 |
|
| 19 |
@pytest.fixture(scope='session') |
| 20 |
def plugin_dir() -> Generator[Path, None, None]: |
| 21 |
with TemporaryDirectory() as d: |
| 22 |
directory = Path(d, 'plugin') |
| 23 |
shutil.copytree(Path.cwd(), directory, ignore=shutil.ignore_patterns('.git')) |
| 24 |
|
| 25 |
yield directory.resolve() |
| 26 |
|
| 27 |
|
| 28 |
@pytest.fixture |
| 29 |
def new_project(plugin_dir, compiled_extension, tmp_path) -> Generator[Path, None, None]: |
| 30 |
project_dir = tmp_path / 'my-app' |
| 31 |
project_dir.mkdir() |
| 32 |
|
| 33 |
gitignore_file = project_dir / '.gitignore' |
| 34 |
gitignore_file.write_text(f'*{compiled_extension}', encoding='utf-8') |
| 35 |
|
| 36 |
project_file = project_dir / 'pyproject.toml' |
| 37 |
project_file.write_text( |
| 38 |
f"""\ |
| 39 |
[build-system] |
| 40 |
requires = ["hatchling", "hatch-mypyc @ {plugin_dir.as_uri()}"] |
| 41 |
build-backend = "hatchling.build" |
| 42 |
|
| 43 |
[project] |
| 44 |
name = "my-app" |
| 45 |
dependencies = [] |
| 46 |
dynamic = ["version"] |
| 47 |
|
| 48 |
[tool.hatch.version] |
| 49 |
path = "my_app/__init__.py" |
| 50 |
|
| 51 |
[tool.hatch.build.targets.wheel] |
| 52 |
macos-max-compat = false |
| 53 |
|
| 54 |
[tool.hatch.build.targets.wheel.hooks.mypyc] |
| 55 |
""", |
| 56 |
encoding='utf-8', |
| 57 |
) |
| 58 |
|
| 59 |
package_dir = project_dir / 'my_app' |
| 60 |
package_dir.mkdir() |
| 61 |
|
| 62 |
package_root = package_dir / '__init__.py' |
| 63 |
package_root.write_text('__version__ = "1.2.3"', encoding='utf-8') |
| 64 |
|
| 65 |
fibonacci_file = package_dir / 'fib.py' |
| 66 |
fibonacci_file.write_text( |
| 67 |
"""\ |
| 68 |
def fib(n: int) -> int: |
| 69 |
if n <= 1: |
| 70 |
return n |
| 71 |
else: |
| 72 |
return fib(n - 2) + fib(n - 1) |
| 73 |
""", |
| 74 |
encoding='utf-8', |
| 75 |
) |
| 76 |
|
| 77 |
package_data_file = package_dir / 'driver.yaml' |
| 78 |
package_data_file.write_text('apiVersion: storage.k8s.io/v1', encoding='utf-8') |
| 79 |
|
| 80 |
origin = os.getcwd() |
| 81 |
os.chdir(project_dir) |
| 82 |
try: |
| 83 |
yield project_dir |
| 84 |
finally: |
| 85 |
os.chdir(origin) |