From 9806d758102813175773f622c2113e55cabcd211 Mon Sep 17 00:00:00 2001 From: hparfr Date: Mon, 16 Oct 2023 18:26:51 +0200 Subject: [PATCH] test gitignored file a file in the template but gitignored in the project should persist in the filesystem --- copier/main.py | 9 ++++++++- tests/test_updatediff.py | 6 ++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/copier/main.py b/copier/main.py index 71c0d191d..595b9d6c8 100644 --- a/copier/main.py +++ b/copier/main.py @@ -895,8 +895,15 @@ def _apply_update(self): # Try to apply cached diff into final destination with local.cwd(subproject_top): apply_cmd = git["apply", "--reject", "--exclude", self.answers_relpath] + ignored_files = git["status", "--ignored", "--porcelain"]() + # returns "!! file1\n !! file2\n" + # extra_exclude will contain: ["file1", file2"] + extra_exclude = [ + filename.split("!! ").pop() + for filename in ignored_files.splitlines() + ] for skip_pattern in chain( - self.skip_if_exists, self.template.skip_if_exists + self.skip_if_exists, self.template.skip_if_exists, extra_exclude ): apply_cmd = apply_cmd["--exclude", skip_pattern] (apply_cmd << diff)(retcode=None) diff --git a/tests/test_updatediff.py b/tests/test_updatediff.py index 4c298758c..fe5a8cdd3 100644 --- a/tests/test_updatediff.py +++ b/tests/test_updatediff.py @@ -604,6 +604,7 @@ def test_file_removed(tmp_path_factory: pytest.TempPathFactory) -> None: Path("dir 3", "subdir 3", "3.txt"): "content 3", Path("dir 4", "subdir 4", "4.txt"): "content 4", Path("dir 5", "subdir 5", "5.txt"): "content 5", + "toignore.txt": "should survive update", } ) git("init") @@ -632,6 +633,7 @@ def test_file_removed(tmp_path_factory: pytest.TempPathFactory) -> None: assert (dst / "dir 3" / "subdir 3" / "3.txt").is_file() assert (dst / "dir 4" / "subdir 4" / "4.txt").is_file() assert (dst / "dir 5" / "subdir 5" / "5.txt").is_file() + assert (dst / "toignore.txt").is_file() assert (dst / "I.txt").is_file() assert (dst / "dir II" / "II.txt").is_file() assert (dst / "dir 3" / "subdir III" / "III.txt").is_file() @@ -649,6 +651,9 @@ def test_file_removed(tmp_path_factory: pytest.TempPathFactory) -> None: git("tag", "2") # Subproject updates with local.cwd(dst): + Path(".gitignore").write_text("toignore.txt") + git("add", ".gitignore") + git("commit", "-m", "ignore file") with pytest.raises( UserMessageError, match="Enable overwrite to update a subproject." ): @@ -661,6 +666,7 @@ def test_file_removed(tmp_path_factory: pytest.TempPathFactory) -> None: assert (dst / "dir 3" / "subdir III" / "III.txt").is_file() assert (dst / "dir 4" / "subdir 4" / "IV.txt").is_file() assert (dst / "6.txt").is_file() + assert (dst / "toignore.txt").is_file() # Check what must not exist assert not (dst / "1.txt").exists() assert not (dst / "dir 2").exists()