|
5 | 5 | import errno |
6 | 6 | from unittest.mock import patch |
7 | 7 |
|
8 | | -from distutils.file_util import move_file |
| 8 | +from distutils.file_util import move_file, copy_file |
9 | 9 | from distutils import log |
10 | 10 | from distutils.tests import support |
11 | 11 | from distutils.errors import DistutilsFileError |
@@ -78,6 +78,36 @@ def test_move_file_exception_unpacking_unlink(self): |
78 | 78 | fobj.write('spam eggs') |
79 | 79 | move_file(self.source, self.target, verbose=0) |
80 | 80 |
|
| 81 | + def test_copy_file_hard_link(self): |
| 82 | + with open(self.source, 'w') as f: |
| 83 | + f.write('some content') |
| 84 | + st = os.stat(self.source) |
| 85 | + copy_file(self.source, self.target, link='hard') |
| 86 | + st2 = os.stat(self.source) |
| 87 | + st3 = os.stat(self.target) |
| 88 | + self.assertTrue(os.path.samestat(st, st2), (st, st2)) |
| 89 | + self.assertTrue(os.path.samestat(st2, st3), (st2, st3)) |
| 90 | + with open(self.source, 'r') as f: |
| 91 | + self.assertEqual(f.read(), 'some content') |
| 92 | + |
| 93 | + def test_copy_file_hard_link_failure(self): |
| 94 | + # If hard linking fails, copy_file() falls back on copying file |
| 95 | + # (some special filesystems don't support hard linking even under |
| 96 | + # Unix, see issue #8876). |
| 97 | + with open(self.source, 'w') as f: |
| 98 | + f.write('some content') |
| 99 | + st = os.stat(self.source) |
| 100 | + with patch("os.link", side_effect=OSError(0, "linking unsupported")): |
| 101 | + copy_file(self.source, self.target, link='hard') |
| 102 | + st2 = os.stat(self.source) |
| 103 | + st3 = os.stat(self.target) |
| 104 | + self.assertTrue(os.path.samestat(st, st2), (st, st2)) |
| 105 | + self.assertFalse(os.path.samestat(st2, st3), (st2, st3)) |
| 106 | + for fn in (self.source, self.target): |
| 107 | + with open(fn, 'r') as f: |
| 108 | + self.assertEqual(f.read(), 'some content') |
| 109 | + |
| 110 | + |
81 | 111 | def test_suite(): |
82 | 112 | return unittest.makeSuite(FileUtilTestCase) |
83 | 113 |
|
|
0 commit comments