浏览代码

Merge pull request #7141 from RayyanAnsari/fix-repository-tests

Fix repository tests on Windows
TW 2 年之前
父节点
当前提交
57c9f6193a
共有 3 个文件被更改,包括 21 次插入8 次删除
  1. 9 3
      src/borg/crypto/file_integrity.py
  2. 3 4
      src/borg/repository.py
  3. 9 1
      src/borg/testsuite/repository.py

+ 9 - 3
src/borg/crypto/file_integrity.py

@@ -128,6 +128,7 @@ class IntegrityCheckedFile(FileLikeWrapper):
         self.writing = write
         mode = "wb" if write else "rb"
         self.file_fd = override_fd or open(path, mode)
+        self.file_opened = override_fd is None
         self.digests = {}
 
         hash_cls = XXH64FileHashingWrapper
@@ -190,9 +191,14 @@ class IntegrityCheckedFile(FileLikeWrapper):
 
     def __exit__(self, exc_type, exc_val, exc_tb):
         exception = exc_type is not None
-        if not exception:
-            self.hash_part("final", is_final=True)
-        self.hasher.__exit__(exc_type, exc_val, exc_tb)
+
+        try:
+            if not exception:
+                self.hash_part("final", is_final=True)
+            self.hasher.__exit__(exc_type, exc_val, exc_tb)
+        finally:
+            if self.file_opened:
+                self.file_fd.close()
         if exception:
             return
         if self.writing:

+ 3 - 4
src/borg/repository.py

@@ -636,7 +636,7 @@ class Repository:
             os.fsync(fd.fileno())
 
         def rename_tmp(file):
-            os.rename(file + ".tmp", file)
+            os.replace(file + ".tmp", file)
 
         hints = {
             "version": 2,
@@ -1448,13 +1448,12 @@ class LoggedIO:
 
     def cleanup(self, transaction_id):
         """Delete segment files left by aborted transactions"""
+        self.close_segment()
         self.segment = transaction_id + 1
         count = 0
         for segment, filename in self.segment_iterator(reverse=True):
             if segment > transaction_id:
-                if segment in self.fds:
-                    del self.fds[segment]
-                safe_unlink(filename)
+                self.delete_segment(segment)
                 count += 1
             else:
                 break

+ 9 - 1
src/borg/testsuite/repository.py

@@ -844,7 +844,7 @@ class RepositoryCheckTestCase(RepositoryTestCaseBase):
             fd.write(b"BOOM")
 
     def delete_segment(self, segment):
-        os.unlink(os.path.join(self.tmppath, "repository", "data", "0", str(segment)))
+        self.repository.io.delete_segment(segment)
 
     def delete_index(self):
         os.unlink(os.path.join(self.tmppath, "repository", f"index.{self.get_head()}"))
@@ -1138,6 +1138,14 @@ class RemoteRepositoryCheckTestCase(RepositoryCheckTestCase):
         # skip this test, we can't mock-patch a Repository class in another process!
         pass
 
+    def test_repair_missing_commit_segment(self):
+        # skip this test, files in RemoteRepository cannot be deleted
+        pass
+
+    def test_repair_missing_segment(self):
+        # skip this test, files in RemoteRepository cannot be deleted
+        pass
+
 
 class RemoteLoggerTestCase(BaseTestCase):
     def setUp(self):