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