Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix: add the context to the output #12

Merged
merged 2 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions pytest_copie/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
29 changes: 29 additions & 0 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 @@ -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 {result.project_path}>"
"""
)

result = testdir.runpytest("-v", f"--template={copier_template}")
test_check(result, "test_copie_project")
assert result.ret == 0