Skip to content

Commit

Permalink
refactor(!include): Removed test class in favor of test functions
Browse files Browse the repository at this point in the history
Changes made based on code review feedback.
  • Loading branch information
davidbrownell committed Jun 25, 2024
1 parent f4ccc02 commit f61583c
Showing 1 changed file with 91 additions and 93 deletions.
184 changes: 91 additions & 93 deletions tests/test_copie.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,98 +210,96 @@ def test_valid_copier_config(_copier_config_file):
assert result.ret == 0


class TestInclude:
"""Tests the '!include' directive supported by copier."""

def test_working(self, testdir):
"""Validates that pytest-copie can handle the '!include' directive."""
(template_dir := Path(testdir.tmpdir) / "copie-template").mkdir()

(template_dir / "copier.yml").write_text(
textwrap.dedent(
"""\
test1: test1
---
!include other.yml
---
test2: test2
_subdirectory: project
""",
),
)

(template_dir / "other.yml").write_text(
textwrap.dedent(
"""\
test3: test3
""",
),
)

(repo_dir := template_dir / "project").mkdir()

(repo_dir / "README.rst.jinja").write_text(
textwrap.dedent(
"""\
test1: {{ test1 }}
test2: {{ test2 }}
test3: {{ test3 }}
""",
),
)

testdir.makepyfile(
"""
def test_copie_project(copie):
result = copie.copy()
assert result.exit_code == 0
assert result.exception is None
readme_file = result.project_dir / "README.rst"
assert readme_file.read_text() == "test1: test1\\ntest2: test2\\ntest3: test3\\n"
"""
)

result = testdir.runpytest("-v", f"--template={template_dir}")
assert result.ret == 0

def test_error_invalid_include_file(self, testdir):
"""Validates that pytest-copie raises an exception when the included file does not exist."""
(template_dir := Path(testdir.tmpdir) / "copie-template").mkdir()

(template_dir / "copier.yml").write_text(
textwrap.dedent(
"""\
test1: test1
---
!include file_does_not_exist.yml
---
test2: test2
_subdirectory: project
""",
),
)

invalid_filename = template_dir / "file_does_not_exist.yml"
invalid_filename_str = str(invalid_filename).replace("\\", "\\\\")

testdir.makepyfile(
f"""
def test_copie_project(copie):
result = copie.copy()
assert result.exit_code == -1
assert result.exception is not None
assert str(result.exception) == "The filename '{invalid_filename_str}' does not exist."
def test_copy_include_file(testdir):
"""Validates that pytest-copie can handle the '!include' directive."""
(template_dir := Path(testdir.tmpdir) / "copie-template").mkdir()

(template_dir / "copier.yml").write_text(
textwrap.dedent(
"""\
test1: test1
---
!include other.yml
---
test2: test2
_subdirectory: project
""",
),
)

(template_dir / "other.yml").write_text(
textwrap.dedent(
"""\
test3: test3
""",
),
)

(repo_dir := template_dir / "project").mkdir()

(repo_dir / "README.rst.jinja").write_text(
textwrap.dedent(
"""\
test1: {{ test1 }}
test2: {{ test2 }}
test3: {{ test3 }}
""",
)
),
)

testdir.makepyfile(
"""
def test_copie_project(copie):
result = copie.copy()
result = testdir.runpytest("-v", f"--template={template_dir}")
assert result.ret == 0
assert result.exit_code == 0
assert result.exception is None
readme_file = result.project_dir / "README.rst"
assert readme_file.read_text() == "test1: test1\\ntest2: test2\\ntest3: test3\\n"
"""
)

result = testdir.runpytest("-v", f"--template={template_dir}")
assert result.ret == 0


def test_copy_include_file_error_invalid_file(testdir):
"""Validates that pytest-copie raises an exception when the included file does not exist."""
(template_dir := Path(testdir.tmpdir) / "copie-template").mkdir()

(template_dir / "copier.yml").write_text(
textwrap.dedent(
"""\
test1: test1
---
!include file_does_not_exist.yml
---
test2: test2
_subdirectory: project
""",
),
)

invalid_filename = template_dir / "file_does_not_exist.yml"
invalid_filename_str = str(invalid_filename).replace("\\", "\\\\")

testdir.makepyfile(
f"""
def test_copie_project(copie):
result = copie.copy()
assert result.exit_code == -1
assert result.exception is not None
assert str(result.exception) == "The filename '{invalid_filename_str}' does not exist."
""",
)

result = testdir.runpytest("-v", f"--template={template_dir}")
assert result.ret == 0

0 comments on commit f61583c

Please sign in to comment.