configurationengine/build-scripts/export-bat/scripts-tests-overlay/runtests.py
changeset 0 2e8eeb919028
equal deleted inserted replaced
-1:000000000000 0:2e8eeb919028
       
     1 #
       
     2 # Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 # All rights reserved.
       
     4 # This component and the accompanying materials are made available
       
     5 # under the terms of "Eclipse Public License v1.0"
       
     6 # which accompanies this distribution, and is available
       
     7 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 #
       
     9 # Initial Contributors:
       
    10 # Nokia Corporation - initial contribution.
       
    11 #
       
    12 # Contributors:
       
    13 #
       
    14 # Description: 
       
    15 #
       
    16 
       
    17 import sys, os, re, unittest
       
    18 
       
    19 ROOT_PATH = os.path.abspath(os.path.dirname(__file__))
       
    20 
       
    21 CONE_PATH = os.path.join(ROOT_PATH, 'cone')
       
    22 EGGS_PATH = os.path.join(ROOT_PATH, 'eggs')
       
    23 
       
    24 def add_eggs_to_path(egg_path):
       
    25     if not os.path.isdir(egg_path):
       
    26         return
       
    27     
       
    28     for name in os.listdir(egg_path):
       
    29         if name.endswith('.egg'):
       
    30             path = os.path.normpath(os.path.join(egg_path, name))
       
    31             if path not in sys.path:
       
    32                 print "Adding '%s' to path" % name
       
    33                 sys.path.append(path)
       
    34 
       
    35 
       
    36 def _collect_test_suite(base_dir, module_subdir):
       
    37     # Check that the directory exists
       
    38     module_dir = os.path.join(base_dir, module_subdir)
       
    39     if not os.path.exists(module_dir):
       
    40         raise RuntimeError("'%s' does not exist!" % module_dir)
       
    41     
       
    42     # Collect the names of all test modules (of the form "unittest_*.py")
       
    43     test_modules_names = []
       
    44     for name in os.listdir(module_dir):
       
    45         if re.match(r'^unittest_.*\.py$', name) != None:
       
    46             test_modules_names.append(name[:-3])
       
    47     
       
    48     # Import the modules
       
    49     sys.path.insert(0, base_dir)
       
    50     try:
       
    51         suite = unittest.TestSuite()
       
    52         for modname in test_modules_names:
       
    53             # Load the test module dynamically and add it to the test suite
       
    54             top_module = __import__(module_subdir + '.' + modname)
       
    55             
       
    56             # top_module contains now actually e.g. mymodule.unittest_sometest,
       
    57             # so get the actual unit test module
       
    58             module = getattr(top_module, modname)
       
    59             
       
    60             suite.addTests(unittest.TestLoader().loadTestsFromModule(module))
       
    61         return suite
       
    62     finally:
       
    63         del sys.path[0]
       
    64 
       
    65 def _run_without_nose():
       
    66     suite = unittest.TestSuite()
       
    67     
       
    68     # Collect script test suite
       
    69     suite.addTest(_collect_test_suite(ROOT_PATH, 'tests'))
       
    70     
       
    71     # Collect test suites from the plugin-tests/ directory
       
    72     PLUGIN_TEST_DIR = os.path.join(ROOT_PATH, 'plugin-tests')
       
    73     if os.path.exists(PLUGIN_TEST_DIR):
       
    74         for name in os.listdir(PLUGIN_TEST_DIR):
       
    75             plugin_suite = _collect_test_suite(PLUGIN_TEST_DIR, name)
       
    76             suite.addTest(plugin_suite)
       
    77     
       
    78     # Run the tests
       
    79     unittest.TextTestRunner(verbosity=2).run(suite)
       
    80 
       
    81 def _run_with_nose():
       
    82     import nose
       
    83     import nose.plugins.xunit
       
    84     
       
    85     # Configure nose
       
    86     plugins = nose.plugins.manager.DefaultPluginManager()
       
    87     allfiles = nose.config.all_config_files() + ['nose_bat.cfg']
       
    88     conf = nose.config.Config(files=allfiles, plugins=plugins)
       
    89     
       
    90     # Run the tests
       
    91     nose.run(config=conf, argv=sys.argv[:1])
       
    92 
       
    93 def main():
       
    94     if 'CONE_PATH' not in os.environ:
       
    95         cone_path = os.path.join(ROOT_PATH, 'cone')
       
    96         print "Setting CONE_PATH to '%s'" % cone_path
       
    97         os.environ['CONE_PATH'] = cone_path
       
    98     
       
    99     add_eggs_to_path(EGGS_PATH)
       
   100     
       
   101     if '--with-nose' in sys.argv:
       
   102         _run_with_nose()
       
   103     else:
       
   104         _run_without_nose()
       
   105     return 0
       
   106 
       
   107 if __name__ == "__main__":
       
   108     sys.exit(main())