Skip to content

Commit

Permalink
feat: add option to keep the files in directory" (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
12rambau authored Sep 6, 2023
1 parent 57ad1d2 commit 5595e6f
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
18 changes: 16 additions & 2 deletions pytest_copie/plugin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""A pytest plugin to build copier project from a template."""

from pathlib import Path
from shutil import rmtree
from typing import Any, Callable, Generator, Optional, Union

import pytest
Expand Down Expand Up @@ -155,12 +156,16 @@ def output_factory(dirname: str) -> Path:

yield Copie(template_dir, output_factory, _copier_config_file)

# add an option to destroy the resulting file after the test
# delete the files if necessary
if not request.config.option.keep_copied_projects:
rmtree(output_dir)


def pytest_addoption(parser):
"""Add the `--template` option to the pytest command."""
"""Add option to the pytest command."""
group = parser.getgroup("copie")

# --template option
group.addoption(
"--template",
action="store",
Expand All @@ -170,6 +175,15 @@ def pytest_addoption(parser):
type=str,
)

# --keep-copied-projects option
group.addoption(
"--keep-copied-projects",
action="store_true",
default=False,
dest="keep_copied_projects",
help="Keep projects directories generated with 'copie.copie()'.",
)


def pytest_configure(config):
"""Force the template path to be absolute to protect ourselves from feature that changes path."""
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def copier_template(tmpdir) -> Path:
]

# create all the folders and files
(template_dir := tmpdir / "copie-template").mkdir()
(template_dir := Path(tmpdir) / "copie-template").mkdir()
(template_dir / "copier.yaml").write_text(yaml.dump(template_config), "utf-8")
(repo_dir := template_dir / r"{{ repo_name }}").mkdir()
(repo_dir / "README.rst").write_text("\n".join(template_readme), "utf-8")
Expand Down
46 changes: 45 additions & 1 deletion tests/test_copie.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def test_copie_copie_with_template_kwarg(testdir, copier_template, test_check):
def test_copie_project(copie):
result = copie.copie(
extra_context={"repo_name": "helloworld"},
template=Path("%s"),
template=Path(r"%s"),
)
assert result.exit_code == 0
Expand All @@ -66,3 +66,47 @@ def test_copie_project(copie):
# run pytest without the template cli arg
result = testdir.runpytest("-v")
test_check(result, "test_copie_project")


def test_copie_fixture_removes_directories(testdir, copier_template, test_check):
"""Check the copie fixture removes the output directories from one test to another."""
testdir.makepyfile(
"""
from pathlib import Path
def test_create_result(copie):
result = copie.copie()
globals().update(result_path = result.project_path.parent)
assert result.exception is None
def test_previous_directory_is_removed(copie):
assert result_path.is_dir() is False
"""
)

result = testdir.runpytest("-v", f"--template={copier_template}")
test_check(result, "test_create_result")
test_check(result, "test_previous_directory_is_removed")


def test_copie_fixture_keeps_directories(testdir, copier_template, test_check):
"""Check the copie fixture keeps the output directories from one test to another."""
testdir.makepyfile(
"""
from pathlib import Path
def test_create_result(copie):
result = copie.copie()
globals().update(result_path = result.project_path.parent)
assert result.exception is None
def test_previous_directory_is_kept(copie):
assert result_path.is_dir() is True
"""
)

result = testdir.runpytest(
"-v", f"--template={copier_template}", "--keep-copied-projects"
)
test_check(result, "test_create_result")
test_check(result, "test_previous_directory_is_kept")

0 comments on commit 5595e6f

Please sign in to comment.