Skip to content

Commit

Permalink
fix: Fix issue with update subdirectory when an answers file path is …
Browse files Browse the repository at this point in the history
…given, add unit test
  • Loading branch information
bburgin committed Sep 25, 2021
1 parent b8ff4d8 commit 44e6cbf
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
8 changes: 7 additions & 1 deletion copier/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,15 @@ def _render_context(self) -> Mapping:
# Backwards compatibility
# FIXME Remove it?
conf = asdict(self)

# During update, the destination _subdirectory is joined with the rendered answers_file value.
# To avoid including the same folders twice in the path, write to the config the
# answers filename (without the path).
answers_file = self.answers_relpath.name

conf.update(
{
"answers_file": self.answers_relpath,
"answers_file": answers_file,
"src_path": self.template.local_abspath,
}
)
Expand Down
62 changes: 62 additions & 0 deletions tests/test_subdirectory.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os
from pathlib import Path

import pytest
import yaml
from plumbum import local
from plumbum.cmd import git

Expand Down Expand Up @@ -86,6 +88,66 @@ def test_update_subdirectory(demo_template, tmp_path):
assert (tmp_path / "conf_readme.md").exists()


@pytest.mark.timeout(20)
def test_update_subdirectory_given_answers_file_path(tmp_path_factory):
src, dst = map(tmp_path_factory.mktemp, ("src", "dst"))
answers_filename = ".copier-answers.component-type1.yaml"
answers_file_relpath = Path("group1/component-type1/component1", answers_filename)
component_dir = answers_file_relpath.parent
component_type_dir = component_dir.parent

copier_yaml_contents = f"""\
_templates_suffix: ''
_subdirectory: '{{{{ template }}}}'
template:
type: str
choices:
- {component_type_dir}
"""

with local.cwd(src):
answers_file_tree_key = str(
component_dir.joinpath("{{_copier_conf.answers_file}}")
)
test_file_tree_key = str(component_dir.joinpath("test.txt"))
build_file_tree(
{
"copier.yaml": copier_yaml_contents,
answers_file_tree_key: "{{ _copier_answers|to_yaml }}",
test_file_tree_key: "True",
}
)
git("init")
git("add", ".")
git("commit", "-m1")
git("tag", "1")
build_file_tree(
{"copier.yaml": copier_yaml_contents, test_file_tree_key: "False"}
)
git("commit", "-am2")
git("tag", "2")
copier.run_copy(
str(src),
str(dst),
vcs_ref="1",
data={"template": str(component_type_dir)},
defaults=True,
overwrite=True,
answers_file=answers_filename,
)
with local.cwd(dst):
git("init")
git("add", ".")
git("commit", "-m1")
copier.run_update(
defaults=True, overwrite=True, answers_file=f"component1/{answers_filename}"
)
answers = yaml.safe_load(dst.joinpath("component1", answers_filename).read_bytes())
assert answers["_commit"] == "2"
assert dst.joinpath("component1/test.txt").read_text() == "False"


def test_new_version_uses_subdirectory(tmp_path_factory):
# Template in v1 doesn't have a _subdirectory;
# in v2 it moves all things into a subdir and adds that key to copier.yml.
Expand Down

0 comments on commit 44e6cbf

Please sign in to comment.