ยปCore Development>Code coverage>Lib/distutils/tests/test_dir_util.py

Python code coverage for Lib/distutils/tests/test_dir_util.py

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