diff --git a/pytest_copie/plugin.py b/pytest_copie/plugin.py index 3f6ee3f..aed0812 100644 --- a/pytest_copie/plugin.py +++ b/pytest_copie/plugin.py @@ -77,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 = {} + 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 @@ -96,17 +95,23 @@ 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()) + # 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) + return Result(exception, exit_code, project_path, answers) @pytest.fixture diff --git a/tests/test_copie.py b/tests/test_copie.py index e7af0d5..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"}) @@ -110,3 +111,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": "copie 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": "copie is awesome", + } + assert str(result) == f"" + """ + ) + + result = testdir.runpytest("-v", f"--template={copier_template}") + test_check(result, "test_copie_project") + assert result.ret == 0