1 | n/a | """Tests for distutils.dir_util.""" |
---|
2 | n/a | import unittest |
---|
3 | n/a | import os |
---|
4 | n/a | import stat |
---|
5 | n/a | import sys |
---|
6 | n/a | from unittest.mock import patch |
---|
7 | n/a | |
---|
8 | n/a | from distutils import dir_util, errors |
---|
9 | n/a | from distutils.dir_util import (mkpath, remove_tree, create_tree, copy_tree, |
---|
10 | n/a | ensure_relative) |
---|
11 | n/a | |
---|
12 | n/a | from distutils import log |
---|
13 | n/a | from distutils.tests import support |
---|
14 | n/a | from test.support import run_unittest |
---|
15 | n/a | |
---|
16 | n/a | |
---|
17 | n/a | class DirUtilTestCase(support.TempdirManager, unittest.TestCase): |
---|
18 | n/a | |
---|
19 | n/a | def _log(self, msg, *args): |
---|
20 | n/a | if len(args) > 0: |
---|
21 | n/a | self._logs.append(msg % args) |
---|
22 | n/a | else: |
---|
23 | n/a | self._logs.append(msg) |
---|
24 | n/a | |
---|
25 | n/a | def setUp(self): |
---|
26 | n/a | super(DirUtilTestCase, self).setUp() |
---|
27 | n/a | self._logs = [] |
---|
28 | n/a | tmp_dir = self.mkdtemp() |
---|
29 | n/a | self.root_target = os.path.join(tmp_dir, 'deep') |
---|
30 | n/a | self.target = os.path.join(self.root_target, 'here') |
---|
31 | n/a | self.target2 = os.path.join(tmp_dir, 'deep2') |
---|
32 | n/a | self.old_log = log.info |
---|
33 | n/a | log.info = self._log |
---|
34 | n/a | |
---|
35 | n/a | def tearDown(self): |
---|
36 | n/a | log.info = self.old_log |
---|
37 | n/a | super(DirUtilTestCase, self).tearDown() |
---|
38 | n/a | |
---|
39 | n/a | def test_mkpath_remove_tree_verbosity(self): |
---|
40 | n/a | |
---|
41 | n/a | mkpath(self.target, verbose=0) |
---|
42 | n/a | wanted = [] |
---|
43 | n/a | self.assertEqual(self._logs, wanted) |
---|
44 | n/a | remove_tree(self.root_target, verbose=0) |
---|
45 | n/a | |
---|
46 | n/a | mkpath(self.target, verbose=1) |
---|
47 | n/a | wanted = ['creating %s' % self.root_target, |
---|
48 | n/a | 'creating %s' % self.target] |
---|
49 | n/a | self.assertEqual(self._logs, wanted) |
---|
50 | n/a | self._logs = [] |
---|
51 | n/a | |
---|
52 | n/a | remove_tree(self.root_target, verbose=1) |
---|
53 | n/a | wanted = ["removing '%s' (and everything under it)" % self.root_target] |
---|
54 | n/a | self.assertEqual(self._logs, wanted) |
---|
55 | n/a | |
---|
56 | n/a | @unittest.skipIf(sys.platform.startswith('win'), |
---|
57 | n/a | "This test is only appropriate for POSIX-like systems.") |
---|
58 | n/a | def test_mkpath_with_custom_mode(self): |
---|
59 | n/a | # Get and set the current umask value for testing mode bits. |
---|
60 | n/a | umask = os.umask(0o002) |
---|
61 | n/a | os.umask(umask) |
---|
62 | n/a | mkpath(self.target, 0o700) |
---|
63 | n/a | self.assertEqual( |
---|
64 | n/a | stat.S_IMODE(os.stat(self.target).st_mode), 0o700 & ~umask) |
---|
65 | n/a | mkpath(self.target2, 0o555) |
---|
66 | n/a | self.assertEqual( |
---|
67 | n/a | stat.S_IMODE(os.stat(self.target2).st_mode), 0o555 & ~umask) |
---|
68 | n/a | |
---|
69 | n/a | def test_create_tree_verbosity(self): |
---|
70 | n/a | |
---|
71 | n/a | create_tree(self.root_target, ['one', 'two', 'three'], verbose=0) |
---|
72 | n/a | self.assertEqual(self._logs, []) |
---|
73 | n/a | remove_tree(self.root_target, verbose=0) |
---|
74 | n/a | |
---|
75 | n/a | wanted = ['creating %s' % self.root_target] |
---|
76 | n/a | create_tree(self.root_target, ['one', 'two', 'three'], verbose=1) |
---|
77 | n/a | self.assertEqual(self._logs, wanted) |
---|
78 | n/a | |
---|
79 | n/a | remove_tree(self.root_target, verbose=0) |
---|
80 | n/a | |
---|
81 | n/a | def test_copy_tree_verbosity(self): |
---|
82 | n/a | |
---|
83 | n/a | mkpath(self.target, verbose=0) |
---|
84 | n/a | |
---|
85 | n/a | copy_tree(self.target, self.target2, verbose=0) |
---|
86 | n/a | self.assertEqual(self._logs, []) |
---|
87 | n/a | |
---|
88 | n/a | remove_tree(self.root_target, verbose=0) |
---|
89 | n/a | |
---|
90 | n/a | mkpath(self.target, verbose=0) |
---|
91 | n/a | a_file = os.path.join(self.target, 'ok.txt') |
---|
92 | n/a | with open(a_file, 'w') as f: |
---|
93 | n/a | f.write('some content') |
---|
94 | n/a | |
---|
95 | n/a | wanted = ['copying %s -> %s' % (a_file, self.target2)] |
---|
96 | n/a | copy_tree(self.target, self.target2, verbose=1) |
---|
97 | n/a | self.assertEqual(self._logs, wanted) |
---|
98 | n/a | |
---|
99 | n/a | remove_tree(self.root_target, verbose=0) |
---|
100 | n/a | remove_tree(self.target2, verbose=0) |
---|
101 | n/a | |
---|
102 | n/a | def test_copy_tree_skips_nfs_temp_files(self): |
---|
103 | n/a | mkpath(self.target, verbose=0) |
---|
104 | n/a | |
---|
105 | n/a | a_file = os.path.join(self.target, 'ok.txt') |
---|
106 | n/a | nfs_file = os.path.join(self.target, '.nfs123abc') |
---|
107 | n/a | for f in a_file, nfs_file: |
---|
108 | n/a | with open(f, 'w') as fh: |
---|
109 | n/a | fh.write('some content') |
---|
110 | n/a | |
---|
111 | n/a | copy_tree(self.target, self.target2) |
---|
112 | n/a | self.assertEqual(os.listdir(self.target2), ['ok.txt']) |
---|
113 | n/a | |
---|
114 | n/a | remove_tree(self.root_target, verbose=0) |
---|
115 | n/a | remove_tree(self.target2, verbose=0) |
---|
116 | n/a | |
---|
117 | n/a | def test_ensure_relative(self): |
---|
118 | n/a | if os.sep == '/': |
---|
119 | n/a | self.assertEqual(ensure_relative('/home/foo'), 'home/foo') |
---|
120 | n/a | self.assertEqual(ensure_relative('some/path'), 'some/path') |
---|
121 | n/a | else: # \\ |
---|
122 | n/a | self.assertEqual(ensure_relative('c:\\home\\foo'), 'c:home\\foo') |
---|
123 | n/a | self.assertEqual(ensure_relative('home\\foo'), 'home\\foo') |
---|
124 | n/a | |
---|
125 | n/a | def test_copy_tree_exception_in_listdir(self): |
---|
126 | n/a | """ |
---|
127 | n/a | An exception in listdir should raise a DistutilsFileError |
---|
128 | n/a | """ |
---|
129 | n/a | with patch("os.listdir", side_effect=OSError()), \ |
---|
130 | n/a | self.assertRaises(errors.DistutilsFileError): |
---|
131 | n/a | src = self.tempdirs[-1] |
---|
132 | n/a | dir_util.copy_tree(src, None) |
---|
133 | n/a | |
---|
134 | n/a | |
---|
135 | n/a | def test_suite(): |
---|
136 | n/a | return unittest.makeSuite(DirUtilTestCase) |
---|
137 | n/a | |
---|
138 | n/a | if __name__ == "__main__": |
---|
139 | n/a | run_unittest(test_suite()) |
---|