Просмотр исходного кода

Fix Windows file deletion error in _remove_pack

Jelmer Vernooij 1 неделя назад
Родитель
Сommit
ef3934ea6c
2 измененных файлов с 8 добавлено и 13 удалено
  1. 6 3
      dulwich/object_store.py
  2. 2 10
      tests/compat/test_client.py

+ 6 - 3
dulwich/object_store.py

@@ -1803,10 +1803,13 @@ class DiskObjectStore(PackBasedObjectStore):
             del self._pack_cache[os.path.basename(pack._basename)]
         except KeyError:
             pass
+        # Store paths before closing to avoid re-opening files on Windows
+        data_path = pack._data_path
+        idx_path = pack._idx_path
         pack.close()
-        os.remove(pack.data.path)
-        if hasattr(pack.index, "path"):
-            os.remove(pack.index.path)
+        os.remove(data_path)
+        if os.path.exists(idx_path):
+            os.remove(idx_path)
 
     def _get_pack_basepath(
         self, entries: Iterable[tuple[bytes, int, int | None]]

+ 2 - 10
tests/compat/test_client.py

@@ -66,12 +66,8 @@ class DulwichClientTestBase:
         self.dest = os.path.join(self.gitroot, "dest")
         file.ensure_dir_exists(self.dest)
         run_git_or_fail(["init", "--quiet", "--bare"], cwd=self.dest)
-
-    def tearDown(self) -> None:
-        rmtree_ro(self.gitroot)
-        # Clear instance variables to break reference cycles
-        self.gitroot = None
-        self.dest = None
+        # Register cleanup to run after test's cleanup handlers
+        self.addCleanup(rmtree_ro, self.gitroot)
 
     def assertDestEqualsSrc(self) -> None:
         repo_dir = os.path.join(self.gitroot, "server_new.export")
@@ -568,7 +564,6 @@ class DulwichTCPClientTest(CompatTestCase, DulwichClientTestBase):
         self.process.wait()
         self.process.stdout.close()
         self.process.stderr.close()
-        DulwichClientTestBase.tearDown(self)
         CompatTestCase.tearDown(self)
 
     def _client(self):
@@ -640,7 +635,6 @@ class DulwichMockSSHClientTest(CompatTestCase, DulwichClientTestBase):
         client.get_ssh_vendor = TestSSHVendor
 
     def tearDown(self) -> None:
-        DulwichClientTestBase.tearDown(self)
         CompatTestCase.tearDown(self)
         client.get_ssh_vendor = self.real_vendor
 
@@ -662,7 +656,6 @@ class DulwichSubprocessClientTest(CompatTestCase, DulwichClientTestBase):
         DulwichClientTestBase.setUp(self)
 
     def tearDown(self) -> None:
-        DulwichClientTestBase.tearDown(self)
         CompatTestCase.tearDown(self)
 
     def _client(self):
@@ -850,7 +843,6 @@ class DulwichHttpClientTest(CompatTestCase, DulwichClientTestBase):
         run_git_or_fail(["config", "http.receivepack", "true"], cwd=self.dest)
 
     def tearDown(self) -> None:
-        DulwichClientTestBase.tearDown(self)
         CompatTestCase.tearDown(self)
         self._httpd.shutdown()
         self._httpd.socket.close()