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

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

#countcontent
1n/a"""Tests for distutils.cmd."""
2n/aimport unittest
3n/aimport os
4n/afrom test.support import captured_stdout, run_unittest
5n/a
6n/afrom distutils.cmd import Command
7n/afrom distutils.dist import Distribution
8n/afrom distutils.errors import DistutilsOptionError
9n/afrom distutils import debug
10n/a
11n/aclass MyCmd(Command):
12n/a def initialize_options(self):
13n/a pass
14n/a
15n/aclass CommandTestCase(unittest.TestCase):
16n/a
17n/a def setUp(self):
18n/a dist = Distribution()
19n/a self.cmd = MyCmd(dist)
20n/a
21n/a def test_ensure_string_list(self):
22n/a
23n/a cmd = self.cmd
24n/a cmd.not_string_list = ['one', 2, 'three']
25n/a cmd.yes_string_list = ['one', 'two', 'three']
26n/a cmd.not_string_list2 = object()
27n/a cmd.yes_string_list2 = 'ok'
28n/a cmd.ensure_string_list('yes_string_list')
29n/a cmd.ensure_string_list('yes_string_list2')
30n/a
31n/a self.assertRaises(DistutilsOptionError,
32n/a cmd.ensure_string_list, 'not_string_list')
33n/a
34n/a self.assertRaises(DistutilsOptionError,
35n/a cmd.ensure_string_list, 'not_string_list2')
36n/a
37n/a cmd.option1 = 'ok,dok'
38n/a cmd.ensure_string_list('option1')
39n/a self.assertEqual(cmd.option1, ['ok', 'dok'])
40n/a
41n/a cmd.option2 = ['xxx', 'www']
42n/a cmd.ensure_string_list('option2')
43n/a
44n/a cmd.option3 = ['ok', 2]
45n/a self.assertRaises(DistutilsOptionError, cmd.ensure_string_list,
46n/a 'option3')
47n/a
48n/a
49n/a def test_make_file(self):
50n/a
51n/a cmd = self.cmd
52n/a
53n/a # making sure it raises when infiles is not a string or a list/tuple
54n/a self.assertRaises(TypeError, cmd.make_file,
55n/a infiles=1, outfile='', func='func', args=())
56n/a
57n/a # making sure execute gets called properly
58n/a def _execute(func, args, exec_msg, level):
59n/a self.assertEqual(exec_msg, 'generating out from in')
60n/a cmd.force = True
61n/a cmd.execute = _execute
62n/a cmd.make_file(infiles='in', outfile='out', func='func', args=())
63n/a
64n/a def test_dump_options(self):
65n/a
66n/a msgs = []
67n/a def _announce(msg, level):
68n/a msgs.append(msg)
69n/a cmd = self.cmd
70n/a cmd.announce = _announce
71n/a cmd.option1 = 1
72n/a cmd.option2 = 1
73n/a cmd.user_options = [('option1', '', ''), ('option2', '', '')]
74n/a cmd.dump_options()
75n/a
76n/a wanted = ["command options for 'MyCmd':", ' option1 = 1',
77n/a ' option2 = 1']
78n/a self.assertEqual(msgs, wanted)
79n/a
80n/a def test_ensure_string(self):
81n/a cmd = self.cmd
82n/a cmd.option1 = 'ok'
83n/a cmd.ensure_string('option1')
84n/a
85n/a cmd.option2 = None
86n/a cmd.ensure_string('option2', 'xxx')
87n/a self.assertTrue(hasattr(cmd, 'option2'))
88n/a
89n/a cmd.option3 = 1
90n/a self.assertRaises(DistutilsOptionError, cmd.ensure_string, 'option3')
91n/a
92n/a def test_ensure_filename(self):
93n/a cmd = self.cmd
94n/a cmd.option1 = __file__
95n/a cmd.ensure_filename('option1')
96n/a cmd.option2 = 'xxx'
97n/a self.assertRaises(DistutilsOptionError, cmd.ensure_filename, 'option2')
98n/a
99n/a def test_ensure_dirname(self):
100n/a cmd = self.cmd
101n/a cmd.option1 = os.path.dirname(__file__) or os.curdir
102n/a cmd.ensure_dirname('option1')
103n/a cmd.option2 = 'xxx'
104n/a self.assertRaises(DistutilsOptionError, cmd.ensure_dirname, 'option2')
105n/a
106n/a def test_debug_print(self):
107n/a cmd = self.cmd
108n/a with captured_stdout() as stdout:
109n/a cmd.debug_print('xxx')
110n/a stdout.seek(0)
111n/a self.assertEqual(stdout.read(), '')
112n/a
113n/a debug.DEBUG = True
114n/a try:
115n/a with captured_stdout() as stdout:
116n/a cmd.debug_print('xxx')
117n/a stdout.seek(0)
118n/a self.assertEqual(stdout.read(), 'xxx\n')
119n/a finally:
120n/a debug.DEBUG = False
121n/a
122n/adef test_suite():
123n/a return unittest.makeSuite(CommandTestCase)
124n/a
125n/aif __name__ == '__main__':
126n/a run_unittest(test_suite())