Skip to content

Commit

Permalink
Merge pull request #60 from sneakers-the-rat/dedupe-tests
Browse files Browse the repository at this point in the history
Unitize/dedupe tests
  • Loading branch information
sneakers-the-rat authored Nov 1, 2024
2 parents 4e7ac2b + 693f392 commit 8c1f1c1
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 73 deletions.
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

[tool.pytest.ini_options]
testpaths = ["tests"]
markers = [
"docs: Tests relating to generated documentation!",
"installs: Tests that install packages, e.g. from executing hatch env scripts",
]

[tool.ruff]
line-length = 88
Expand Down
174 changes: 101 additions & 73 deletions tests/test_template_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import sys
from datetime import datetime, timezone
from pathlib import Path
from typing import Callable

import pytest
from copier import run_copy
Expand Down Expand Up @@ -58,15 +59,6 @@ def documentation(request: pytest.FixtureRequest) -> str:
return request.param


@pytest.fixture
def destination_path(
tmp_path_factory: pytest.TempPathFactory,
dev_platform: str,
) -> Path:
"""Provide a destination directory based on the development platform."""
return tmp_path_factory.mktemp("instance") / dev_platform


@pytest.fixture(scope="module")
def answers() -> dict[str, str]:
"""Provide a full project context."""
Expand All @@ -84,26 +76,52 @@ def answers() -> dict[str, str]:
}


@pytest.fixture
def generated(tmp_path: Path, answers: dict[str, str]) -> Callable[..., Path]:
"""Fixture closure to generate a template project, overriding passed data values."""
def _generated(**kwargs) -> Path:
run_copy(
src_path=str(TEMPLATE),
dst_path=tmp_path,
vcs_ref="HEAD",
data={
**answers,
**kwargs,
},
defaults=True,
)

init_git(tmp_path)

return tmp_path
return _generated


def init_git(path: Path):
"""Initialize a git repository such that hatch-vcs can be used."""
project_dir = path.resolve(strict=True)
repo = Repo.init(project_dir)
repo.index.add(
[
Path(dirpath, name)
for dirpath, _, filenames in os.walk(project_dir)
for name in filenames
],
)
repo.index.commit("chore: initialize template")


def test_init_template(
destination_path: Path,
answers: dict[str, str],
dev_platform: str,
license: str,
documentation: str,
generated: Callable[..., Path],
) -> None:
"""Expect that the template can be initialized with any provided license."""
parent = destination_path / license / answers["project_slug"]
run_copy(
src_path=str(TEMPLATE),
dst_path=parent,
vcs_ref="HEAD",
data={
**answers,
"dev_platform": dev_platform,
"license": license,
"documentation": documentation,
},
defaults=True,
"""Expect that the template can be initialized with all combinations of choices."""
parent = generated(
dev_platform=dev_platform,
license=license,
documentation=documentation,
)

project_files = {path.relative_to(parent) for path in parent.rglob("*")}
Expand All @@ -117,38 +135,12 @@ def test_init_template(
assert expected.issubset(project_files), expected.difference(project_files)


@pytest.mark.installs
def test_template_suite(
destination_path: Path,
dev_platform: str,
answers: dict[str, str],
documentation: str,
generated: Callable[..., Path],
) -> None:
"""Expect that the test suite passes for the initialized template."""
parent = destination_path / answers["project_slug"]
run_copy(
src_path=str(TEMPLATE),
dst_path=parent,
vcs_ref="HEAD",
data={
**answers,
"dev_platform": dev_platform,
"license": "MIT",
"documentation": documentation,
},
defaults=True,
)

# Initialize a git repository such that hatch-vcs can be used.
project_dir = parent.resolve(strict=True)
repo = Repo.init(project_dir)
repo.index.add(
[
Path(dirpath, name)
for dirpath, _, filenames in os.walk(project_dir)
for name in filenames
],
)
repo.index.commit("chore: initialize template")
project_dir = generated()

# Run the local test suite.
try:
Expand All @@ -164,13 +156,6 @@ def test_template_suite(
check=True,
shell=True,
)
if documentation:
subprocess.run(
"hatch run docs:build",
cwd=project_dir,
check=True,
shell=True,
)
subprocess.run(
"hatch run style:check",
cwd=project_dir,
Expand All @@ -183,23 +168,66 @@ def test_template_suite(
check=True,
shell=True,
)
subprocess.run(
"pre-commit run --all-files -v check-readthedocs",
cwd=project_dir,
check=True,
shell=True,
)
if dev_platform.lower() == "github":
subprocess.run(
"pre-commit run --all-files -v check-github-workflows",
cwd=project_dir,
check=True,
shell=True,
)

except subprocess.CalledProcessError as error:
logger.error( # noqa: TRY400
"Command = %r; Return code = %d.",
error.cmd,
error.returncode,
)
raise


@pytest.mark.docs
@pytest.mark.installs
def test_docs_build(documentation: str, generated: Callable[..., Path]):
"""The docs should build."""
if not documentation:
return

project = generated(documentation=documentation)

subprocess.run(
"hatch run docs:build",
cwd=project,
check=True,
shell=True,
)
subprocess.run(
"pre-commit run --all-files -v check-readthedocs",
cwd=project,
check=True,
shell=True,
)


@pytest.mark.installs
def test_dev_platform_github(generated: Callable[..., Path]):
"""Test github stuff idk!."""
project = generated(use_git=True, dev_platform="GitHub")

workflows_dir = project / ".github" / "workflows"
assert workflows_dir.exists()
workflows = list(workflows_dir.iterdir())
assert len(workflows) > 0
assert all(workflow.suffix in (".yml", ".yaml") for workflow in workflows)

subprocess.run(
"pre-commit run --all-files -v check-github-workflows",
cwd=project,
check=True,
shell=True,
)


@pytest.mark.installs
def test_dev_platform_gitlab(generated: Callable[..., Path]):
"""Test gitlab stuff idk!."""
project = generated(use_git=True, dev_platform="GitLab")

subprocess.run(
"pre-commit run --all-files -v check-gitlab-ci",
cwd=project,
check=True,
shell=True,
)

0 comments on commit 8c1f1c1

Please sign in to comment.