Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / trace-viewer / third_party / tvcm / tvcm / test_runner.py
1 #!/usr/bin/env python
2 # Copyright (c) 2014 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
5 import inspect
6 import unittest
7 import sys
8 import os
9 import optparse
10
11 __all__ = []
12
13 def FilterSuite(suite, predicate):
14   if hasattr(suite, 'recreateEmptyVersion'):
15     new_suite = suite.recreateEmptyVersion()
16   else:
17     new_suite = suite.__class__()
18
19   for x in suite:
20     if isinstance(x, unittest.TestSuite):
21       subsuite = FilterSuite(x, predicate)
22       if subsuite.countTestCases() == 0:
23         continue
24
25       new_suite.addTest(subsuite)
26       continue
27
28     assert isinstance(x, unittest.TestCase)
29     if predicate(x):
30       new_suite.addTest(x)
31
32   return new_suite
33
34 class _TestLoader(unittest.TestLoader):
35   def __init__(self, *args):
36     super(_TestLoader, self).__init__(*args)
37     self.discover_calls = []
38
39   def loadTestsFromModule(self, module, use_load_tests=True):
40     if module.__file__ != __file__:
41       return super(_TestLoader, self).loadTestsFromModule(module, use_load_tests)
42
43     suite = unittest.TestSuite()
44     for discover_args in self.discover_calls:
45       subsuite = self.discover(*discover_args)
46       suite.addTest(subsuite)
47     return suite
48
49 class _RunnerImpl(unittest.TextTestRunner):
50   def __init__(self, filters):
51     super(_RunnerImpl, self).__init__(verbosity=2)
52     self.filters = filters
53
54   def ShouldTestRun(self, test):
55     if len(self.filters) == 0:
56       return True
57     found = False
58     for name in self.filters:
59       if name in test.id():
60         found = True
61     return found
62
63   def run(self, suite):
64     filtered_test = FilterSuite(suite, self.ShouldTestRun)
65     return super(_RunnerImpl, self).run(filtered_test)
66
67 PY_ONLY_TESTS=False
68
69
70 class TestRunner(object):
71   def __init__(self):
72     self._loader = _TestLoader()
73
74   def AddModule(self, module, pattern="*unittest.py"):
75     assert inspect.ismodule(module)
76     module_file_basename = os.path.splitext(os.path.basename(module.__file__))[0]
77     if module_file_basename != '__init__':
78       raise NotImplementedError('Modules that are one file are not supported, only directories.')
79
80     file_basename = os.path.basename(os.path.dirname(module.__file__))
81     module_first_dir = module.__name__.split('.')[0]
82     assert file_basename == module_first_dir, 'Module must be toplevel'
83
84     start_dir = os.path.dirname(module.__file__)
85     top_dir = os.path.normpath(os.path.join(os.path.dirname(module.__file__), '..'))
86     self._loader.discover_calls.append((start_dir, pattern, top_dir))
87
88   def Main(self, argv=None):
89     if argv == None:
90       argv = sys.argv
91
92     parser = optparse.OptionParser()
93     parser.add_option('--py-only', action='store_true',
94                       help='Runs only python based tests')
95     options, args = parser.parse_args(argv[1:])
96
97     global PY_ONLY_TESTS
98     PY_ONLY_TESTS = options.py_only
99
100     runner = _RunnerImpl(filters=args)
101     return unittest.main(module=__name__, argv=[sys.argv[0]],
102                          testLoader=self._loader,
103                          testRunner=runner)