Tarek Ziadé | 70a74eb | 2009-02-06 00:38:35 +0000 | [diff] [blame] | 1 | """Tests for distutils.file_util.""" |
| 2 | import unittest |
| 3 | import os |
Berker Peksag | 6685883 | 2014-08-29 07:07:35 +0300 | [diff] [blame] | 4 | import errno |
| 5 | from unittest.mock import patch |
Tarek Ziadé | 70a74eb | 2009-02-06 00:38:35 +0000 | [diff] [blame] | 6 | |
Antoine Pitrou | ed14c86 | 2014-10-30 19:37:07 +0100 | [diff] [blame] | 7 | from distutils.file_util import move_file, copy_file |
Tarek Ziadé | 70a74eb | 2009-02-06 00:38:35 +0000 | [diff] [blame] | 8 | from distutils import log |
Tarek Ziadé | c1375d5 | 2009-02-14 14:35:51 +0000 | [diff] [blame] | 9 | from distutils.tests import support |
Berker Peksag | 6685883 | 2014-08-29 07:07:35 +0300 | [diff] [blame] | 10 | from distutils.errors import DistutilsFileError |
xdegaye | d34d8fc | 2017-11-18 18:17:16 +0100 | [diff] [blame] | 11 | from test.support import run_unittest, unlink |
Tarek Ziadé | 70a74eb | 2009-02-06 00:38:35 +0000 | [diff] [blame] | 12 | |
Tarek Ziadé | c1375d5 | 2009-02-14 14:35:51 +0000 | [diff] [blame] | 13 | class FileUtilTestCase(support.TempdirManager, unittest.TestCase): |
Tarek Ziadé | 70a74eb | 2009-02-06 00:38:35 +0000 | [diff] [blame] | 14 | |
| 15 | def _log(self, msg, *args): |
| 16 | if len(args) > 0: |
| 17 | self._logs.append(msg % args) |
| 18 | else: |
| 19 | self._logs.append(msg) |
| 20 | |
| 21 | def setUp(self): |
Hirokazu Yamamoto | 2703fd9 | 2009-05-29 09:14:04 +0000 | [diff] [blame] | 22 | super(FileUtilTestCase, self).setUp() |
Tarek Ziadé | 70a74eb | 2009-02-06 00:38:35 +0000 | [diff] [blame] | 23 | self._logs = [] |
| 24 | self.old_log = log.info |
| 25 | log.info = self._log |
Tarek Ziadé | c1375d5 | 2009-02-14 14:35:51 +0000 | [diff] [blame] | 26 | tmp_dir = self.mkdtemp() |
| 27 | self.source = os.path.join(tmp_dir, 'f1') |
| 28 | self.target = os.path.join(tmp_dir, 'f2') |
| 29 | self.target_dir = os.path.join(tmp_dir, 'd1') |
Tarek Ziadé | 70a74eb | 2009-02-06 00:38:35 +0000 | [diff] [blame] | 30 | |
| 31 | def tearDown(self): |
| 32 | log.info = self.old_log |
Hirokazu Yamamoto | 2703fd9 | 2009-05-29 09:14:04 +0000 | [diff] [blame] | 33 | super(FileUtilTestCase, self).tearDown() |
Tarek Ziadé | 70a74eb | 2009-02-06 00:38:35 +0000 | [diff] [blame] | 34 | |
| 35 | def test_move_file_verbosity(self): |
Tarek Ziadé | 70a74eb | 2009-02-06 00:38:35 +0000 | [diff] [blame] | 36 | f = open(self.source, 'w') |
Éric Araujo | bee5cef | 2010-11-05 23:51:56 +0000 | [diff] [blame] | 37 | try: |
| 38 | f.write('some content') |
| 39 | finally: |
| 40 | f.close() |
Tarek Ziadé | 70a74eb | 2009-02-06 00:38:35 +0000 | [diff] [blame] | 41 | |
| 42 | move_file(self.source, self.target, verbose=0) |
| 43 | wanted = [] |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 44 | self.assertEqual(self._logs, wanted) |
Tarek Ziadé | 70a74eb | 2009-02-06 00:38:35 +0000 | [diff] [blame] | 45 | |
| 46 | # back to original state |
| 47 | move_file(self.target, self.source, verbose=0) |
| 48 | |
| 49 | move_file(self.source, self.target, verbose=1) |
| 50 | wanted = ['moving %s -> %s' % (self.source, self.target)] |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 51 | self.assertEqual(self._logs, wanted) |
Tarek Ziadé | 70a74eb | 2009-02-06 00:38:35 +0000 | [diff] [blame] | 52 | |
| 53 | # back to original state |
| 54 | move_file(self.target, self.source, verbose=0) |
| 55 | |
| 56 | self._logs = [] |
| 57 | # now the target is a dir |
| 58 | os.mkdir(self.target_dir) |
| 59 | move_file(self.source, self.target_dir, verbose=1) |
| 60 | wanted = ['moving %s -> %s' % (self.source, self.target_dir)] |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 61 | self.assertEqual(self._logs, wanted) |
Tarek Ziadé | 70a74eb | 2009-02-06 00:38:35 +0000 | [diff] [blame] | 62 | |
Benjamin Peterson | 9833fcb | 2014-09-20 11:53:12 -0400 | [diff] [blame] | 63 | def test_move_file_exception_unpacking_rename(self): |
Berker Peksag | 6685883 | 2014-08-29 07:07:35 +0300 | [diff] [blame] | 64 | # see issue 22182 |
Benjamin Peterson | 9833fcb | 2014-09-20 11:53:12 -0400 | [diff] [blame] | 65 | with patch("os.rename", side_effect=OSError("wrong", 1)), \ |
| 66 | self.assertRaises(DistutilsFileError): |
Berker Peksag | 6685883 | 2014-08-29 07:07:35 +0300 | [diff] [blame] | 67 | with open(self.source, 'w') as fobj: |
| 68 | fobj.write('spam eggs') |
| 69 | move_file(self.source, self.target, verbose=0) |
| 70 | |
Benjamin Peterson | 9833fcb | 2014-09-20 11:53:12 -0400 | [diff] [blame] | 71 | def test_move_file_exception_unpacking_unlink(self): |
Berker Peksag | 6685883 | 2014-08-29 07:07:35 +0300 | [diff] [blame] | 72 | # see issue 22182 |
Benjamin Peterson | 9833fcb | 2014-09-20 11:53:12 -0400 | [diff] [blame] | 73 | with patch("os.rename", side_effect=OSError(errno.EXDEV, "wrong")), \ |
| 74 | patch("os.unlink", side_effect=OSError("wrong", 1)), \ |
| 75 | self.assertRaises(DistutilsFileError): |
Berker Peksag | 6685883 | 2014-08-29 07:07:35 +0300 | [diff] [blame] | 76 | with open(self.source, 'w') as fobj: |
| 77 | fobj.write('spam eggs') |
| 78 | move_file(self.source, self.target, verbose=0) |
| 79 | |
Antoine Pitrou | ed14c86 | 2014-10-30 19:37:07 +0100 | [diff] [blame] | 80 | def test_copy_file_hard_link(self): |
| 81 | with open(self.source, 'w') as f: |
| 82 | f.write('some content') |
xdegaye | d34d8fc | 2017-11-18 18:17:16 +0100 | [diff] [blame] | 83 | # Check first that copy_file() will not fall back on copying the file |
| 84 | # instead of creating the hard link. |
| 85 | try: |
| 86 | os.link(self.source, self.target) |
| 87 | except OSError as e: |
| 88 | self.skipTest('os.link: %s' % e) |
| 89 | else: |
| 90 | unlink(self.target) |
Antoine Pitrou | ed14c86 | 2014-10-30 19:37:07 +0100 | [diff] [blame] | 91 | st = os.stat(self.source) |
| 92 | copy_file(self.source, self.target, link='hard') |
| 93 | st2 = os.stat(self.source) |
| 94 | st3 = os.stat(self.target) |
| 95 | self.assertTrue(os.path.samestat(st, st2), (st, st2)) |
| 96 | self.assertTrue(os.path.samestat(st2, st3), (st2, st3)) |
| 97 | with open(self.source, 'r') as f: |
| 98 | self.assertEqual(f.read(), 'some content') |
| 99 | |
| 100 | def test_copy_file_hard_link_failure(self): |
| 101 | # If hard linking fails, copy_file() falls back on copying file |
| 102 | # (some special filesystems don't support hard linking even under |
| 103 | # Unix, see issue #8876). |
| 104 | with open(self.source, 'w') as f: |
| 105 | f.write('some content') |
| 106 | st = os.stat(self.source) |
| 107 | with patch("os.link", side_effect=OSError(0, "linking unsupported")): |
| 108 | copy_file(self.source, self.target, link='hard') |
| 109 | st2 = os.stat(self.source) |
| 110 | st3 = os.stat(self.target) |
| 111 | self.assertTrue(os.path.samestat(st, st2), (st, st2)) |
| 112 | self.assertFalse(os.path.samestat(st2, st3), (st2, st3)) |
| 113 | for fn in (self.source, self.target): |
| 114 | with open(fn, 'r') as f: |
| 115 | self.assertEqual(f.read(), 'some content') |
| 116 | |
| 117 | |
Tarek Ziadé | 70a74eb | 2009-02-06 00:38:35 +0000 | [diff] [blame] | 118 | def test_suite(): |
| 119 | return unittest.makeSuite(FileUtilTestCase) |
| 120 | |
| 121 | if __name__ == "__main__": |
Éric Araujo | 70ec44a | 2010-11-06 02:44:43 +0000 | [diff] [blame] | 122 | run_unittest(test_suite()) |