NovaBryan/EVE_Dump
Log | Files | 📦 下载最新源码 (.zip) | Refs | README

test_cmd.py (3726B)


      1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\carbon\common\stdlib\distutils\tests\test_cmd.py
      2 import unittest
      3 import os
      4 from test.test_support import captured_stdout, run_unittest
      5 from distutils.cmd import Command
      6 from distutils.dist import Distribution
      7 from distutils.errors import DistutilsOptionError
      8 from distutils import debug
      9 
     10 class MyCmd(Command):
     11 
     12     def initialize_options(self):
     13         pass
     14 
     15 
     16 class CommandTestCase(unittest.TestCase):
     17 
     18     def setUp(self):
     19         dist = Distribution()
     20         self.cmd = MyCmd(dist)
     21 
     22     def test_ensure_string_list(self):
     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         self.assertRaises(DistutilsOptionError, cmd.ensure_string_list, 'not_string_list')
     31         self.assertRaises(DistutilsOptionError, cmd.ensure_string_list, 'not_string_list2')
     32 
     33     def test_make_file(self):
     34         cmd = self.cmd
     35         self.assertRaises(TypeError, cmd.make_file, infiles=1, outfile='', func='func', args=())
     36 
     37         def _execute(func, args, exec_msg, level):
     38             self.assertEqual(exec_msg, 'generating out from in')
     39 
     40         cmd.force = True
     41         cmd.execute = _execute
     42         cmd.make_file(infiles='in', outfile='out', func='func', args=())
     43 
     44     def test_dump_options(self):
     45         msgs = []
     46 
     47         def _announce(msg, level):
     48             msgs.append(msg)
     49 
     50         cmd = self.cmd
     51         cmd.announce = _announce
     52         cmd.option1 = 1
     53         cmd.option2 = 1
     54         cmd.user_options = [('option1', '', ''), ('option2', '', '')]
     55         cmd.dump_options()
     56         wanted = ["command options for 'MyCmd':", '  option1 = 1', '  option2 = 1']
     57         self.assertEqual(msgs, wanted)
     58 
     59     def test_ensure_string(self):
     60         cmd = self.cmd
     61         cmd.option1 = 'ok'
     62         cmd.ensure_string('option1')
     63         cmd.option2 = None
     64         cmd.ensure_string('option2', 'xxx')
     65         self.assertTrue(hasattr(cmd, 'option2'))
     66         cmd.option3 = 1
     67         self.assertRaises(DistutilsOptionError, cmd.ensure_string, 'option3')
     68 
     69     def test_ensure_string_list(self):
     70         cmd = self.cmd
     71         cmd.option1 = 'ok,dok'
     72         cmd.ensure_string_list('option1')
     73         self.assertEqual(cmd.option1, ['ok', 'dok'])
     74         cmd.option2 = ['xxx', 'www']
     75         cmd.ensure_string_list('option2')
     76         cmd.option3 = ['ok', 2]
     77         self.assertRaises(DistutilsOptionError, cmd.ensure_string_list, 'option3')
     78 
     79     def test_ensure_filename(self):
     80         cmd = self.cmd
     81         cmd.option1 = __file__
     82         cmd.ensure_filename('option1')
     83         cmd.option2 = 'xxx'
     84         self.assertRaises(DistutilsOptionError, cmd.ensure_filename, 'option2')
     85 
     86     def test_ensure_dirname(self):
     87         cmd = self.cmd
     88         cmd.option1 = os.path.dirname(__file__)
     89         cmd.ensure_dirname('option1')
     90         cmd.option2 = 'xxx'
     91         self.assertRaises(DistutilsOptionError, cmd.ensure_dirname, 'option2')
     92 
     93     def test_debug_print(self):
     94         cmd = self.cmd
     95         with captured_stdout() as stdout:
     96             cmd.debug_print('xxx')
     97         stdout.seek(0)
     98         self.assertEqual(stdout.read(), '')
     99         debug.DEBUG = True
    100         try:
    101             with captured_stdout() as stdout:
    102                 cmd.debug_print('xxx')
    103             stdout.seek(0)
    104             self.assertEqual(stdout.read(), 'xxx\n')
    105         finally:
    106             debug.DEBUG = False
    107 
    108 
    109 def test_suite():
    110     return unittest.makeSuite(CommandTestCase)
    111 
    112 
    113 if __name__ == '__main__':
    114     run_unittest(test_suite())