nix-update/tests/conftest.py

46 lines
1.4 KiB
Python
Raw Normal View History

import os
2021-01-09 10:55:05 +03:00
import shutil
import subprocess
import sys
2021-01-09 10:55:05 +03:00
import tempfile
2023-08-25 09:53:31 +03:00
from collections.abc import Iterator
2021-01-09 10:55:05 +03:00
from contextlib import contextmanager
from pathlib import Path
2021-01-09 10:55:05 +03:00
import pytest
2021-01-09 10:55:05 +03:00
TEST_ROOT = Path(__file__).parent.resolve()
sys.path.append(str(TEST_ROOT.parent))
class Helpers:
@staticmethod
def root() -> Path:
return TEST_ROOT
@staticmethod
@contextmanager
def testpkgs(init_git: bool = False) -> Iterator[Path]:
2021-01-09 10:55:05 +03:00
with tempfile.TemporaryDirectory() as tmpdirname:
shutil.copytree(
Helpers.root().joinpath("testpkgs"), tmpdirname, dirs_exist_ok=True
)
if init_git:
os.environ["GIT_AUTHOR_NAME"] = "nix-update"
os.environ["GIT_AUTHOR_EMAIL"] = "nix-update@example.com"
os.environ["GIT_COMMITTER_NAME"] = "nix-update"
os.environ["GIT_COMMITTER_EMAIL"] = "nix-update@example.com"
subprocess.run(["git", "-C", tmpdirname, "init"], check=True)
subprocess.run(["git", "-C", tmpdirname, "add", "--all"], check=True)
subprocess.run(
["git", "-C", tmpdirname, "commit", "-m", "first commit"],
check=True,
)
2021-01-09 10:55:05 +03:00
yield Path(tmpdirname)
2021-03-25 13:02:40 +03:00
@pytest.fixture # type: ignore
2023-08-25 09:53:31 +03:00
def helpers() -> type[Helpers]:
2021-01-09 10:55:05 +03:00
return Helpers