Skip to content

Commit

Permalink
fix: add the context to the output
Browse files Browse the repository at this point in the history
  • Loading branch information
12rambau committed Sep 7, 2023
1 parent 1c5b736 commit 89804ae
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
18 changes: 10 additions & 8 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 @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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):
Expand Down
5 changes: 3 additions & 2 deletions tests/test_copie.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
Expand Down Expand Up @@ -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
Expand All @@ -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"<Result {result.project_path}>"
"""
Expand Down

0 comments on commit 89804ae

Please # to comment.