From 1c5b7366c73e8ce662126736683d53aeadc55ec4 Mon Sep 17 00:00:00 2001 From: Pierrick Rambaud Date: Wed, 6 Sep 2023 16:02:01 +0000 Subject: [PATCH 1/2] test: test the context result --- pytest_copie/plugin.py | 13 ++++++++----- tests/test_copie.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/pytest_copie/plugin.py b/pytest_copie/plugin.py index 3f6ee3f..e82d7ea 100644 --- a/pytest_copie/plugin.py +++ b/pytest_copie/plugin.py @@ -1,7 +1,6 @@ """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 @@ -77,7 +76,7 @@ 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 = {} + context_out = {} template_dir = template or self.default_template context_file = template_dir / "copier.yaml" @@ -96,17 +95,21 @@ def copie(self, extra_context: dict = {}, template: Any = None) -> Result: dst_path=str(output_dir), data=context, unsafe=True, + answers_file=".copier-answers.yml", ) # 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() + ) 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) + return Result(exception, exit_code, project_path, context_out) @pytest.fixture @@ -157,8 +160,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 e7af0d5..bbb0faa 100644 --- a/tests/test_copie.py +++ b/tests/test_copie.py @@ -110,3 +110,31 @@ def test_previous_directory_is_kept(copie): test_check(result, "test_create_result") test_check(result, "test_previous_directory_is_kept") assert result.ret == 0 + + +def test_copie_result_context(testdir, copier_template, test_check): + """Check that the result holds the rendered context.""" + testdir.makepyfile( + """ + def test_copie_project(copie): + result = copie.copie(extra_context={ + "repo_name": "cookies", + "short_description": "{{repo_name}} is awesome", + }) + + assert result.exit_code == 0 + assert result.exception is None + assert result.project_path.stem == 'cookies' + assert result.project_path.is_dir() + + assert result.context == { + "repo_name": "cookies", + "short_description": "cookies is awesome", + } + assert str(result) == f"" + """ + ) + + result = testdir.runpytest("-v", f"--template={copier_template}") + test_check(result, "test_copie_project") + assert result.ret == 0 From 89804aeffb0c91fe0a0b5418fda1dfe1df747553 Mon Sep 17 00:00:00 2001 From: Pierrick Rambaud Date: Thu, 7 Sep 2023 06:15:48 +0000 Subject: [PATCH 2/2] fix: add the context to the output --- pytest_copie/plugin.py | 18 ++++++++++-------- tests/test_copie.py | 5 +++-- 2 files changed, 13 insertions(+), 10 deletions(-) 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"" """