| 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 |
cmd.option1 = 'ok,dok' |
| 38 |
cmd.ensure_string_list('option1') |
| 39 |
self.assertEqual(cmd.option1, ['ok', 'dok']) |
| 40 |
|
| 41 |
cmd.option2 = ['xxx', 'www'] |
| 42 |
cmd.ensure_string_list('option2') |
| 43 |
|
| 44 |
cmd.option3 = ['ok', 2] |
| 45 |
self.assertRaises(DistutilsOptionError, cmd.ensure_string_list, |
| 46 |
'option3') |
| 47 |
|
| 48 |
|
| 49 |
def test_make_file(self): |
| 50 |
|
| 51 |
cmd = self.cmd |
| 52 |
|
| 53 |
|
| 54 |
self.assertRaises(TypeError, cmd.make_file, |
| 55 |
infiles=1, outfile='', func='func', args=()) |
| 56 |
|
| 57 |
|
| 58 |
def _execute(func, args, exec_msg, level): |
| 59 |
self.assertEqual(exec_msg, 'generating out from in') |
| 60 |
cmd.force = True |
| 61 |
cmd.execute = _execute |
| 62 |
cmd.make_file(infiles='in', outfile='out', func='func', args=()) |
| 63 |
|
| 64 |
def test_dump_options(self): |
| 65 |
|
| 66 |
msgs = [] |
| 67 |
def _announce(msg, level): |
| 68 |
msgs.append(msg) |
| 69 |
cmd = self.cmd |
| 70 |
cmd.announce = _announce |
| 71 |
cmd.option1 = 1 |
| 72 |
cmd.option2 = 1 |
| 73 |
cmd.user_options = [('option1', '', ''), ('option2', '', '')] |
| 74 |
cmd.dump_options() |
| 75 |
|
| 76 |
wanted = ["command options for 'MyCmd':", ' option1 = 1', |
| 77 |
' option2 = 1'] |
| 78 |
self.assertEqual(msgs, wanted) |
| 79 |
|
| 80 |
def test_ensure_string(self): |
| 81 |
cmd = self.cmd |
| 82 |
cmd.option1 = 'ok' |
| 83 |
cmd.ensure_string('option1') |
| 84 |
|
| 85 |
cmd.option2 = None |
| 86 |
cmd.ensure_string('option2', 'xxx') |
| 87 |
self.assertTrue(hasattr(cmd, 'option2')) |
| 88 |
|
| 89 |
cmd.option3 = 1 |
| 90 |
self.assertRaises(DistutilsOptionError, cmd.ensure_string, 'option3') |
| 91 |
|
| 92 |
def test_ensure_filename(self): |
| 93 |
cmd = self.cmd |
| 94 |
cmd.option1 = __file__ |
| 95 |
cmd.ensure_filename('option1') |
| 96 |
cmd.option2 = 'xxx' |
| 97 |
self.assertRaises(DistutilsOptionError, cmd.ensure_filename, 'option2') |
| 98 |
|
| 99 |
def test_ensure_dirname(self): |
| 100 |
cmd = self.cmd |
| 101 |
cmd.option1 = os.path.dirname(__file__) or os.curdir |
| 102 |
cmd.ensure_dirname('option1') |
| 103 |
cmd.option2 = 'xxx' |
| 104 |
self.assertRaises(DistutilsOptionError, cmd.ensure_dirname, 'option2') |
| 105 |
|
| 106 |
def test_debug_print(self): |
| 107 |
cmd = self.cmd |
| 108 |
with captured_stdout() as stdout: |
| 109 |
cmd.debug_print('xxx') |
| 110 |
stdout.seek(0) |
| 111 |
self.assertEqual(stdout.read(), '') |
| 112 |
|
| 113 |
debug.DEBUG = True |
| 114 |
try: |
| 115 |
with captured_stdout() as stdout: |
| 116 |
cmd.debug_print('xxx') |
| 117 |
stdout.seek(0) |
| 118 |
self.assertEqual(stdout.read(), 'xxx\n') |
| 119 |
finally: |
| 120 |
debug.DEBUG = False |
| 121 |
|
| 122 |
def test_suite(): |
| 123 |
return unittest.makeSuite(CommandTestCase) |
| 124 |
|
| 125 |
if __name__ == '__main__': |
| 126 |
run_unittest(test_suite()) |