From 5666259146e9dc284e58e52c4b9643b920053b1e Mon Sep 17 00:00:00 2001 From: Sigurd Spieckermann Date: Wed, 15 Feb 2023 10:36:20 +0100 Subject: [PATCH 1/6] fix: skip tasks in pretend mode --- copier/main.py | 17 +++++++------ tests/test_migrations.py | 54 ++++++++++++++++++++++++++++++++++++++++ tests/test_tasks.py | 15 +++++++++++ 3 files changed, 79 insertions(+), 7 deletions(-) diff --git a/copier/main.py b/copier/main.py index d65b33383..02f8fe36d 100644 --- a/copier/main.py +++ b/copier/main.py @@ -651,7 +651,8 @@ def run_copy(self) -> None: if not self.quiet: # TODO Unify printing tools print("") # padding space - self._execute_tasks(self.template.tasks) + if not self.pretend: + self._execute_tasks(self.template.tasks) except Exception: if not was_existing and self.cleanup_on_error: rmtree(self.subproject.local_abspath) @@ -743,9 +744,10 @@ def _apply_update(self): ) diff = diff_cmd("--inter-hunk-context=0") # Run pre-migration tasks - self._execute_tasks( - self.template.migration_tasks("before", self.subproject.template) - ) + if not self.pretend: + self._execute_tasks( + self.template.migration_tasks("before", self.subproject.template) + ) self._uncached_copy() # Try to apply cached diff into final destination with local.cwd(self.subproject.local_abspath): @@ -786,9 +788,10 @@ def _apply_update(self): _remove_old_files(self.subproject.local_abspath, compared) # Run post-migration tasks - self._execute_tasks( - self.template.migration_tasks("after", self.subproject.template) - ) + if not self.pretend: + self._execute_tasks( + self.template.migration_tasks("after", self.subproject.template) + ) def _uncached_copy(self): """Copy template to destination without using answer cache.""" diff --git a/tests/test_migrations.py b/tests/test_migrations.py index 665f38021..5cd21fe48 100644 --- a/tests/test_migrations.py +++ b/tests/test_migrations.py @@ -223,3 +223,57 @@ 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): + src, dst = tmp_path_factory.mktemp("src"), tmp_path_factory.mktemp("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), str(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..51fc04366 100644 --- a/tests/test_tasks.py +++ b/tests/test_tasks.py @@ -43,3 +43,18 @@ 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): + src, dst = tmp_path_factory.mktemp("src"), tmp_path_factory.mktemp("dst") + build_file_tree( + { + src + / "copier.yml": f""" + _tasks: + - touch created-by-task.txt + """ + } + ) + copier.copy(str(src), str(dst), pretend=True) + assert not (dst / "created-by-task.txt").exists() From 9fbecb900507c4551d3111e18760a4d5f64f8f71 Mon Sep 17 00:00:00 2001 From: Sigurd Spieckermann Date: Wed, 15 Feb 2023 11:06:31 +0100 Subject: [PATCH 2/6] style: replace f-string by regular string --- tests/test_tasks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_tasks.py b/tests/test_tasks.py index 51fc04366..c969b5a5a 100644 --- a/tests/test_tasks.py +++ b/tests/test_tasks.py @@ -50,7 +50,7 @@ def test_pretend_mode(tmp_path_factory): build_file_tree( { src - / "copier.yml": f""" + / "copier.yml": """ _tasks: - touch created-by-task.txt """ From 39717b5fe78d82f30994f397d49eafffac25cbc5 Mon Sep 17 00:00:00 2001 From: Sigurd Spieckermann Date: Mon, 6 Mar 2023 08:13:44 +0100 Subject: [PATCH 3/6] refactor: check pretend mode in `_execute_task()` method --- copier/main.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/copier/main.py b/copier/main.py index 02f8fe36d..46acb310f 100644 --- a/copier/main.py +++ b/copier/main.py @@ -218,6 +218,8 @@ def _execute_tasks(self, tasks: Sequence[Task]) -> None: Arguments: tasks: The list of tasks to run. """ + if self.pretend: + return for i, task in enumerate(tasks): task_cmd = task.cmd if isinstance(task_cmd, str): @@ -651,8 +653,7 @@ def run_copy(self) -> None: if not self.quiet: # TODO Unify printing tools print("") # padding space - if not self.pretend: - self._execute_tasks(self.template.tasks) + self._execute_tasks(self.template.tasks) except Exception: if not was_existing and self.cleanup_on_error: rmtree(self.subproject.local_abspath) @@ -744,10 +745,9 @@ def _apply_update(self): ) diff = diff_cmd("--inter-hunk-context=0") # Run pre-migration tasks - if not self.pretend: - self._execute_tasks( - self.template.migration_tasks("before", self.subproject.template) - ) + self._execute_tasks( + self.template.migration_tasks("before", self.subproject.template) + ) self._uncached_copy() # Try to apply cached diff into final destination with local.cwd(self.subproject.local_abspath): @@ -788,10 +788,9 @@ def _apply_update(self): _remove_old_files(self.subproject.local_abspath, compared) # Run post-migration tasks - if not self.pretend: - self._execute_tasks( - self.template.migration_tasks("after", self.subproject.template) - ) + self._execute_tasks( + self.template.migration_tasks("after", self.subproject.template) + ) def _uncached_copy(self): """Copy template to destination without using answer cache.""" From 0ac2e7801ecfd988db305911ddf263474ad8f9b3 Mon Sep 17 00:00:00 2001 From: Sigurd Spieckermann Date: Sat, 11 Mar 2023 15:12:17 +0100 Subject: [PATCH 4/6] fix: preserve printing of pretended task commands --- copier/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/copier/main.py b/copier/main.py index 46acb310f..cf9bae5f9 100644 --- a/copier/main.py +++ b/copier/main.py @@ -218,8 +218,6 @@ def _execute_tasks(self, tasks: Sequence[Task]) -> None: Arguments: tasks: The list of tasks to run. """ - if self.pretend: - return for i, task in enumerate(tasks): task_cmd = task.cmd if isinstance(task_cmd, str): @@ -234,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) From 7e55f820504089a538a44de37b035b886d6ea9d1 Mon Sep 17 00:00:00 2001 From: Sigurd Spieckermann Date: Wed, 22 Mar 2023 10:54:08 +0100 Subject: [PATCH 5/6] test: add type hints --- tests/test_migrations.py | 2 +- tests/test_tasks.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_migrations.py b/tests/test_migrations.py index 5cd21fe48..2d64acece 100644 --- a/tests/test_migrations.py +++ b/tests/test_migrations.py @@ -225,7 +225,7 @@ def test_prereleases(tmp_path: Path): copy(dst_path=dst, defaults=True, overwrite=True) -def test_pretend_mode(tmp_path_factory): +def test_pretend_mode(tmp_path_factory: pytest.TempPathFactory) -> None: src, dst = tmp_path_factory.mktemp("src"), tmp_path_factory.mktemp("dst") # Build template in v1 diff --git a/tests/test_tasks.py b/tests/test_tasks.py index c969b5a5a..117798a80 100644 --- a/tests/test_tasks.py +++ b/tests/test_tasks.py @@ -45,7 +45,7 @@ def test_copy_tasks(tmp_path, demo_template): assert (tmp_path / "pyfile").is_file() -def test_pretend_mode(tmp_path_factory): +def test_pretend_mode(tmp_path_factory: pytest.TempPathFactory) -> None: src, dst = tmp_path_factory.mktemp("src"), tmp_path_factory.mktemp("dst") build_file_tree( { From b2f5a3c6ddbabc9669e4d151e51dffcf8345c9cf Mon Sep 17 00:00:00 2001 From: Sigurd Spieckermann Date: Wed, 22 Mar 2023 10:59:32 +0100 Subject: [PATCH 6/6] style: improve test code formatting --- tests/test_migrations.py | 16 ++++++++++------ tests/test_tasks.py | 11 ++++++----- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/tests/test_migrations.py b/tests/test_migrations.py index 2d64acece..85ae7f76f 100644 --- a/tests/test_migrations.py +++ b/tests/test_migrations.py @@ -226,7 +226,7 @@ def test_prereleases(tmp_path: Path): def test_pretend_mode(tmp_path_factory: pytest.TempPathFactory) -> None: - src, dst = tmp_path_factory.mktemp("src"), tmp_path_factory.mktemp("dst") + src, dst = map(tmp_path_factory.mktemp, ("src", "dst")) # Build template in v1 with local.cwd(src): @@ -234,16 +234,18 @@ def test_pretend_mode(tmp_path_factory: pytest.TempPathFactory) -> None: build_file_tree( { "[[ _copier_conf.answers_file ]].jinja": "[[_copier_answers|to_nice_yaml]]", - "copier.yml": f""" + "copier.yml": ( + f"""\ _envops: {BRACKET_ENVOPS_JSON} - """, + """ + ), } ) git("add", ".") git("commit", "-mv1") git("tag", "v1") - copy(str(src), str(dst)) + copy(str(src), dst) answers = yaml.safe_load((dst / ".copier-answers.yml").read_text()) assert answers["_commit"] == "v1" @@ -257,7 +259,8 @@ def test_pretend_mode(tmp_path_factory: pytest.TempPathFactory) -> None: build_file_tree( { "[[ _copier_conf.answers_file ]].jinja": "[[_copier_answers|to_nice_yaml]]", - "copier.yml": f""" + "copier.yml": ( + f"""\ _envops: {BRACKET_ENVOPS_JSON} _migrations: - version: v2 @@ -265,7 +268,8 @@ def test_pretend_mode(tmp_path_factory: pytest.TempPathFactory) -> None: - touch v2-before.txt after: - touch v2-after.txt - """, + """ + ), } ) git("add", ".") diff --git a/tests/test_tasks.py b/tests/test_tasks.py index 117798a80..14bd49202 100644 --- a/tests/test_tasks.py +++ b/tests/test_tasks.py @@ -46,15 +46,16 @@ def test_copy_tasks(tmp_path, demo_template): def test_pretend_mode(tmp_path_factory: pytest.TempPathFactory) -> None: - src, dst = tmp_path_factory.mktemp("src"), tmp_path_factory.mktemp("dst") + src, dst = map(tmp_path_factory.mktemp, ("src", "dst")) build_file_tree( { - src - / "copier.yml": """ + (src / "copier.yml"): ( + """ _tasks: - touch created-by-task.txt - """ + """ + ) } ) - copier.copy(str(src), str(dst), pretend=True) + copier.copy(str(src), dst, pretend=True) assert not (dst / "created-by-task.txt").exists()