home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_2732 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  7.4 KB  |  226 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import sys
  5. import os
  6. import string
  7. import re
  8. import pythoncom
  9. import win32com.client as win32com
  10. from util import CheckClean, TestCase, CapturingFunctionTestCase, TestLoader
  11. import win32com.test.util as win32com
  12. import traceback
  13. import getopt
  14. import unittest
  15. verbosity = 1
  16.  
  17. try:
  18.     this_file = __file__
  19. except NameError:
  20.     this_file = sys.argv[0]
  21.  
  22.  
  23. def GenerateAndRunOldStyle():
  24.     import GenTestScripts
  25.     GenTestScripts.GenerateAll()
  26.     
  27.     try:
  28.         pass
  29.     finally:
  30.         GenTestScripts.CleanAll()
  31.  
  32.  
  33.  
  34. def CleanGenerated():
  35.     import win32com
  36.     import shutil
  37.     if os.path.isdir(win32com.__gen_path__):
  38.         if verbosity > 1:
  39.             print 'Deleting files from %s' % win32com.__gen_path__
  40.         
  41.         shutil.rmtree(win32com.__gen_path__)
  42.     
  43.     import win32com.client.gencache as win32com
  44.     win32com.client.gencache.__init__()
  45.  
  46.  
  47. def RemoveRefCountOutput(data):
  48.     while None:
  49.         last_line_pos = data.rfind('\n')
  50.         if not re.match('\\[\\d+ refs\\]', data[last_line_pos + 1:]):
  51.             break
  52.         
  53.         if last_line_pos < 0:
  54.             return ''
  55.         data = data[:last_line_pos]
  56.         continue
  57.         return data
  58.  
  59.  
  60. def ExecuteSilentlyIfOK(cmd, testcase):
  61.     f = os.popen(cmd)
  62.     data = f.read().strip()
  63.     rc = f.close()
  64.     if rc:
  65.         print data
  66.         testcase.fail("Executing '%s' failed (%d)" % (cmd, rc))
  67.     
  68.     return RemoveRefCountOutput(data)
  69.  
  70.  
  71. class PyCOMTest(TestCase):
  72.     no_leak_tests = True
  73.     
  74.     def testit(self):
  75.         fname = os.path.join(os.path.dirname(this_file), 'testPyComTest.py')
  76.         cmd = '%s "%s" -q 2>&1' % (sys.executable, fname)
  77.         data = ExecuteSilentlyIfOK(cmd, self)
  78.  
  79.  
  80.  
  81. class PippoTest(TestCase):
  82.     
  83.     def testit(self):
  84.         python = sys.executable
  85.         fname = os.path.join(os.path.dirname(this_file), 'testPippo.py')
  86.         cmd = '%s "%s" 2>&1' % (python, fname)
  87.         ExecuteSilentlyIfOK(cmd, self)
  88.  
  89.  
  90. unittest_modules = [
  91.     'testIterators testvbscript_regexp testStorage \n          testStreams testWMI policySemantics testShell testROT\n          testAXScript testxslt testDictionary testCollections\n          testServers errorSemantics.test testvb testArrays\n          testClipboard testMarshal\n        '.split(),
  92.     'testMSOffice.TestAll testMSOfficeEvents.test testAccess.test\n           testExplorer.TestAll testExchange.test\n        '.split(),
  93.     'testmakepy.TestAll\n        '.split()]
  94. output_checked_programs = [
  95.     [
  96.         ('cscript.exe /nologo //E:vbscript testInterp.vbs', 'VBScript test worked OK'),
  97.         ('cscript.exe /nologo //E:vbscript testDictionary.vbs', 'VBScript has successfully tested Python.Dictionary')],
  98.     [],
  99.     []]
  100. custom_test_cases = [
  101.     [
  102.         PyCOMTest,
  103.         PippoTest],
  104.     [],
  105.     []]
  106.  
  107. def get_test_mod_and_func(test_name, import_failures):
  108.     if test_name.find('.') > 0:
  109.         (mod_name, func_name) = test_name.split('.')
  110.     else:
  111.         mod_name = test_name
  112.         func_name = None
  113.     
  114.     try:
  115.         mod = __import__(mod_name)
  116.     except:
  117.         import_failures.append((mod_name, sys.exc_info()[:2]))
  118.         return (None, None)
  119.  
  120.     if func_name is None:
  121.         func = None
  122.     else:
  123.         func = getattr(mod, func_name)
  124.     return (mod, func)
  125.  
  126.  
  127. def make_test_suite(test_level = 1):
  128.     suite = unittest.TestSuite()
  129.     import_failures = []
  130.     loader = TestLoader()
  131.     for i in range(testLevel):
  132.         for mod_name in unittest_modules[i]:
  133.             (mod, func) = get_test_mod_and_func(mod_name, import_failures)
  134.             if mod is None:
  135.                 continue
  136.             
  137.             if func is not None:
  138.                 test = win32com.test.util.CapturingFunctionTestCase(func, description = mod_name)
  139.             elif hasattr(mod, 'suite'):
  140.                 test = mod.suite()
  141.             else:
  142.                 test = loader.loadTestsFromModule(mod)
  143.             suite.addTest(test)
  144.         
  145.         for cmd, output in output_checked_programs[i]:
  146.             suite.addTest(win32com.test.util.ShellTestCase(cmd, output))
  147.         
  148.         for test_class in custom_test_cases[i]:
  149.             suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(test_class))
  150.         
  151.     
  152.     return (suite, import_failures)
  153.  
  154.  
  155. def usage(why):
  156.     print why
  157.     print 
  158.     print 'win32com test suite'
  159.     print 'usage: testall [-v] test_level'
  160.     print '  where test_level is an integer 1-3.  Level 1 tests are quick,'
  161.     print '  level 2 tests invoke Word, IE etc, level 3 take ages!'
  162.     sys.exit(1)
  163.  
  164. if __name__ == '__main__':
  165.     
  166.     try:
  167.         (opts, args) = getopt.getopt(sys.argv[1:], 'v')
  168.     except getopt.error:
  169.         why = None
  170.         usage(why)
  171.  
  172.     for opt, val in opts:
  173.         if opt == '-v':
  174.             verbosity += 1
  175.             continue
  176.     
  177.     testLevel = 1
  178.     test_names = []
  179.     for arg in args:
  180.         
  181.         try:
  182.             testLevel = int(arg)
  183.             if testLevel < 0 or testLevel > 3:
  184.                 raise ValueError, 'Only levels 1-3 are supported'
  185.             testLevel > 3
  186.         continue
  187.         except ValueError:
  188.             test_names.append(arg)
  189.             continue
  190.         
  191.  
  192.     
  193.     if test_names:
  194.         usage('Test names are not supported yet')
  195.     
  196.     CleanGenerated()
  197.     (suite, import_failures) = make_test_suite(testLevel)
  198.     if verbosity:
  199.         if hasattr(sys, 'gettotalrefcount'):
  200.             print 'This is a debug build - memory leak tests will also be run.'
  201.             print 'These tests may take *many* minutes to run - be patient!'
  202.             print '(running from python.exe will avoid these leak tests)'
  203.         
  204.         print 'Executing level %d tests - %d test cases will be run' % (testLevel, suite.countTestCases())
  205.         if verbosity == 1 and suite.countTestCases() < 70:
  206.             print '|' * suite.countTestCases()
  207.         
  208.     
  209.     testRunner = unittest.TextTestRunner(verbosity = verbosity)
  210.     testResult = testRunner.run(suite)
  211.     if import_failures:
  212.         testResult.stream.writeln('*** The following test modules could not be imported ***')
  213.         for exc_type, exc_val in import_failures:
  214.             desc = testResult._exc_info_to_string((exc_type, exc_val, None))
  215.             testResult.stream.write('%s: %s' % (mod_name, desc))
  216.         
  217.         testResult.stream.writeln('*** %d test(s) could not be run ***' % len(import_failures))
  218.     
  219.     if not testResult.wasSuccessful():
  220.         print '********************', '- unittest tests FAILED'
  221.     
  222.     CheckClean()
  223.     pythoncom.CoUninitialize()
  224.     CleanGenerated()
  225.  
  226.