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 19import unittest 20import mock 21 22import atest_error 23import test_finder_handler 24from test_finders import test_info 25from test_finders import test_finder_base 26 27#pylint: disable=protected-access 28REF_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 35class 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 64class 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], 85 ExampleFinderA.unregistered_find_method_from_example_finder, 86 _EXAMPLE_FINDER_A)]).start() 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( 95 test_finder_handler._get_test_reference_types('ModuleOrClassName'), 96 [REF_TYPE.CACHE, REF_TYPE.INTEGRATION, REF_TYPE.MODULE, 97 REF_TYPE.SUITE_PLAN, REF_TYPE.CLASS, REF_TYPE.CC_CLASS] 98 ) 99 self.assertEqual( 100 test_finder_handler._get_test_reference_types('Module_or_Class_name'), 101 [REF_TYPE.CACHE, REF_TYPE.INTEGRATION, REF_TYPE.MODULE, 102 REF_TYPE.SUITE_PLAN, REF_TYPE.CLASS, REF_TYPE.CC_CLASS] 103 ) 104 self.assertEqual( 105 test_finder_handler._get_test_reference_types('SuiteName'), 106 [REF_TYPE.CACHE, REF_TYPE.INTEGRATION, REF_TYPE.MODULE, 107 REF_TYPE.SUITE_PLAN, REF_TYPE.CLASS, REF_TYPE.CC_CLASS] 108 ) 109 self.assertEqual( 110 test_finder_handler._get_test_reference_types('Suite-Name'), 111 [REF_TYPE.CACHE, REF_TYPE.INTEGRATION, REF_TYPE.MODULE, 112 REF_TYPE.SUITE_PLAN, REF_TYPE.CLASS, REF_TYPE.CC_CLASS] 113 ) 114 self.assertEqual( 115 test_finder_handler._get_test_reference_types('some.package'), 116 [REF_TYPE.CACHE, REF_TYPE.MODULE, REF_TYPE.PACKAGE] 117 ) 118 self.assertEqual( 119 test_finder_handler._get_test_reference_types('fully.q.Class'), 120 [REF_TYPE.CACHE, REF_TYPE.QUALIFIED_CLASS] 121 ) 122 self.assertEqual( 123 test_finder_handler._get_test_reference_types('Integration.xml'), 124 [REF_TYPE.CACHE, REF_TYPE.INTEGRATION_FILE_PATH, 125 REF_TYPE.SUITE_PLAN_FILE_PATH] 126 ) 127 self.assertEqual( 128 test_finder_handler._get_test_reference_types('SomeClass.java'), 129 [REF_TYPE.CACHE, REF_TYPE.MODULE_FILE_PATH] 130 ) 131 self.assertEqual( 132 test_finder_handler._get_test_reference_types('SomeClass.kt'), 133 [REF_TYPE.CACHE, REF_TYPE.MODULE_FILE_PATH] 134 ) 135 self.assertEqual( 136 test_finder_handler._get_test_reference_types('Android.mk'), 137 [REF_TYPE.CACHE, REF_TYPE.MODULE_FILE_PATH] 138 ) 139 self.assertEqual( 140 test_finder_handler._get_test_reference_types('Android.bp'), 141 [REF_TYPE.CACHE, REF_TYPE.MODULE_FILE_PATH] 142 ) 143 self.assertEqual( 144 test_finder_handler._get_test_reference_types('SomeTest.cc'), 145 [REF_TYPE.CACHE, REF_TYPE.MODULE_FILE_PATH] 146 ) 147 self.assertEqual( 148 test_finder_handler._get_test_reference_types('SomeTest.cpp'), 149 [REF_TYPE.CACHE, REF_TYPE.MODULE_FILE_PATH] 150 ) 151 self.assertEqual( 152 test_finder_handler._get_test_reference_types('SomeTest.cc#method'), 153 [REF_TYPE.CACHE, REF_TYPE.MODULE_FILE_PATH] 154 ) 155 self.assertEqual( 156 test_finder_handler._get_test_reference_types('module:Class'), 157 [REF_TYPE.CACHE, REF_TYPE.INTEGRATION, REF_TYPE.MODULE_CLASS] 158 ) 159 self.assertEqual( 160 test_finder_handler._get_test_reference_types('module:f.q.Class'), 161 [REF_TYPE.CACHE, REF_TYPE.INTEGRATION, REF_TYPE.MODULE_CLASS] 162 ) 163 self.assertEqual( 164 test_finder_handler._get_test_reference_types('module:a.package'), 165 [REF_TYPE.CACHE, REF_TYPE.MODULE_PACKAGE, REF_TYPE.MODULE_CLASS] 166 ) 167 self.assertEqual( 168 test_finder_handler._get_test_reference_types('.'), 169 [REF_TYPE.CACHE, REF_TYPE.INTEGRATION_FILE_PATH, 170 REF_TYPE.MODULE_FILE_PATH, REF_TYPE.SUITE_PLAN_FILE_PATH] 171 ) 172 self.assertEqual( 173 test_finder_handler._get_test_reference_types('..'), 174 [REF_TYPE.CACHE, REF_TYPE.INTEGRATION_FILE_PATH, 175 REF_TYPE.MODULE_FILE_PATH, REF_TYPE.SUITE_PLAN_FILE_PATH] 176 ) 177 self.assertEqual( 178 test_finder_handler._get_test_reference_types('./rel/path/to/test'), 179 [REF_TYPE.CACHE, REF_TYPE.INTEGRATION_FILE_PATH, 180 REF_TYPE.MODULE_FILE_PATH, REF_TYPE.SUITE_PLAN_FILE_PATH] 181 ) 182 self.assertEqual( 183 test_finder_handler._get_test_reference_types('rel/path/to/test'), 184 [REF_TYPE.CACHE, REF_TYPE.INTEGRATION_FILE_PATH, 185 REF_TYPE.MODULE_FILE_PATH, REF_TYPE.INTEGRATION, 186 REF_TYPE.SUITE_PLAN_FILE_PATH] 187 ) 188 self.assertEqual( 189 test_finder_handler._get_test_reference_types('/abs/path/to/test'), 190 [REF_TYPE.CACHE, REF_TYPE.INTEGRATION_FILE_PATH, 191 REF_TYPE.MODULE_FILE_PATH, REF_TYPE.SUITE_PLAN_FILE_PATH] 192 ) 193 self.assertEqual( 194 test_finder_handler._get_test_reference_types('int/test'), 195 [REF_TYPE.CACHE, REF_TYPE.INTEGRATION_FILE_PATH, 196 REF_TYPE.MODULE_FILE_PATH, REF_TYPE.INTEGRATION, 197 REF_TYPE.SUITE_PLAN_FILE_PATH] 198 ) 199 self.assertEqual( 200 test_finder_handler._get_test_reference_types('int/test:fully.qual.Class#m'), 201 [REF_TYPE.CACHE, REF_TYPE.INTEGRATION_FILE_PATH, 202 REF_TYPE.MODULE_FILE_PATH, REF_TYPE.INTEGRATION, 203 REF_TYPE.SUITE_PLAN_FILE_PATH] 204 ) 205 self.assertEqual( 206 test_finder_handler._get_test_reference_types('int/test:Class#method'), 207 [REF_TYPE.CACHE, REF_TYPE.INTEGRATION_FILE_PATH, 208 REF_TYPE.MODULE_FILE_PATH, REF_TYPE.INTEGRATION, 209 REF_TYPE.SUITE_PLAN_FILE_PATH] 210 ) 211 self.assertEqual( 212 test_finder_handler._get_test_reference_types('int_name_no_slash:Class#m'), 213 [REF_TYPE.CACHE, REF_TYPE.INTEGRATION, REF_TYPE.MODULE_CLASS] 214 ) 215 216 def test_get_registered_find_methods(self): 217 """Test that we get the registered find methods.""" 218 empty_mod_info = None 219 example_finder_a_instance = test_finder_handler._get_finder_instance_dict( 220 empty_mod_info)[_EXAMPLE_FINDER_A] 221 should_equal = [ 222 test_finder_base.Finder( 223 example_finder_a_instance, 224 ExampleFinderA.registered_find_method_from_example_finder, 225 _EXAMPLE_FINDER_A)] 226 should_not_equal = [ 227 test_finder_base.Finder( 228 example_finder_a_instance, 229 ExampleFinderA.unregistered_find_method_from_example_finder, 230 _EXAMPLE_FINDER_A)] 231 # Let's make sure we see the registered method. 232 self.assertEqual( 233 should_equal, 234 test_finder_handler._get_registered_find_methods(empty_mod_info) 235 ) 236 # Make sure we don't see the unregistered method here. 237 self.assertNotEqual( 238 should_not_equal, 239 test_finder_handler._get_registered_find_methods(empty_mod_info) 240 ) 241 242 def test_get_find_methods_for_test(self): 243 """Test that we get the find methods we expect.""" 244 # Let's see that we get the unregistered and registered find methods in 245 # the order we expect. 246 test = '' 247 registered_find_methods = [ 248 test_finder_base.Finder( 249 _FINDER_INSTANCES[_EXAMPLE_FINDER_A], 250 ExampleFinderA.registered_find_method_from_example_finder, 251 _EXAMPLE_FINDER_A)] 252 default_find_methods = [ 253 test_finder_base.Finder( 254 _FINDER_INSTANCES[_EXAMPLE_FINDER_A], 255 ExampleFinderA.unregistered_find_method_from_example_finder, 256 _EXAMPLE_FINDER_A)] 257 should_equal = registered_find_methods + default_find_methods 258 self.assertEqual( 259 should_equal, 260 test_finder_handler.get_find_methods_for_test(self.empty_mod_info, 261 test)) 262 263 264if __name__ == '__main__': 265 unittest.main() 266