Kevin Cheng | 8b2c94c | 2017-12-18 14:43:26 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright 2018, The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | """Unittests for test_finder_handler.""" |
| 18 | |
| 19 | import unittest |
| 20 | import mock |
| 21 | |
| 22 | import atest_error |
| 23 | import test_finder_handler |
| 24 | from test_finders import test_info |
| 25 | from test_finders import test_finder_base |
| 26 | |
| 27 | #pylint: disable=protected-access |
| 28 | REF_TYPE = test_finder_handler._REFERENCE_TYPE |
| 29 | |
| 30 | _EXAMPLE_FINDER_A = 'EXAMPLE_A' |
| 31 | |
| 32 | |
| 33 | #pylint: disable=no-self-use |
| 34 | @test_finder_base.find_method_register |
| 35 | class ExampleFinderA(test_finder_base.TestFinderBase): |
| 36 | """Example finder class A.""" |
| 37 | NAME = _EXAMPLE_FINDER_A |
| 38 | _TEST_RUNNER = 'TEST_RUNNER' |
| 39 | |
| 40 | @test_finder_base.register() |
| 41 | def registered_find_method_from_example_finder(self, test): |
| 42 | """Registered Example find method.""" |
| 43 | if test == 'ExampleFinderATrigger': |
| 44 | return test_info.TestInfo(test_name=test, |
| 45 | test_runner=self._TEST_RUNNER, |
| 46 | build_targets=set()) |
| 47 | return None |
| 48 | |
| 49 | def unregistered_find_method_from_example_finder(self, _test): |
| 50 | """Unregistered Example find method, should never be called.""" |
| 51 | raise atest_error.ShouldNeverBeCalledError() |
| 52 | |
| 53 | |
| 54 | _TEST_FINDERS_PATCH = { |
| 55 | ExampleFinderA, |
| 56 | } |
| 57 | |
| 58 | |
| 59 | _FINDER_INSTANCES = { |
| 60 | _EXAMPLE_FINDER_A: ExampleFinderA(), |
| 61 | } |
| 62 | |
| 63 | |
| 64 | class TestFinderHandlerUnittests(unittest.TestCase): |
| 65 | """Unit tests for test_finder_handler.py""" |
| 66 | |
| 67 | def setUp(self): |
| 68 | """Set up for testing.""" |
| 69 | # pylint: disable=invalid-name |
| 70 | # This is so we can see the full diffs when there are mismatches. |
| 71 | self.maxDiff = None |
| 72 | self.empty_mod_info = None |
| 73 | # We want to control the finders we return. |
| 74 | mock.patch('test_finder_handler._get_test_finders', |
| 75 | lambda: _TEST_FINDERS_PATCH).start() |
| 76 | # Since we're going to be comparing instance objects, we'll need to keep |
| 77 | # track of the objects so they align. |
| 78 | mock.patch('test_finder_handler._get_finder_instance_dict', |
| 79 | lambda x: _FINDER_INSTANCES).start() |
| 80 | # We want to mock out the default find methods to make sure we got all |
| 81 | # the methods we expect. |
| 82 | mock.patch('test_finder_handler._get_default_find_methods', |
| 83 | lambda x, y: [test_finder_base.Finder( |
| 84 | _FINDER_INSTANCES[_EXAMPLE_FINDER_A], |
nelsonli | 34997d5 | 2018-08-17 09:43:28 +0800 | [diff] [blame] | 85 | ExampleFinderA.unregistered_find_method_from_example_finder, |
| 86 | _EXAMPLE_FINDER_A)]).start() |
Kevin Cheng | 8b2c94c | 2017-12-18 14:43:26 -0800 | [diff] [blame] | 87 | |
| 88 | def tearDown(self): |
| 89 | """Tear down.""" |
| 90 | mock.patch.stopall() |
| 91 | |
| 92 | def test_get_test_reference_types(self): |
| 93 | """Test _get_test_reference_types parses reference types correctly.""" |
| 94 | self.assertEqual( |
mikehoran | 6740a41 | 2018-02-20 15:04:16 -0800 | [diff] [blame] | 95 | test_finder_handler._get_test_reference_types('ModuleOrClassName'), |
easoncylee | 5f5cf03 | 2018-06-26 16:17:08 +0800 | [diff] [blame] | 96 | [REF_TYPE.INTEGRATION, REF_TYPE.MODULE, REF_TYPE.SUITE_PLAN, |
easoncylee | 2dd42a9 | 2018-04-26 15:49:56 +0800 | [diff] [blame] | 97 | REF_TYPE.CLASS, REF_TYPE.CC_CLASS] |
Kevin Cheng | 8b2c94c | 2017-12-18 14:43:26 -0800 | [diff] [blame] | 98 | ) |
| 99 | self.assertEqual( |
mikehoran | 6740a41 | 2018-02-20 15:04:16 -0800 | [diff] [blame] | 100 | test_finder_handler._get_test_reference_types('Module_or_Class_name'), |
easoncylee | 5f5cf03 | 2018-06-26 16:17:08 +0800 | [diff] [blame] | 101 | [REF_TYPE.INTEGRATION, REF_TYPE.MODULE, REF_TYPE.SUITE_PLAN, |
easoncylee | 2dd42a9 | 2018-04-26 15:49:56 +0800 | [diff] [blame] | 102 | REF_TYPE.CLASS, REF_TYPE.CC_CLASS] |
| 103 | ) |
| 104 | self.assertEqual( |
| 105 | test_finder_handler._get_test_reference_types('SuiteName'), |
easoncylee | 5f5cf03 | 2018-06-26 16:17:08 +0800 | [diff] [blame] | 106 | [REF_TYPE.INTEGRATION, REF_TYPE.MODULE, REF_TYPE.SUITE_PLAN, |
easoncylee | 2dd42a9 | 2018-04-26 15:49:56 +0800 | [diff] [blame] | 107 | REF_TYPE.CLASS, REF_TYPE.CC_CLASS] |
| 108 | ) |
| 109 | self.assertEqual( |
| 110 | test_finder_handler._get_test_reference_types('Suite-Name'), |
easoncylee | 5f5cf03 | 2018-06-26 16:17:08 +0800 | [diff] [blame] | 111 | [REF_TYPE.INTEGRATION, REF_TYPE.MODULE, REF_TYPE.SUITE_PLAN, |
easoncylee | 2dd42a9 | 2018-04-26 15:49:56 +0800 | [diff] [blame] | 112 | REF_TYPE.CLASS, REF_TYPE.CC_CLASS] |
Kevin Cheng | 8b2c94c | 2017-12-18 14:43:26 -0800 | [diff] [blame] | 113 | ) |
| 114 | self.assertEqual( |
mikehoran | 6740a41 | 2018-02-20 15:04:16 -0800 | [diff] [blame] | 115 | test_finder_handler._get_test_reference_types('some.package'), |
| 116 | [REF_TYPE.PACKAGE] |
Kevin Cheng | 8b2c94c | 2017-12-18 14:43:26 -0800 | [diff] [blame] | 117 | ) |
| 118 | self.assertEqual( |
mikehoran | 6740a41 | 2018-02-20 15:04:16 -0800 | [diff] [blame] | 119 | test_finder_handler._get_test_reference_types('fully.q.Class'), |
| 120 | [REF_TYPE.QUALIFIED_CLASS] |
Kevin Cheng | 8b2c94c | 2017-12-18 14:43:26 -0800 | [diff] [blame] | 121 | ) |
| 122 | self.assertEqual( |
mikehoran | 6740a41 | 2018-02-20 15:04:16 -0800 | [diff] [blame] | 123 | test_finder_handler._get_test_reference_types('Integration.xml'), |
easoncylee | 5f5cf03 | 2018-06-26 16:17:08 +0800 | [diff] [blame] | 124 | [REF_TYPE.INTEGRATION_FILE_PATH, REF_TYPE.SUITE_PLAN_FILE_PATH] |
mikehoran | 6740a41 | 2018-02-20 15:04:16 -0800 | [diff] [blame] | 125 | ) |
| 126 | self.assertEqual( |
| 127 | test_finder_handler._get_test_reference_types('SomeClass.java'), |
| 128 | [REF_TYPE.MODULE_FILE_PATH] |
| 129 | ) |
| 130 | self.assertEqual( |
yelinhsieh | e4580a5 | 2018-08-16 15:51:54 +0800 | [diff] [blame] | 131 | test_finder_handler._get_test_reference_types('SomeClass.kt'), |
| 132 | [REF_TYPE.MODULE_FILE_PATH] |
| 133 | ) |
| 134 | self.assertEqual( |
mikehoran | 6740a41 | 2018-02-20 15:04:16 -0800 | [diff] [blame] | 135 | test_finder_handler._get_test_reference_types('Android.mk'), |
| 136 | [REF_TYPE.MODULE_FILE_PATH] |
| 137 | ) |
| 138 | self.assertEqual( |
| 139 | test_finder_handler._get_test_reference_types('Android.bp'), |
| 140 | [REF_TYPE.MODULE_FILE_PATH] |
| 141 | ) |
| 142 | self.assertEqual( |
kellyhung | f4b5947 | 2018-05-10 17:00:10 +0800 | [diff] [blame] | 143 | test_finder_handler._get_test_reference_types('SomeTest.cc'), |
| 144 | [REF_TYPE.MODULE_FILE_PATH] |
| 145 | ) |
| 146 | self.assertEqual( |
| 147 | test_finder_handler._get_test_reference_types('SomeTest.cpp'), |
| 148 | [REF_TYPE.MODULE_FILE_PATH] |
| 149 | ) |
| 150 | self.assertEqual( |
| 151 | test_finder_handler._get_test_reference_types('SomeTest.cc#method'), |
| 152 | [REF_TYPE.MODULE_FILE_PATH] |
| 153 | ) |
| 154 | self.assertEqual( |
mikehoran | 6740a41 | 2018-02-20 15:04:16 -0800 | [diff] [blame] | 155 | test_finder_handler._get_test_reference_types('module:Class'), |
| 156 | [REF_TYPE.INTEGRATION, REF_TYPE.MODULE_CLASS] |
| 157 | ) |
| 158 | self.assertEqual( |
| 159 | test_finder_handler._get_test_reference_types('module:f.q.Class'), |
| 160 | [REF_TYPE.INTEGRATION, REF_TYPE.MODULE_CLASS] |
| 161 | ) |
| 162 | self.assertEqual( |
| 163 | test_finder_handler._get_test_reference_types('module:a.package'), |
| 164 | [REF_TYPE.MODULE_PACKAGE] |
Kevin Cheng | 8b2c94c | 2017-12-18 14:43:26 -0800 | [diff] [blame] | 165 | ) |
| 166 | self.assertEqual( |
| 167 | test_finder_handler._get_test_reference_types('.'), |
easoncylee | 5f5cf03 | 2018-06-26 16:17:08 +0800 | [diff] [blame] | 168 | [REF_TYPE.INTEGRATION_FILE_PATH, REF_TYPE.MODULE_FILE_PATH, |
| 169 | REF_TYPE.SUITE_PLAN_FILE_PATH] |
Kevin Cheng | 8b2c94c | 2017-12-18 14:43:26 -0800 | [diff] [blame] | 170 | ) |
| 171 | self.assertEqual( |
| 172 | test_finder_handler._get_test_reference_types('..'), |
easoncylee | 5f5cf03 | 2018-06-26 16:17:08 +0800 | [diff] [blame] | 173 | [REF_TYPE.INTEGRATION_FILE_PATH, REF_TYPE.MODULE_FILE_PATH, |
| 174 | REF_TYPE.SUITE_PLAN_FILE_PATH] |
Kevin Cheng | 8b2c94c | 2017-12-18 14:43:26 -0800 | [diff] [blame] | 175 | ) |
| 176 | self.assertEqual( |
| 177 | test_finder_handler._get_test_reference_types('./rel/path/to/test'), |
easoncylee | 5f5cf03 | 2018-06-26 16:17:08 +0800 | [diff] [blame] | 178 | [REF_TYPE.INTEGRATION_FILE_PATH, REF_TYPE.MODULE_FILE_PATH, |
| 179 | REF_TYPE.SUITE_PLAN_FILE_PATH] |
Kevin Cheng | 8b2c94c | 2017-12-18 14:43:26 -0800 | [diff] [blame] | 180 | ) |
| 181 | self.assertEqual( |
| 182 | test_finder_handler._get_test_reference_types('rel/path/to/test'), |
| 183 | [REF_TYPE.INTEGRATION_FILE_PATH, REF_TYPE.MODULE_FILE_PATH, |
easoncylee | 5f5cf03 | 2018-06-26 16:17:08 +0800 | [diff] [blame] | 184 | REF_TYPE.INTEGRATION, REF_TYPE.SUITE_PLAN_FILE_PATH] |
Kevin Cheng | 8b2c94c | 2017-12-18 14:43:26 -0800 | [diff] [blame] | 185 | ) |
| 186 | self.assertEqual( |
| 187 | test_finder_handler._get_test_reference_types('/abs/path/to/test'), |
easoncylee | 5f5cf03 | 2018-06-26 16:17:08 +0800 | [diff] [blame] | 188 | [REF_TYPE.INTEGRATION_FILE_PATH, REF_TYPE.MODULE_FILE_PATH, |
| 189 | REF_TYPE.SUITE_PLAN_FILE_PATH] |
Kevin Cheng | 8b2c94c | 2017-12-18 14:43:26 -0800 | [diff] [blame] | 190 | ) |
| 191 | self.assertEqual( |
| 192 | test_finder_handler._get_test_reference_types('int/test'), |
| 193 | [REF_TYPE.INTEGRATION_FILE_PATH, REF_TYPE.MODULE_FILE_PATH, |
easoncylee | 5f5cf03 | 2018-06-26 16:17:08 +0800 | [diff] [blame] | 194 | REF_TYPE.INTEGRATION, REF_TYPE.SUITE_PLAN_FILE_PATH] |
Kevin Cheng | 8b2c94c | 2017-12-18 14:43:26 -0800 | [diff] [blame] | 195 | ) |
| 196 | self.assertEqual( |
mikehoran | 6740a41 | 2018-02-20 15:04:16 -0800 | [diff] [blame] | 197 | test_finder_handler._get_test_reference_types('int/test:fully.qual.Class#m'), |
Kevin Cheng | 8b2c94c | 2017-12-18 14:43:26 -0800 | [diff] [blame] | 198 | [REF_TYPE.INTEGRATION_FILE_PATH, REF_TYPE.MODULE_FILE_PATH, |
easoncylee | 5f5cf03 | 2018-06-26 16:17:08 +0800 | [diff] [blame] | 199 | REF_TYPE.INTEGRATION, REF_TYPE.SUITE_PLAN_FILE_PATH] |
Kevin Cheng | 8b2c94c | 2017-12-18 14:43:26 -0800 | [diff] [blame] | 200 | ) |
| 201 | self.assertEqual( |
mikehoran | 6740a41 | 2018-02-20 15:04:16 -0800 | [diff] [blame] | 202 | test_finder_handler._get_test_reference_types('int/test:Class#method'), |
Kevin Cheng | 8b2c94c | 2017-12-18 14:43:26 -0800 | [diff] [blame] | 203 | [REF_TYPE.INTEGRATION_FILE_PATH, REF_TYPE.MODULE_FILE_PATH, |
easoncylee | 5f5cf03 | 2018-06-26 16:17:08 +0800 | [diff] [blame] | 204 | REF_TYPE.INTEGRATION, REF_TYPE.SUITE_PLAN_FILE_PATH] |
Kevin Cheng | 8b2c94c | 2017-12-18 14:43:26 -0800 | [diff] [blame] | 205 | ) |
| 206 | self.assertEqual( |
mikehoran | 6740a41 | 2018-02-20 15:04:16 -0800 | [diff] [blame] | 207 | test_finder_handler._get_test_reference_types('int_name_no_slash:Class#m'), |
| 208 | [REF_TYPE.INTEGRATION, REF_TYPE.MODULE_CLASS] |
Kevin Cheng | 8b2c94c | 2017-12-18 14:43:26 -0800 | [diff] [blame] | 209 | ) |
| 210 | |
| 211 | def test_get_registered_find_methods(self): |
| 212 | """Test that we get the registered find methods.""" |
| 213 | empty_mod_info = None |
| 214 | example_finder_a_instance = test_finder_handler._get_finder_instance_dict( |
| 215 | empty_mod_info)[_EXAMPLE_FINDER_A] |
| 216 | should_equal = [ |
| 217 | test_finder_base.Finder( |
| 218 | example_finder_a_instance, |
nelsonli | 34997d5 | 2018-08-17 09:43:28 +0800 | [diff] [blame] | 219 | ExampleFinderA.registered_find_method_from_example_finder, |
| 220 | _EXAMPLE_FINDER_A)] |
Kevin Cheng | 8b2c94c | 2017-12-18 14:43:26 -0800 | [diff] [blame] | 221 | should_not_equal = [ |
| 222 | test_finder_base.Finder( |
| 223 | example_finder_a_instance, |
nelsonli | 34997d5 | 2018-08-17 09:43:28 +0800 | [diff] [blame] | 224 | ExampleFinderA.unregistered_find_method_from_example_finder, |
| 225 | _EXAMPLE_FINDER_A)] |
Kevin Cheng | 8b2c94c | 2017-12-18 14:43:26 -0800 | [diff] [blame] | 226 | # Let's make sure we see the registered method. |
| 227 | self.assertEqual( |
| 228 | should_equal, |
| 229 | test_finder_handler._get_registered_find_methods(empty_mod_info) |
| 230 | ) |
| 231 | # Make sure we don't see the unregistered method here. |
| 232 | self.assertNotEqual( |
| 233 | should_not_equal, |
| 234 | test_finder_handler._get_registered_find_methods(empty_mod_info) |
| 235 | ) |
| 236 | |
| 237 | def test_get_find_methods_for_test(self): |
| 238 | """Test that we get the find methods we expect.""" |
| 239 | # Let's see that we get the unregistered and registered find methods in |
| 240 | # the order we expect. |
| 241 | test = '' |
| 242 | registered_find_methods = [ |
| 243 | test_finder_base.Finder( |
| 244 | _FINDER_INSTANCES[_EXAMPLE_FINDER_A], |
nelsonli | 34997d5 | 2018-08-17 09:43:28 +0800 | [diff] [blame] | 245 | ExampleFinderA.registered_find_method_from_example_finder, |
| 246 | _EXAMPLE_FINDER_A)] |
Kevin Cheng | 8b2c94c | 2017-12-18 14:43:26 -0800 | [diff] [blame] | 247 | default_find_methods = [ |
| 248 | test_finder_base.Finder( |
| 249 | _FINDER_INSTANCES[_EXAMPLE_FINDER_A], |
nelsonli | 34997d5 | 2018-08-17 09:43:28 +0800 | [diff] [blame] | 250 | ExampleFinderA.unregistered_find_method_from_example_finder, |
| 251 | _EXAMPLE_FINDER_A)] |
Kevin Cheng | 8b2c94c | 2017-12-18 14:43:26 -0800 | [diff] [blame] | 252 | should_equal = registered_find_methods + default_find_methods |
| 253 | self.assertEqual( |
| 254 | should_equal, |
| 255 | test_finder_handler.get_find_methods_for_test(self.empty_mod_info, |
| 256 | test)) |
| 257 | |
| 258 | |
| 259 | if __name__ == '__main__': |
| 260 | unittest.main() |