Skip to content

Commit

Permalink
fix: Fix removing dir in feedstock_io, and add a test case (#2199)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgorny authored Jan 1, 2025
1 parent e598613 commit c3b4c88
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
5 changes: 4 additions & 1 deletion conda_smithy/feedstock_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
23 changes: 23 additions & 0 deletions news/fix-feedstock_io-dir-remove.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
**Added:**

* <news item>

**Changed:**

* <news item>

**Deprecated:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* Fixed removing directories recursively from git index. (#2199)

**Security:**

* <news item>
27 changes: 27 additions & 0 deletions tests/test_feedstock_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit c3b4c88

Please sign in to comment.