Skip to content

Commit

Permalink
Test pytest-copie on a demo template repo as part of test suite (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
GenevieveBuckley authored Nov 13, 2023
1 parent aebafc7 commit 2980669
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 1 deletion.
5 changes: 5 additions & 0 deletions demo_template/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Demo template
=============

A demo copier template, including tests with pytest-copie.
For more details, see `docs/usage.rst`.
7 changes: 7 additions & 0 deletions demo_template/copier.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name:
type: str
default: foobar
short_description:
type: str
default: Test Project
_subdirectory: template
4 changes: 4 additions & 0 deletions demo_template/template/README.rst.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{{ name }}
===============

{{ short_description }}
34 changes: 34 additions & 0 deletions demo_template/tests/test_custom_template_fixture.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""Demo example of how to use pytest-copie with a custom template fixture."""
from pathlib import Path

import pytest
import yaml


@pytest.fixture
def custom_template(tmp_path) -> Path:
"""Generate a custom copier template to use as a pytest fixture."""
# Create custom copier template directory and copier.yaml file
(template := tmp_path / "copier-template").mkdir()
questions = {"custom_name": {"type": "str", "default": "my_default_name"}}
(template / "copier.yaml").write_text(yaml.dump(questions))
# Create custom subdirectory
(repo_dir := template / "custom_template").mkdir()
(template / "copier.yaml").write_text(
yaml.dump({"_subdirectory": "custom_template"})
)
# Create custom template text files
(repo_dir / "README.rst.jinja").write_text("{{custom_name}}\n")

return template


def test_copie_custom_project(copie, custom_template):
"""Test custom copier template fixture using pytest-copie."""
result = copie.copy(
template_dir=custom_template, extra_answers={"custom_name": "tutu"}
)

assert result.project_dir.is_dir()
with open(result.project_dir / "README.rst") as f:
assert f.readline() == "tutu\n"
38 changes: 38 additions & 0 deletions demo_template/tests/test_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""Demo example of how to test a copier template with pytest-copie."""
from pathlib import Path


def test_template(copie):
"""Test the demo copier template using pytest-copie."""
# This demo copier template is a subdirectory of the main repository
# so we have to specify the location for pytest.
# Users who have `copier.yaml` at the top level of their repository
# can delete the template_dir keyword argument here.
demo_template_dir = Path(__file__).parent.parent

result = copie.copy(template_dir=demo_template_dir)

assert result.exit_code == 0
assert result.exception is None
assert result.project_dir.is_dir()
with open(result.project_dir / "README.rst") as f:
assert f.readline() == "foobar\n"


def test_template_with_extra_answers(copie):
"""Test the demo copier template with extra answers using pytest-copie."""
# This demo copier template is a subdirectory of the main repository
# so we have to specify the location for pytest.
# Users who have `copier.yaml` at the top level of their repository
# can delete the template_dir keyword argument here.
demo_template_dir = Path(__file__).parent.parent

result = copie.copy(
extra_answers={"name": "helloworld"}, template_dir=demo_template_dir
)

assert result.exit_code == 0
assert result.exception is None
assert result.project_dir.is_dir()
with open(result.project_dir / "README.rst") as f:
assert f.readline() == "helloworld\n"
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ version_files = [
]

[tool.pytest.ini_options]
testpaths = "tests"
testpaths = ["tests", "demo_template"]

[tool.ruff]
ignore-init-module-imports = true
Expand Down

0 comments on commit 2980669

Please sign in to comment.