fix(updater): sync dependency deletions

This commit is contained in:
Deepak kudi 2026-06-04 08:43:27 +05:30
parent 70ad5e3df8
commit d7b95c2eaf
2 changed files with 61 additions and 1 deletions

View File

@ -0,0 +1,54 @@
import os
import shutil
import tempfile
import unittest
from pathlib import Path
from unittest.mock import patch
import updater
class DependencyApplyUpstreamChangesTest(unittest.TestCase):
def test_apply_upstream_changes_removes_stale_files(self):
with tempfile.TemporaryDirectory() as tmp:
tmp_path = Path(tmp)
worktree = tmp_path / "worktree"
source = tmp_path / "source"
target = worktree / "plugins" / "example"
source.mkdir()
(source / "kept.zsh").write_text("new content")
(source / ".git").mkdir()
(source / ".github").mkdir()
target.mkdir(parents=True)
(target / "kept.zsh").write_text("old content")
(target / "stale.zsh").write_text("stale content")
def fake_clone(_remote_url, _ref, repo_dir, reclone=False):
shutil.copytree(source, repo_dir, dirs_exist_ok=True)
dependency = updater.Dependency(
"plugins/example",
{"repo": "owner/repo", "branch": "main", "version": "old"},
)
previous_cwd = os.getcwd()
try:
os.chdir(worktree)
with (
patch.object(updater, "TMP_DIR", str(tmp_path / "tmp")),
patch.object(updater.Git, "clone", fake_clone),
):
dependency._Dependency__apply_upstream_changes("new")
finally:
os.chdir(previous_cwd)
self.assertEqual((target / "kept.zsh").read_text(), "new content")
self.assertFalse((target / "stale.zsh").exists())
self.assertFalse((target / ".git").exists())
self.assertFalse((target / ".github").exists())
if __name__ == "__main__":
unittest.main()

View File

@ -325,10 +325,16 @@ Check out the [list of changes]({status["compare_url"]}).
# Copy files from upstream repo
print(f"Copying files from {repo_dir} to {path}", file=sys.stderr)
if os.path.exists(path):
if os.path.islink(path) or os.path.isfile(path):
os.remove(path)
else:
shutil.rmtree(path)
os.makedirs(os.path.dirname(path), exist_ok=True)
shutil.copytree(
repo_dir,
path,
dirs_exist_ok=True,
ignore=shutil.ignore_patterns(*GLOBAL_IGNORE),
)