1 | n/a | """Tests for distutils.file_util.""" |
---|
2 | n/a | import unittest |
---|
3 | n/a | import os |
---|
4 | n/a | import errno |
---|
5 | n/a | from unittest.mock import patch |
---|
6 | n/a | |
---|
7 | n/a | from distutils.file_util import move_file, copy_file |
---|
8 | n/a | from distutils import log |
---|
9 | n/a | from distutils.tests import support |
---|
10 | n/a | from distutils.errors import DistutilsFileError |
---|
11 | n/a | from test.support import run_unittest |
---|
12 | n/a | |
---|
13 | n/a | class FileUtilTestCase(support.TempdirManager, unittest.TestCase): |
---|
14 | n/a | |
---|
15 | n/a | def _log(self, msg, *args): |
---|
16 | n/a | if len(args) > 0: |
---|
17 | n/a | self._logs.append(msg % args) |
---|
18 | n/a | else: |
---|
19 | n/a | self._logs.append(msg) |
---|
20 | n/a | |
---|
21 | n/a | def setUp(self): |
---|
22 | n/a | super(FileUtilTestCase, self).setUp() |
---|
23 | n/a | self._logs = [] |
---|
24 | n/a | self.old_log = log.info |
---|
25 | n/a | log.info = self._log |
---|
26 | n/a | tmp_dir = self.mkdtemp() |
---|
27 | n/a | self.source = os.path.join(tmp_dir, 'f1') |
---|
28 | n/a | self.target = os.path.join(tmp_dir, 'f2') |
---|
29 | n/a | self.target_dir = os.path.join(tmp_dir, 'd1') |
---|
30 | n/a | |
---|
31 | n/a | def tearDown(self): |
---|
32 | n/a | log.info = self.old_log |
---|
33 | n/a | super(FileUtilTestCase, self).tearDown() |
---|
34 | n/a | |
---|
35 | n/a | def test_move_file_verbosity(self): |
---|
36 | n/a | f = open(self.source, 'w') |
---|
37 | n/a | try: |
---|
38 | n/a | f.write('some content') |
---|
39 | n/a | finally: |
---|
40 | n/a | f.close() |
---|
41 | n/a | |
---|
42 | n/a | move_file(self.source, self.target, verbose=0) |
---|
43 | n/a | wanted = [] |
---|
44 | n/a | self.assertEqual(self._logs, wanted) |
---|
45 | n/a | |
---|
46 | n/a | # back to original state |
---|
47 | n/a | move_file(self.target, self.source, verbose=0) |
---|
48 | n/a | |
---|
49 | n/a | move_file(self.source, self.target, verbose=1) |
---|
50 | n/a | wanted = ['moving %s -> %s' % (self.source, self.target)] |
---|
51 | n/a | self.assertEqual(self._logs, wanted) |
---|
52 | n/a | |
---|
53 | n/a | # back to original state |
---|
54 | n/a | move_file(self.target, self.source, verbose=0) |
---|
55 | n/a | |
---|
56 | n/a | self._logs = [] |
---|
57 | n/a | # now the target is a dir |
---|
58 | n/a | os.mkdir(self.target_dir) |
---|
59 | n/a | move_file(self.source, self.target_dir, verbose=1) |
---|
60 | n/a | wanted = ['moving %s -> %s' % (self.source, self.target_dir)] |
---|
61 | n/a | self.assertEqual(self._logs, wanted) |
---|
62 | n/a | |
---|
63 | n/a | def test_move_file_exception_unpacking_rename(self): |
---|
64 | n/a | # see issue 22182 |
---|
65 | n/a | with patch("os.rename", side_effect=OSError("wrong", 1)), \ |
---|
66 | n/a | self.assertRaises(DistutilsFileError): |
---|
67 | n/a | with open(self.source, 'w') as fobj: |
---|
68 | n/a | fobj.write('spam eggs') |
---|
69 | n/a | move_file(self.source, self.target, verbose=0) |
---|
70 | n/a | |
---|
71 | n/a | def test_move_file_exception_unpacking_unlink(self): |
---|
72 | n/a | # see issue 22182 |
---|
73 | n/a | with patch("os.rename", side_effect=OSError(errno.EXDEV, "wrong")), \ |
---|
74 | n/a | patch("os.unlink", side_effect=OSError("wrong", 1)), \ |
---|
75 | n/a | self.assertRaises(DistutilsFileError): |
---|
76 | n/a | with open(self.source, 'w') as fobj: |
---|
77 | n/a | fobj.write('spam eggs') |
---|
78 | n/a | move_file(self.source, self.target, verbose=0) |
---|
79 | n/a | |
---|
80 | n/a | def test_copy_file_hard_link(self): |
---|
81 | n/a | with open(self.source, 'w') as f: |
---|
82 | n/a | f.write('some content') |
---|
83 | n/a | st = os.stat(self.source) |
---|
84 | n/a | copy_file(self.source, self.target, link='hard') |
---|
85 | n/a | st2 = os.stat(self.source) |
---|
86 | n/a | st3 = os.stat(self.target) |
---|
87 | n/a | self.assertTrue(os.path.samestat(st, st2), (st, st2)) |
---|
88 | n/a | self.assertTrue(os.path.samestat(st2, st3), (st2, st3)) |
---|
89 | n/a | with open(self.source, 'r') as f: |
---|
90 | n/a | self.assertEqual(f.read(), 'some content') |
---|
91 | n/a | |
---|
92 | n/a | def test_copy_file_hard_link_failure(self): |
---|
93 | n/a | # If hard linking fails, copy_file() falls back on copying file |
---|
94 | n/a | # (some special filesystems don't support hard linking even under |
---|
95 | n/a | # Unix, see issue #8876). |
---|
96 | n/a | with open(self.source, 'w') as f: |
---|
97 | n/a | f.write('some content') |
---|
98 | n/a | st = os.stat(self.source) |
---|
99 | n/a | with patch("os.link", side_effect=OSError(0, "linking unsupported")): |
---|
100 | n/a | copy_file(self.source, self.target, link='hard') |
---|
101 | n/a | st2 = os.stat(self.source) |
---|
102 | n/a | st3 = os.stat(self.target) |
---|
103 | n/a | self.assertTrue(os.path.samestat(st, st2), (st, st2)) |
---|
104 | n/a | self.assertFalse(os.path.samestat(st2, st3), (st2, st3)) |
---|
105 | n/a | for fn in (self.source, self.target): |
---|
106 | n/a | with open(fn, 'r') as f: |
---|
107 | n/a | self.assertEqual(f.read(), 'some content') |
---|
108 | n/a | |
---|
109 | n/a | |
---|
110 | n/a | def test_suite(): |
---|
111 | n/a | return unittest.makeSuite(FileUtilTestCase) |
---|
112 | n/a | |
---|
113 | n/a | if __name__ == "__main__": |
---|
114 | n/a | run_unittest(test_suite()) |
---|