Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
michael@0 | 2 | # Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | # found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | """Module containing information about the python-driven tests.""" |
michael@0 | 6 | |
michael@0 | 7 | import logging |
michael@0 | 8 | import os |
michael@0 | 9 | |
michael@0 | 10 | import tests_annotations |
michael@0 | 11 | |
michael@0 | 12 | |
michael@0 | 13 | class TestInfo(object): |
michael@0 | 14 | """An object containing and representing a test function, plus metadata.""" |
michael@0 | 15 | |
michael@0 | 16 | def __init__(self, runnable, set_up=None, tear_down=None): |
michael@0 | 17 | # The actual test function/method. |
michael@0 | 18 | self.runnable = runnable |
michael@0 | 19 | # Qualified name of test function/method (e.g. FooModule.testBar). |
michael@0 | 20 | self.qualified_name = self._GetQualifiedName(runnable) |
michael@0 | 21 | # setUp and teardown functions, if any. |
michael@0 | 22 | self.set_up = set_up |
michael@0 | 23 | self.tear_down = tear_down |
michael@0 | 24 | |
michael@0 | 25 | def _GetQualifiedName(self, runnable): |
michael@0 | 26 | """Helper method to infer a runnable's name and module name. |
michael@0 | 27 | |
michael@0 | 28 | Many filters and lists presuppose a format of module_name.testMethodName. |
michael@0 | 29 | To make this easy on everyone, we use some reflection magic to infer this |
michael@0 | 30 | name automatically. |
michael@0 | 31 | |
michael@0 | 32 | Args: |
michael@0 | 33 | runnable: the test method to get the qualified name for |
michael@0 | 34 | |
michael@0 | 35 | Returns: |
michael@0 | 36 | qualified name for this runnable, incl. module name and method name. |
michael@0 | 37 | """ |
michael@0 | 38 | runnable_name = runnable.__name__ |
michael@0 | 39 | # See also tests_annotations. |
michael@0 | 40 | module_name = os.path.splitext( |
michael@0 | 41 | os.path.basename(runnable.__globals__['__file__']))[0] |
michael@0 | 42 | return '.'.join([module_name, runnable_name]) |
michael@0 | 43 | |
michael@0 | 44 | def __str__(self): |
michael@0 | 45 | return self.qualified_name |
michael@0 | 46 | |
michael@0 | 47 | |
michael@0 | 48 | class TestInfoCollection(object): |
michael@0 | 49 | """A collection of TestInfo objects which facilitates filtering.""" |
michael@0 | 50 | |
michael@0 | 51 | def __init__(self): |
michael@0 | 52 | """Initialize a new TestInfoCollection.""" |
michael@0 | 53 | # Master list of all valid tests. |
michael@0 | 54 | self.all_tests = [] |
michael@0 | 55 | |
michael@0 | 56 | def AddTests(self, test_infos): |
michael@0 | 57 | """Adds a set of tests to this collection. |
michael@0 | 58 | |
michael@0 | 59 | The user may then retrieve them, optionally according to criteria, via |
michael@0 | 60 | GetAvailableTests(). |
michael@0 | 61 | |
michael@0 | 62 | Args: |
michael@0 | 63 | test_infos: a list of TestInfos representing test functions/methods. |
michael@0 | 64 | """ |
michael@0 | 65 | self.all_tests = test_infos |
michael@0 | 66 | |
michael@0 | 67 | def GetAvailableTests(self, annotation, name_filter): |
michael@0 | 68 | """Get a collection of TestInfos which match the supplied criteria. |
michael@0 | 69 | |
michael@0 | 70 | Args: |
michael@0 | 71 | annotation: annotation which tests must match, if any |
michael@0 | 72 | name_filter: name filter which tests must match, if any |
michael@0 | 73 | |
michael@0 | 74 | Returns: |
michael@0 | 75 | List of available tests. |
michael@0 | 76 | """ |
michael@0 | 77 | available_tests = self.all_tests |
michael@0 | 78 | |
michael@0 | 79 | # Filter out tests which match neither the requested annotation, nor the |
michael@0 | 80 | # requested name filter, if any. |
michael@0 | 81 | available_tests = [t for t in available_tests if |
michael@0 | 82 | self._AnnotationIncludesTest(t, annotation)] |
michael@0 | 83 | if annotation and len(annotation) == 1 and annotation[0] == 'SmallTest': |
michael@0 | 84 | tests_without_annotation = [ |
michael@0 | 85 | t for t in self.all_tests if |
michael@0 | 86 | not tests_annotations.AnnotatedFunctions.GetTestAnnotations( |
michael@0 | 87 | t.qualified_name)] |
michael@0 | 88 | test_names = [t.qualified_name for t in tests_without_annotation] |
michael@0 | 89 | logging.warning('The following tests do not contain any annotation. ' |
michael@0 | 90 | 'Assuming "SmallTest":\n%s', |
michael@0 | 91 | '\n'.join(test_names)) |
michael@0 | 92 | available_tests += tests_without_annotation |
michael@0 | 93 | available_tests = [t for t in available_tests if |
michael@0 | 94 | self._NameFilterIncludesTest(t, name_filter)] |
michael@0 | 95 | |
michael@0 | 96 | return available_tests |
michael@0 | 97 | |
michael@0 | 98 | def _AnnotationIncludesTest(self, test_info, annotation_filter_list): |
michael@0 | 99 | """Checks whether a given test represented by test_info matches annotation. |
michael@0 | 100 | |
michael@0 | 101 | Args: |
michael@0 | 102 | test_info: TestInfo object representing the test |
michael@0 | 103 | annotation_filter_list: list of annotation filters to match (e.g. Smoke) |
michael@0 | 104 | |
michael@0 | 105 | Returns: |
michael@0 | 106 | True if no annotation was supplied or the test matches; false otherwise. |
michael@0 | 107 | """ |
michael@0 | 108 | if not annotation_filter_list: |
michael@0 | 109 | return True |
michael@0 | 110 | for annotation_filter in annotation_filter_list: |
michael@0 | 111 | filters = annotation_filter.split('=') |
michael@0 | 112 | if len(filters) == 2: |
michael@0 | 113 | key = filters[0] |
michael@0 | 114 | value_list = filters[1].split(',') |
michael@0 | 115 | for value in value_list: |
michael@0 | 116 | if tests_annotations.AnnotatedFunctions.IsAnnotated( |
michael@0 | 117 | key + ':' + value, test_info.qualified_name): |
michael@0 | 118 | return True |
michael@0 | 119 | elif tests_annotations.AnnotatedFunctions.IsAnnotated( |
michael@0 | 120 | annotation_filter, test_info.qualified_name): |
michael@0 | 121 | return True |
michael@0 | 122 | return False |
michael@0 | 123 | |
michael@0 | 124 | def _NameFilterIncludesTest(self, test_info, name_filter): |
michael@0 | 125 | """Checks whether a name filter matches a given test_info's method name. |
michael@0 | 126 | |
michael@0 | 127 | This is a case-sensitive, substring comparison: 'Foo' will match methods |
michael@0 | 128 | Foo.testBar and Bar.testFoo. 'foo' would not match either. |
michael@0 | 129 | |
michael@0 | 130 | Args: |
michael@0 | 131 | test_info: TestInfo object representing the test |
michael@0 | 132 | name_filter: substring to check for in the qualified name of the test |
michael@0 | 133 | |
michael@0 | 134 | Returns: |
michael@0 | 135 | True if no name filter supplied or it matches; False otherwise. |
michael@0 | 136 | """ |
michael@0 | 137 | return not name_filter or name_filter in test_info.qualified_name |