commit:     0cc3f9e269b26173c2f81d731c5fe208b758270b
Author:     Mike Gilbert <floppym <AT> gentoo <DOT> org>
AuthorDate: Sun Mar  3 02:28:42 2024 +0000
Commit:     Mike Gilbert <floppym <AT> gentoo <DOT> org>
CommitDate: Fri Mar 15 20:05:34 2024 +0000
URL:        https://gitweb.gentoo.org/proj/portage.git/commit/?id=0cc3f9e2

Improve testCopyFileSparse

Actually create sparse blocks at the start and end.
Check file size before/after copying.
Ensure sparse output when _fastcopy succeeds.

Signed-off-by: Mike Gilbert <floppym <AT> gentoo.org>

 lib/portage/tests/util/file_copy/test_copyfile.py | 35 ++++++++++++++++-------
 1 file changed, 24 insertions(+), 11 deletions(-)

diff --git a/lib/portage/tests/util/file_copy/test_copyfile.py 
b/lib/portage/tests/util/file_copy/test_copyfile.py
index e91a47bed8..e114e6ae35 100644
--- a/lib/portage/tests/util/file_copy/test_copyfile.py
+++ b/lib/portage/tests/util/file_copy/test_copyfile.py
@@ -3,13 +3,14 @@
 
 import shutil
 import tempfile
+from unittest.mock import patch
 
 import pytest
 
 from portage import os
 from portage.tests import TestCase
 from portage.checksum import perform_md5
-from portage.util.file_copy import copyfile
+from portage.util.file_copy import copyfile, _fastcopy
 
 
 class CopyFileTestCase(TestCase):
@@ -42,25 +43,37 @@ class CopyFileSparseTestCase(TestCase):
             # files too big, in case the filesystem doesn't support
             # sparse files.
             with open(src_path, "wb") as f:
+                f.seek(2**16, os.SEEK_SET)
                 f.write(content)
-                f.seek(2**17, 1)
-                f.write(content)
-                f.seek(2**18, 1)
+                f.seek(2**17, os.SEEK_SET)
                 f.write(content)
                 # Test that sparse blocks are handled correctly at
-                # the end of the file (involves seek and truncate).
-                f.seek(2**17, 1)
+                # the end of the file.
+                f.truncate(2**18)
 
-            copyfile(src_path, dest_path)
+            fastcopy_success = False
+
+            def mock_fastcopy(src, dst):
+                nonlocal fastcopy_success
+                _fastcopy(src, dst)
+                fastcopy_success = True
+
+            with patch("portage.util.file_copy._fastcopy", new=mock_fastcopy):
+                copyfile(src_path, dest_path)
 
             self.assertEqual(perform_md5(src_path), perform_md5(dest_path))
 
-            # This last part of the test is expected to fail when sparse
-            # copy is not implemented, so mark it xfail:
-            pytest.xfail(reason="sparse copy is not implemented")
+            src_stat = os.stat(src_path)
+            dest_stat = os.stat(dest_path)
+
+            self.assertEqual(src_stat.st_size, dest_stat.st_size)
 
             # If sparse blocks were preserved, then both files should
             # consume the same number of blocks.
-            self.assertEqual(os.stat(src_path).st_blocks, 
os.stat(dest_path).st_blocks)
+            # This is expected to fail when sparse copy is not implemented.
+            if src_stat.st_blocks != dest_stat.st_blocks:
+                if fastcopy_success:
+                    pytest.fail(reason="sparse copy failed with _fastcopy")
+                pytest.xfail(reason="sparse copy is not implemented")
         finally:
             shutil.rmtree(tempdir)

Reply via email to