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