diff --git a/copier/main.py b/copier/main.py index d65b33383..cf9bae5f9 100644 --- a/copier/main.py +++ b/copier/main.py @@ -232,6 +232,8 @@ def _execute_tasks(self, tasks: Sequence[Task]) -> None: | f" > Running task {i + 1} of {len(tasks)}: {task_cmd}", file=sys.stderr, ) + if self.pretend: + continue with local.cwd(self.subproject.local_abspath), local.env(**task.extra_env): subprocess.run(task_cmd, shell=use_shell, check=True, env=local.env) diff --git a/tests/test_migrations.py b/tests/test_migrations.py index 665f38021..85ae7f76f 100644 --- a/tests/test_migrations.py +++ b/tests/test_migrations.py @@ -223,3 +223,61 @@ def test_prereleases(tmp_path: Path): # It should fail if downgrading with pytest.raises(UserMessageError): copy(dst_path=dst, defaults=True, overwrite=True) + + +def test_pretend_mode(tmp_path_factory: pytest.TempPathFactory) -> None: + src, dst = map(tmp_path_factory.mktemp, ("src", "dst")) + + # Build template in v1 + with local.cwd(src): + git("init") + build_file_tree( + { + "[[ _copier_conf.answers_file ]].jinja": "[[_copier_answers|to_nice_yaml]]", + "copier.yml": ( + f"""\ + _envops: {BRACKET_ENVOPS_JSON} + """ + ), + } + ) + git("add", ".") + git("commit", "-mv1") + git("tag", "v1") + + copy(str(src), dst) + answers = yaml.safe_load((dst / ".copier-answers.yml").read_text()) + assert answers["_commit"] == "v1" + + with local.cwd(dst): + git("init") + git("add", ".") + git("commit", "-mv1") + + # Evolve template to v2 + with local.cwd(src): + build_file_tree( + { + "[[ _copier_conf.answers_file ]].jinja": "[[_copier_answers|to_nice_yaml]]", + "copier.yml": ( + f"""\ + _envops: {BRACKET_ENVOPS_JSON} + _migrations: + - version: v2 + before: + - touch v2-before.txt + after: + - touch v2-after.txt + """ + ), + } + ) + git("add", ".") + git("commit", "-mv2") + git("tag", "v2") + + copy(dst_path=dst, overwrite=True, pretend=True) + answers = yaml.safe_load((dst / ".copier-answers.yml").read_text()) + assert answers["_commit"] == "v1" + assert not (dst / "v2-before.txt").exists() + assert not (dst / "v2-after.txt").exists() diff --git a/tests/test_tasks.py b/tests/test_tasks.py index 446be98f1..14bd49202 100644 --- a/tests/test_tasks.py +++ b/tests/test_tasks.py @@ -43,3 +43,19 @@ def test_copy_tasks(tmp_path, demo_template): assert (tmp_path / "hello" / "world").exists() assert (tmp_path / "bye").is_file() assert (tmp_path / "pyfile").is_file() + + +def test_pretend_mode(tmp_path_factory: pytest.TempPathFactory) -> None: + src, dst = map(tmp_path_factory.mktemp, ("src", "dst")) + build_file_tree( + { + (src / "copier.yml"): ( + """ + _tasks: + - touch created-by-task.txt + """ + ) + } + ) + copier.copy(str(src), dst, pretend=True) + assert not (dst / "created-by-task.txt").exists()