diff --git a/pytest_copie/plugin.py b/pytest_copie/plugin.py index e82d7ea..aed0812 100644 --- a/pytest_copie/plugin.py +++ b/pytest_copie/plugin.py @@ -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 @@ -76,13 +77,12 @@ def copie(self, extra_context: dict = {}, template: Any = None) -> Result: exception: Union[None, SystemExit, Exception] = None exit_code: Union[str, int, None] = 0 project_path = None - context_out = {} + answers = {} template_dir = template or self.default_template context_file = template_dir / "copier.yaml" output_dir = self._new_output_dir() - print(f"output_dir: {output_dir.resolve()}") try: # write the answers in the destination folder so they are used by the worker @@ -100,16 +100,18 @@ def copie(self, extra_context: dict = {}, template: Any = None) -> Result: # the project path will be the first child of the ouptut_dir project_path = next(d for d in worker.dst_path.glob("*") if d.is_dir()) - context_out = yaml.safe_load( - (project_path / ".copier-answers.yml").read_text() - ) + + # the context is not written as we don't answer questions in the tests. + # So we regenerate it directly from the worker + answers = worker._answers_to_remember() + answers = {k: v for k, v in answers.items() if not k.startswith("_")} except SystemExit as e: exception, exit_code = e, e.code except Exception as e: exception, exit_code = e, -1 - return Result(exception, exit_code, project_path, context_out) + return Result(exception, exit_code, project_path, answers) @pytest.fixture @@ -160,8 +162,8 @@ def output_factory(dirname: str) -> Path: yield Copie(template_dir, output_factory, _copier_config_file) # delete the files if necessary - # if not request.config.option.keep_copied_projects: - # rmtree(output_dir) + if not request.config.option.keep_copied_projects: + rmtree(output_dir) def pytest_addoption(parser): diff --git a/tests/test_copie.py b/tests/test_copie.py index bbb0faa..cd21611 100644 --- a/tests/test_copie.py +++ b/tests/test_copie.py @@ -22,6 +22,7 @@ def test_copie_copie(testdir, copier_template, test_check): """Programmatically create a **Copier** template and use `copie` to create a project from it.""" testdir.makepyfile( """ + from pathlib import Path def test_copie_project(copie): result = copie.copie(extra_context={"repo_name": "helloworld"}) @@ -119,7 +120,7 @@ def test_copie_result_context(testdir, copier_template, test_check): def test_copie_project(copie): result = copie.copie(extra_context={ "repo_name": "cookies", - "short_description": "{{repo_name}} is awesome", + "short_description": "copie is awesome", }) assert result.exit_code == 0 @@ -129,7 +130,7 @@ def test_copie_project(copie): assert result.context == { "repo_name": "cookies", - "short_description": "cookies is awesome", + "short_description": "copie is awesome", } assert str(result) == f"" """