-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test pytest-copie on a demo template repo as part of test suite (#54)
- Loading branch information
1 parent
aebafc7
commit 2980669
Showing
6 changed files
with
89 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{{ name }} | ||
=============== | ||
|
||
{{ short_description }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters