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

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

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