diff --git a/conda_smithy/feedstock_io.py b/conda_smithy/feedstock_io.py index 7e4146cf1..54d18aba9 100644 --- a/conda_smithy/feedstock_io.py +++ b/conda_smithy/feedstock_io.py @@ -87,7 +87,10 @@ def remove_file_or_dir(filename): repo = get_repo(filename) if repo: - repo.index.remove_all(["filename/**"]) + index_path = ( + Path(filename).resolve().relative_to(repo.workdir).as_posix() + ) + repo.index.remove_all([f"{index_path}/**"]) repo.index.write() shutil.rmtree(filename) diff --git a/news/fix-feedstock_io-dir-remove.rst b/news/fix-feedstock_io-dir-remove.rst new file mode 100644 index 000000000..8ee248b2a --- /dev/null +++ b/news/fix-feedstock_io-dir-remove.rst @@ -0,0 +1,23 @@ +**Added:** + +* + +**Changed:** + +* + +**Deprecated:** + +* + +**Removed:** + +* + +**Fixed:** + +* Fixed removing directories recursively from git index. (#2199) + +**Security:** + +* diff --git a/tests/test_feedstock_io.py b/tests/test_feedstock_io.py index b8c188cac..eb6ee4b84 100644 --- a/tests/test_feedstock_io.py +++ b/tests/test_feedstock_io.py @@ -178,6 +178,33 @@ def test_remove_file(self): repo.index.read() self.assertRaises(KeyError, lambda: repo.index[basename]) + def test_remove_dir(self): + for tmp_dir, repo, pathfunc in parameterize(): + dirname = os.path.join(tmp_dir, "dir") + os.makedirs(f"{dirname}/a") + os.makedirs(f"{dirname}/b") + for basename in ["dir/a/foo.txt", "dir/b/bar.txt", "dir/baz.txt"]: + filename = os.path.join(tmp_dir, basename) + + with open(filename, "w", encoding="utf-8", newline="\n") as fh: + fh.write("") + if repo is not None: + repo.index.add(basename) + repo.index.write() + + self.assertTrue(os.path.exists(filename)) + if repo is not None: + self.assertIsNotNone(repo.index[basename]) + + fio.remove_file_or_dir(pathfunc(dirname)) + + for basename in ["dir/a/foo.txt", "dir/b/bar.txt", "dir/baz.txt"]: + self.assertFalse(os.path.exists(filename)) + if repo is not None: + repo.index.read() + self.assertRaises(KeyError, lambda: repo.index[basename]) + self.assertFalse(os.path.exists(dirname)) + def test_copy_file(self): for tmp_dir, repo, pathfunc in parameterize(): basename1 = "test1.txt"