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 module_finder.""" 18 19import re 20import unittest 21import os 22import mock 23 24# pylint: disable=import-error 25import atest_error 26import constants 27import module_info 28import unittest_constants as uc 29import unittest_utils 30from test_finders import module_finder 31from test_finders import test_finder_utils 32from test_finders import test_info 33from test_runners import atest_tf_test_runner as atf_tr 34 35MODULE_CLASS = '%s:%s' % (uc.MODULE_NAME, uc.CLASS_NAME) 36MODULE_PACKAGE = '%s:%s' % (uc.MODULE_NAME, uc.PACKAGE) 37CC_MODULE_CLASS = '%s:%s' % (uc.CC_MODULE_NAME, uc.CC_CLASS_NAME) 38KERNEL_TEST_CLASS = 'test_class_1' 39KERNEL_TEST_CONFIG = 'KernelTest.xml' 40KERNEL_MODULE_CLASS = '%s:%s' % (constants.REQUIRED_KERNEL_TEST_MODULES[0], 41 KERNEL_TEST_CLASS) 42KERNEL_CONFIG_FILE = os.path.join(uc.TEST_DATA_DIR, KERNEL_TEST_CONFIG) 43KERNEL_CLASS_FILTER = test_info.TestFilter(KERNEL_TEST_CLASS, frozenset()) 44KERNEL_MODULE_CLASS_DATA = {constants.TI_REL_CONFIG: KERNEL_CONFIG_FILE, 45 constants.TI_FILTER: frozenset([KERNEL_CLASS_FILTER])} 46KERNEL_MODULE_CLASS_INFO = test_info.TestInfo( 47 constants.REQUIRED_KERNEL_TEST_MODULES[0], 48 atf_tr.AtestTradefedTestRunner.NAME, 49 uc.CLASS_BUILD_TARGETS, KERNEL_MODULE_CLASS_DATA) 50FLAT_METHOD_INFO = test_info.TestInfo( 51 uc.MODULE_NAME, 52 atf_tr.AtestTradefedTestRunner.NAME, 53 uc.MODULE_BUILD_TARGETS, 54 data={constants.TI_FILTER: frozenset([uc.FLAT_METHOD_FILTER]), 55 constants.TI_REL_CONFIG: uc.CONFIG_FILE}) 56MODULE_CLASS_METHOD = '%s#%s' % (MODULE_CLASS, uc.METHOD_NAME) 57CC_MODULE_CLASS_METHOD = '%s#%s' % (CC_MODULE_CLASS, uc.CC_METHOD_NAME) 58CLASS_INFO_MODULE_2 = test_info.TestInfo( 59 uc.MODULE2_NAME, 60 atf_tr.AtestTradefedTestRunner.NAME, 61 uc.CLASS_BUILD_TARGETS, 62 data={constants.TI_FILTER: frozenset([uc.CLASS_FILTER]), 63 constants.TI_REL_CONFIG: uc.CONFIG2_FILE}) 64CC_CLASS_INFO_MODULE_2 = test_info.TestInfo( 65 uc.CC_MODULE2_NAME, 66 atf_tr.AtestTradefedTestRunner.NAME, 67 uc.CLASS_BUILD_TARGETS, 68 data={constants.TI_FILTER: frozenset([uc.CC_CLASS_FILTER]), 69 constants.TI_REL_CONFIG: uc.CC_CONFIG2_FILE}) 70DEFAULT_INSTALL_PATH = ['/path/to/install'] 71ROBO_MOD_PATH = ['/shared/robo/path'] 72NON_RUN_ROBO_MOD_NAME = 'robo_mod' 73RUN_ROBO_MOD_NAME = 'run_robo_mod' 74NON_RUN_ROBO_MOD = {constants.MODULE_NAME: NON_RUN_ROBO_MOD_NAME, 75 constants.MODULE_PATH: ROBO_MOD_PATH, 76 constants.MODULE_CLASS: ['random_class']} 77RUN_ROBO_MOD = {constants.MODULE_NAME: RUN_ROBO_MOD_NAME, 78 constants.MODULE_PATH: ROBO_MOD_PATH, 79 constants.MODULE_CLASS: [constants.MODULE_CLASS_ROBOLECTRIC]} 80 81SEARCH_DIR_RE = re.compile(r'^find ([^ ]*).*$') 82 83#pylint: disable=unused-argument 84def classoutside_side_effect(find_cmd, shell=False): 85 """Mock the check output of a find cmd where class outside module path.""" 86 search_dir = SEARCH_DIR_RE.match(find_cmd).group(1).strip() 87 if search_dir == uc.ROOT: 88 return uc.FIND_ONE 89 return None 90 91 92#pylint: disable=protected-access 93class ModuleFinderUnittests(unittest.TestCase): 94 """Unit tests for module_finder.py""" 95 96 def setUp(self): 97 """Set up stuff for testing.""" 98 self.mod_finder = module_finder.ModuleFinder() 99 self.mod_finder.module_info = mock.Mock(spec=module_info.ModuleInfo) 100 self.mod_finder.module_info.path_to_module_info = {} 101 self.mod_finder.root_dir = uc.ROOT 102 103 def test_is_vts_module(self): 104 """Test _load_module_info_file regular operation.""" 105 mod_name = 'mod' 106 is_vts_module_info = {'compatibility_suites': ['vts10', 'tests']} 107 self.mod_finder.module_info.get_module_info.return_value = is_vts_module_info 108 self.assertTrue(self.mod_finder._is_vts_module(mod_name)) 109 110 is_not_vts_module = {'compatibility_suites': ['vts10', 'cts']} 111 self.mod_finder.module_info.get_module_info.return_value = is_not_vts_module 112 self.assertFalse(self.mod_finder._is_vts_module(mod_name)) 113 114 # pylint: disable=unused-argument 115 @mock.patch.object(module_finder.ModuleFinder, '_get_build_targets', 116 return_value=uc.MODULE_BUILD_TARGETS) 117 def test_find_test_by_module_name(self, _get_targ): 118 """Test find_test_by_module_name.""" 119 self.mod_finder.module_info.is_robolectric_test.return_value = False 120 self.mod_finder.module_info.has_test_config.return_value = True 121 mod_info = {'installed': ['/path/to/install'], 122 'path': [uc.MODULE_DIR], 123 constants.MODULE_CLASS: [], 124 constants.MODULE_COMPATIBILITY_SUITES: []} 125 self.mod_finder.module_info.get_module_info.return_value = mod_info 126 t_infos = self.mod_finder.find_test_by_module_name(uc.MODULE_NAME) 127 unittest_utils.assert_equal_testinfos( 128 self, 129 t_infos[0], 130 uc.MODULE_INFO) 131 self.mod_finder.module_info.get_module_info.return_value = None 132 self.mod_finder.module_info.is_testable_module.return_value = False 133 self.assertIsNone(self.mod_finder.find_test_by_module_name('Not_Module')) 134 135 @mock.patch.object(test_finder_utils, 'has_method_in_file', 136 return_value=True) 137 @mock.patch.object(module_finder.ModuleFinder, '_is_vts_module', 138 return_value=False) 139 @mock.patch.object(module_finder.ModuleFinder, '_get_build_targets') 140 @mock.patch('subprocess.check_output', return_value=uc.FIND_ONE) 141 @mock.patch.object(test_finder_utils, 'get_fully_qualified_class_name', 142 return_value=uc.FULL_CLASS_NAME) 143 @mock.patch('os.path.isfile', side_effect=unittest_utils.isfile_side_effect) 144 @mock.patch('os.path.isdir', return_value=True) 145 #pylint: disable=unused-argument 146 def test_find_test_by_class_name(self, _isdir, _isfile, _fqcn, 147 mock_checkoutput, mock_build, 148 _vts, _has_method_in_file): 149 """Test find_test_by_class_name.""" 150 mock_build.return_value = uc.CLASS_BUILD_TARGETS 151 self.mod_finder.module_info.is_auto_gen_test_config.return_value = False 152 self.mod_finder.module_info.is_robolectric_test.return_value = False 153 self.mod_finder.module_info.has_test_config.return_value = True 154 self.mod_finder.module_info.get_module_names.return_value = [uc.MODULE_NAME] 155 self.mod_finder.module_info.get_module_info.return_value = { 156 constants.MODULE_INSTALLED: DEFAULT_INSTALL_PATH, 157 constants.MODULE_NAME: uc.MODULE_NAME, 158 constants.MODULE_CLASS: [], 159 constants.MODULE_COMPATIBILITY_SUITES: []} 160 t_infos = self.mod_finder.find_test_by_class_name(uc.CLASS_NAME) 161 unittest_utils.assert_equal_testinfos( 162 self, t_infos[0], uc.CLASS_INFO) 163 164 # with method 165 mock_build.return_value = uc.MODULE_BUILD_TARGETS 166 class_with_method = '%s#%s' % (uc.CLASS_NAME, uc.METHOD_NAME) 167 t_infos = self.mod_finder.find_test_by_class_name(class_with_method) 168 unittest_utils.assert_equal_testinfos( 169 self, t_infos[0], uc.METHOD_INFO) 170 mock_build.return_value = uc.MODULE_BUILD_TARGETS 171 class_methods = '%s,%s' % (class_with_method, uc.METHOD2_NAME) 172 t_infos = self.mod_finder.find_test_by_class_name(class_methods) 173 unittest_utils.assert_equal_testinfos( 174 self, t_infos[0], 175 FLAT_METHOD_INFO) 176 # module and rel_config passed in 177 mock_build.return_value = uc.CLASS_BUILD_TARGETS 178 t_infos = self.mod_finder.find_test_by_class_name( 179 uc.CLASS_NAME, uc.MODULE_NAME, uc.CONFIG_FILE) 180 unittest_utils.assert_equal_testinfos( 181 self, t_infos[0], uc.CLASS_INFO) 182 # find output fails to find class file 183 mock_checkoutput.return_value = '' 184 self.assertIsNone(self.mod_finder.find_test_by_class_name('Not class')) 185 # class is outside given module path 186 mock_checkoutput.side_effect = classoutside_side_effect 187 t_infos = self.mod_finder.find_test_by_class_name(uc.CLASS_NAME, 188 uc.MODULE2_NAME, 189 uc.CONFIG2_FILE) 190 unittest_utils.assert_equal_testinfos( 191 self, t_infos[0], 192 CLASS_INFO_MODULE_2) 193 194 @mock.patch.object(test_finder_utils, 'has_method_in_file', 195 return_value=True) 196 @mock.patch.object(module_finder.ModuleFinder, '_is_vts_module', 197 return_value=False) 198 @mock.patch.object(module_finder.ModuleFinder, '_get_build_targets') 199 @mock.patch('subprocess.check_output', return_value=uc.FIND_ONE) 200 @mock.patch.object(test_finder_utils, 'get_fully_qualified_class_name', 201 return_value=uc.FULL_CLASS_NAME) 202 @mock.patch('os.path.isfile', side_effect=unittest_utils.isfile_side_effect) 203 #pylint: disable=unused-argument 204 def test_find_test_by_module_and_class(self, _isfile, _fqcn, 205 mock_checkoutput, mock_build, 206 _vts, _has_method_in_file): 207 """Test find_test_by_module_and_class.""" 208 # Native test was tested in test_find_test_by_cc_class_name(). 209 self.mod_finder.module_info.is_native_test.return_value = False 210 self.mod_finder.module_info.is_auto_gen_test_config.return_value = False 211 self.mod_finder.module_info.is_robolectric_test.return_value = False 212 self.mod_finder.module_info.has_test_config.return_value = True 213 mock_build.return_value = uc.CLASS_BUILD_TARGETS 214 mod_info = {constants.MODULE_INSTALLED: DEFAULT_INSTALL_PATH, 215 constants.MODULE_PATH: [uc.MODULE_DIR], 216 constants.MODULE_CLASS: [], 217 constants.MODULE_COMPATIBILITY_SUITES: []} 218 self.mod_finder.module_info.get_module_info.return_value = mod_info 219 t_infos = self.mod_finder.find_test_by_module_and_class(MODULE_CLASS) 220 unittest_utils.assert_equal_testinfos(self, t_infos[0], uc.CLASS_INFO) 221 # with method 222 mock_build.return_value = uc.MODULE_BUILD_TARGETS 223 t_infos = self.mod_finder.find_test_by_module_and_class(MODULE_CLASS_METHOD) 224 unittest_utils.assert_equal_testinfos(self, t_infos[0], uc.METHOD_INFO) 225 self.mod_finder.module_info.is_testable_module.return_value = False 226 # bad module, good class, returns None 227 bad_module = '%s:%s' % ('BadMod', uc.CLASS_NAME) 228 self.mod_finder.module_info.get_module_info.return_value = None 229 self.assertIsNone(self.mod_finder.find_test_by_module_and_class(bad_module)) 230 # find output fails to find class file 231 mock_checkoutput.return_value = '' 232 bad_class = '%s:%s' % (uc.MODULE_NAME, 'Anything') 233 self.mod_finder.module_info.get_module_info.return_value = mod_info 234 self.assertIsNone(self.mod_finder.find_test_by_module_and_class(bad_class)) 235 236 @mock.patch.object(module_finder.ModuleFinder, 'find_test_by_kernel_class_name', 237 return_value=None) 238 @mock.patch.object(module_finder.ModuleFinder, '_is_vts_module', 239 return_value=False) 240 @mock.patch.object(module_finder.ModuleFinder, '_get_build_targets') 241 @mock.patch('subprocess.check_output', return_value=uc.FIND_CC_ONE) 242 @mock.patch.object(test_finder_utils, 'find_class_file', 243 side_effect=[None, None, '/']) 244 @mock.patch('os.path.isfile', side_effect=unittest_utils.isfile_side_effect) 245 #pylint: disable=unused-argument 246 def test_find_test_by_module_and_class_part_2(self, _isfile, mock_fcf, 247 mock_checkoutput, mock_build, 248 _vts, _find_kernel): 249 """Test find_test_by_module_and_class for MODULE:CC_CLASS.""" 250 # Native test was tested in test_find_test_by_cc_class_name() 251 self.mod_finder.module_info.is_native_test.return_value = False 252 self.mod_finder.module_info.is_auto_gen_test_config.return_value = False 253 self.mod_finder.module_info.is_robolectric_test.return_value = False 254 self.mod_finder.module_info.has_test_config.return_value = True 255 mock_build.return_value = uc.CLASS_BUILD_TARGETS 256 mod_info = {constants.MODULE_INSTALLED: DEFAULT_INSTALL_PATH, 257 constants.MODULE_PATH: [uc.CC_MODULE_DIR], 258 constants.MODULE_CLASS: [], 259 constants.MODULE_COMPATIBILITY_SUITES: []} 260 self.mod_finder.module_info.get_module_info.return_value = mod_info 261 t_infos = self.mod_finder.find_test_by_module_and_class(CC_MODULE_CLASS) 262 unittest_utils.assert_equal_testinfos(self, t_infos[0], uc.CC_MODULE_CLASS_INFO) 263 # with method 264 mock_build.return_value = uc.MODULE_BUILD_TARGETS 265 mock_fcf.side_effect = [None, None, '/'] 266 t_infos = self.mod_finder.find_test_by_module_and_class(CC_MODULE_CLASS_METHOD) 267 unittest_utils.assert_equal_testinfos(self, t_infos[0], uc.CC_METHOD_INFO) 268 # bad module, good class, returns None 269 bad_module = '%s:%s' % ('BadMod', uc.CC_CLASS_NAME) 270 self.mod_finder.module_info.get_module_info.return_value = None 271 self.mod_finder.module_info.is_testable_module.return_value = False 272 self.assertIsNone(self.mod_finder.find_test_by_module_and_class(bad_module)) 273 274 @mock.patch.object(module_finder.ModuleFinder, '_get_module_test_config', 275 return_value=KERNEL_CONFIG_FILE) 276 @mock.patch.object(module_finder.ModuleFinder, '_is_vts_module', 277 return_value=False) 278 @mock.patch.object(module_finder.ModuleFinder, '_get_build_targets') 279 @mock.patch('subprocess.check_output', return_value=uc.FIND_CC_ONE) 280 @mock.patch.object(test_finder_utils, 'find_class_file', 281 side_effect=[None, None, '/']) 282 @mock.patch('os.path.isfile', side_effect=unittest_utils.isfile_side_effect) 283 #pylint: disable=unused-argument 284 def test_find_test_by_module_and_class_for_kernel_test( 285 self, _isfile, mock_fcf, mock_checkoutput, mock_build, _vts, 286 _test_config): 287 """Test find_test_by_module_and_class for MODULE:CC_CLASS.""" 288 # Kernel test was tested in find_test_by_kernel_class_name() 289 self.mod_finder.module_info.is_native_test.return_value = False 290 self.mod_finder.module_info.is_auto_gen_test_config.return_value = False 291 self.mod_finder.module_info.is_robolectric_test.return_value = False 292 self.mod_finder.module_info.has_test_config.return_value = True 293 mock_build.return_value = uc.CLASS_BUILD_TARGETS 294 mod_info = {constants.MODULE_INSTALLED: DEFAULT_INSTALL_PATH, 295 constants.MODULE_PATH: [uc.CC_MODULE_DIR], 296 constants.MODULE_CLASS: [], 297 constants.MODULE_COMPATIBILITY_SUITES: []} 298 self.mod_finder.module_info.get_module_info.return_value = mod_info 299 t_infos = self.mod_finder.find_test_by_module_and_class(KERNEL_MODULE_CLASS) 300 unittest_utils.assert_equal_testinfos(self, t_infos[0], KERNEL_MODULE_CLASS_INFO) 301 302 @mock.patch.object(module_finder.ModuleFinder, '_is_vts_module', 303 return_value=False) 304 @mock.patch.object(module_finder.ModuleFinder, '_get_build_targets') 305 @mock.patch('subprocess.check_output', return_value=uc.FIND_PKG) 306 @mock.patch('os.path.isfile', side_effect=unittest_utils.isfile_side_effect) 307 @mock.patch('os.path.isdir', return_value=True) 308 #pylint: disable=unused-argument 309 def test_find_test_by_package_name(self, _isdir, _isfile, mock_checkoutput, 310 mock_build, _vts): 311 """Test find_test_by_package_name.""" 312 self.mod_finder.module_info.is_auto_gen_test_config.return_value = False 313 self.mod_finder.module_info.is_robolectric_test.return_value = False 314 self.mod_finder.module_info.has_test_config.return_value = True 315 mock_build.return_value = uc.CLASS_BUILD_TARGETS 316 self.mod_finder.module_info.get_module_names.return_value = [uc.MODULE_NAME] 317 self.mod_finder.module_info.get_module_info.return_value = { 318 constants.MODULE_INSTALLED: DEFAULT_INSTALL_PATH, 319 constants.MODULE_NAME: uc.MODULE_NAME, 320 constants.MODULE_CLASS: [], 321 constants.MODULE_COMPATIBILITY_SUITES: [] 322 } 323 t_infos = self.mod_finder.find_test_by_package_name(uc.PACKAGE) 324 unittest_utils.assert_equal_testinfos( 325 self, t_infos[0], 326 uc.PACKAGE_INFO) 327 # with method, should raise 328 pkg_with_method = '%s#%s' % (uc.PACKAGE, uc.METHOD_NAME) 329 self.assertRaises(atest_error.MethodWithoutClassError, 330 self.mod_finder.find_test_by_package_name, 331 pkg_with_method) 332 # module and rel_config passed in 333 t_infos = self.mod_finder.find_test_by_package_name( 334 uc.PACKAGE, uc.MODULE_NAME, uc.CONFIG_FILE) 335 unittest_utils.assert_equal_testinfos( 336 self, t_infos[0], uc.PACKAGE_INFO) 337 # find output fails to find class file 338 mock_checkoutput.return_value = '' 339 self.assertIsNone(self.mod_finder.find_test_by_package_name('Not pkg')) 340 341 @mock.patch('os.path.isdir', return_value=False) 342 @mock.patch.object(module_finder.ModuleFinder, '_is_vts_module', 343 return_value=False) 344 @mock.patch.object(module_finder.ModuleFinder, '_get_build_targets') 345 @mock.patch('subprocess.check_output', return_value=uc.FIND_PKG) 346 @mock.patch('os.path.isfile', side_effect=unittest_utils.isfile_side_effect) 347 #pylint: disable=unused-argument 348 def test_find_test_by_module_and_package(self, _isfile, mock_checkoutput, 349 mock_build, _vts, _isdir): 350 """Test find_test_by_module_and_package.""" 351 self.mod_finder.module_info.is_auto_gen_test_config.return_value = False 352 self.mod_finder.module_info.is_robolectric_test.return_value = False 353 self.mod_finder.module_info.has_test_config.return_value = True 354 mock_build.return_value = uc.CLASS_BUILD_TARGETS 355 mod_info = {constants.MODULE_INSTALLED: DEFAULT_INSTALL_PATH, 356 constants.MODULE_PATH: [uc.MODULE_DIR], 357 constants.MODULE_CLASS: [], 358 constants.MODULE_COMPATIBILITY_SUITES: []} 359 self.mod_finder.module_info.get_module_info.return_value = mod_info 360 t_infos = self.mod_finder.find_test_by_module_and_package(MODULE_PACKAGE) 361 self.assertEqual(t_infos, None) 362 _isdir.return_value = True 363 t_infos = self.mod_finder.find_test_by_module_and_package(MODULE_PACKAGE) 364 unittest_utils.assert_equal_testinfos(self, t_infos[0], uc.PACKAGE_INFO) 365 366 # with method, raises 367 module_pkg_with_method = '%s:%s#%s' % (uc.MODULE2_NAME, uc.PACKAGE, 368 uc.METHOD_NAME) 369 self.assertRaises(atest_error.MethodWithoutClassError, 370 self.mod_finder.find_test_by_module_and_package, 371 module_pkg_with_method) 372 # bad module, good pkg, returns None 373 self.mod_finder.module_info.is_testable_module.return_value = False 374 bad_module = '%s:%s' % ('BadMod', uc.PACKAGE) 375 self.mod_finder.module_info.get_module_info.return_value = None 376 self.assertIsNone(self.mod_finder.find_test_by_module_and_package(bad_module)) 377 # find output fails to find package path 378 mock_checkoutput.return_value = '' 379 bad_pkg = '%s:%s' % (uc.MODULE_NAME, 'Anything') 380 self.mod_finder.module_info.get_module_info.return_value = mod_info 381 self.assertIsNone(self.mod_finder.find_test_by_module_and_package(bad_pkg)) 382 383 @mock.patch.object(test_finder_utils, 'has_method_in_file', 384 return_value=True) 385 @mock.patch.object(test_finder_utils, 'has_cc_class', 386 return_value=True) 387 @mock.patch.object(module_finder.ModuleFinder, '_get_build_targets') 388 @mock.patch.object(module_finder.ModuleFinder, '_is_vts_module', 389 return_value=False) 390 @mock.patch.object(test_finder_utils, 'get_fully_qualified_class_name', 391 return_value=uc.FULL_CLASS_NAME) 392 @mock.patch('os.path.realpath', 393 side_effect=unittest_utils.realpath_side_effect) 394 @mock.patch('os.path.isfile', side_effect=unittest_utils.isfile_side_effect) 395 @mock.patch.object(test_finder_utils, 'find_parent_module_dir') 396 @mock.patch('os.path.exists') 397 #pylint: disable=unused-argument 398 def test_find_test_by_path(self, mock_pathexists, mock_dir, _isfile, _real, 399 _fqcn, _vts, mock_build, _has_cc_class, 400 _has_method_in_file): 401 """Test find_test_by_path.""" 402 self.mod_finder.module_info.is_robolectric_test.return_value = False 403 self.mod_finder.module_info.has_test_config.return_value = True 404 mock_build.return_value = set() 405 # Check that we don't return anything with invalid test references. 406 mock_pathexists.return_value = False 407 unittest_utils.assert_equal_testinfos( 408 self, None, self.mod_finder.find_test_by_path('bad/path')) 409 mock_pathexists.return_value = True 410 mock_dir.return_value = None 411 unittest_utils.assert_equal_testinfos( 412 self, None, self.mod_finder.find_test_by_path('no/module')) 413 self.mod_finder.module_info.get_module_names.return_value = [uc.MODULE_NAME] 414 self.mod_finder.module_info.get_module_info.return_value = { 415 constants.MODULE_INSTALLED: DEFAULT_INSTALL_PATH, 416 constants.MODULE_NAME: uc.MODULE_NAME, 417 constants.MODULE_CLASS: [], 418 constants.MODULE_COMPATIBILITY_SUITES: []} 419 420 # Happy path testing. 421 mock_dir.return_value = uc.MODULE_DIR 422 423 class_path = '%s.kt' % uc.CLASS_NAME 424 mock_build.return_value = uc.CLASS_BUILD_TARGETS 425 t_infos = self.mod_finder.find_test_by_path(class_path) 426 unittest_utils.assert_equal_testinfos( 427 self, uc.CLASS_INFO, t_infos[0]) 428 429 class_path = '%s.java' % uc.CLASS_NAME 430 mock_build.return_value = uc.CLASS_BUILD_TARGETS 431 t_infos = self.mod_finder.find_test_by_path(class_path) 432 unittest_utils.assert_equal_testinfos( 433 self, uc.CLASS_INFO, t_infos[0]) 434 435 class_with_method = '%s#%s' % (class_path, uc.METHOD_NAME) 436 mock_build.return_value = uc.MODULE_BUILD_TARGETS 437 t_infos = self.mod_finder.find_test_by_path(class_with_method) 438 unittest_utils.assert_equal_testinfos( 439 self, t_infos[0], uc.METHOD_INFO) 440 441 class_with_methods = '%s,%s' % (class_with_method, uc.METHOD2_NAME) 442 mock_build.return_value = uc.MODULE_BUILD_TARGETS 443 t_infos = self.mod_finder.find_test_by_path(class_with_methods) 444 unittest_utils.assert_equal_testinfos( 445 self, t_infos[0], 446 FLAT_METHOD_INFO) 447 448 # Cc path testing. 449 self.mod_finder.module_info.get_module_names.return_value = [uc.CC_MODULE_NAME] 450 self.mod_finder.module_info.get_module_info.return_value = { 451 constants.MODULE_INSTALLED: DEFAULT_INSTALL_PATH, 452 constants.MODULE_NAME: uc.CC_MODULE_NAME, 453 constants.MODULE_CLASS: [], 454 constants.MODULE_COMPATIBILITY_SUITES: []} 455 mock_dir.return_value = uc.CC_MODULE_DIR 456 class_path = '%s' % uc.CC_PATH 457 mock_build.return_value = uc.CLASS_BUILD_TARGETS 458 t_infos = self.mod_finder.find_test_by_path(class_path) 459 unittest_utils.assert_equal_testinfos( 460 self, uc.CC_PATH_INFO2, t_infos[0]) 461 462 @mock.patch.object(module_finder.ModuleFinder, '_get_build_targets', 463 return_value=uc.MODULE_BUILD_TARGETS) 464 @mock.patch.object(module_finder.ModuleFinder, '_is_vts_module', 465 return_value=False) 466 @mock.patch.object(test_finder_utils, 'find_parent_module_dir', 467 return_value=os.path.relpath(uc.TEST_DATA_DIR, uc.ROOT)) 468 #pylint: disable=unused-argument 469 def test_find_test_by_path_part_2(self, _find_parent, _is_vts, _get_build): 470 """Test find_test_by_path for directories.""" 471 self.mod_finder.module_info.is_auto_gen_test_config.return_value = False 472 self.mod_finder.module_info.is_robolectric_test.return_value = False 473 self.mod_finder.module_info.has_test_config.return_value = True 474 # Dir with java files in it, should run as package 475 class_dir = os.path.join(uc.TEST_DATA_DIR, 'path_testing') 476 self.mod_finder.module_info.get_module_names.return_value = [uc.MODULE_NAME] 477 self.mod_finder.module_info.get_module_info.return_value = { 478 constants.MODULE_INSTALLED: DEFAULT_INSTALL_PATH, 479 constants.MODULE_NAME: uc.MODULE_NAME, 480 constants.MODULE_CLASS: [], 481 constants.MODULE_COMPATIBILITY_SUITES: []} 482 t_infos = self.mod_finder.find_test_by_path(class_dir) 483 unittest_utils.assert_equal_testinfos( 484 self, uc.PATH_INFO, t_infos[0]) 485 # Dir with no java files in it, should run whole module 486 empty_dir = os.path.join(uc.TEST_DATA_DIR, 'path_testing_empty') 487 t_infos = self.mod_finder.find_test_by_path(empty_dir) 488 unittest_utils.assert_equal_testinfos( 489 self, uc.EMPTY_PATH_INFO, 490 t_infos[0]) 491 # Dir with cc files in it, should run as cc class 492 class_dir = os.path.join(uc.TEST_DATA_DIR, 'cc_path_testing') 493 self.mod_finder.module_info.get_module_names.return_value = [uc.CC_MODULE_NAME] 494 self.mod_finder.module_info.get_module_info.return_value = { 495 constants.MODULE_INSTALLED: DEFAULT_INSTALL_PATH, 496 constants.MODULE_NAME: uc.CC_MODULE_NAME, 497 constants.MODULE_CLASS: [], 498 constants.MODULE_COMPATIBILITY_SUITES: []} 499 t_infos = self.mod_finder.find_test_by_path(class_dir) 500 unittest_utils.assert_equal_testinfos( 501 self, uc.CC_PATH_INFO, t_infos[0]) 502 503 @mock.patch.object(test_finder_utils, 'has_method_in_file', 504 return_value=True) 505 @mock.patch.object(module_finder.ModuleFinder, '_is_vts_module', 506 return_value=False) 507 @mock.patch.object(module_finder.ModuleFinder, '_get_build_targets') 508 @mock.patch('subprocess.check_output', return_value=uc.CC_FIND_ONE) 509 @mock.patch('os.path.isfile', side_effect=unittest_utils.isfile_side_effect) 510 @mock.patch('os.path.isdir', return_value=True) 511 #pylint: disable=unused-argument 512 def test_find_test_by_cc_class_name(self, _isdir, _isfile, 513 mock_checkoutput, mock_build, 514 _vts, _has_method): 515 """Test find_test_by_cc_class_name.""" 516 mock_build.return_value = uc.CLASS_BUILD_TARGETS 517 self.mod_finder.module_info.is_auto_gen_test_config.return_value = False 518 self.mod_finder.module_info.is_robolectric_test.return_value = False 519 self.mod_finder.module_info.has_test_config.return_value = True 520 self.mod_finder.module_info.get_module_names.return_value = [uc.CC_MODULE_NAME] 521 self.mod_finder.module_info.get_module_info.return_value = { 522 constants.MODULE_INSTALLED: DEFAULT_INSTALL_PATH, 523 constants.MODULE_NAME: uc.CC_MODULE_NAME, 524 constants.MODULE_CLASS: [], 525 constants.MODULE_COMPATIBILITY_SUITES: []} 526 t_infos = self.mod_finder.find_test_by_cc_class_name(uc.CC_CLASS_NAME) 527 unittest_utils.assert_equal_testinfos( 528 self, t_infos[0], uc.CC_CLASS_INFO) 529 530 # with method 531 mock_build.return_value = uc.MODULE_BUILD_TARGETS 532 class_with_method = '%s#%s' % (uc.CC_CLASS_NAME, uc.CC_METHOD_NAME) 533 t_infos = self.mod_finder.find_test_by_cc_class_name(class_with_method) 534 unittest_utils.assert_equal_testinfos( 535 self, 536 t_infos[0], 537 uc.CC_METHOD_INFO) 538 mock_build.return_value = uc.MODULE_BUILD_TARGETS 539 class_methods = '%s,%s' % (class_with_method, uc.CC_METHOD2_NAME) 540 t_infos = self.mod_finder.find_test_by_cc_class_name(class_methods) 541 unittest_utils.assert_equal_testinfos( 542 self, t_infos[0], 543 uc.CC_METHOD2_INFO) 544 # module and rel_config passed in 545 mock_build.return_value = uc.CLASS_BUILD_TARGETS 546 t_infos = self.mod_finder.find_test_by_cc_class_name( 547 uc.CC_CLASS_NAME, uc.CC_MODULE_NAME, uc.CC_CONFIG_FILE) 548 unittest_utils.assert_equal_testinfos( 549 self, t_infos[0], uc.CC_CLASS_INFO) 550 # find output fails to find class file 551 mock_checkoutput.return_value = '' 552 self.assertIsNone(self.mod_finder.find_test_by_cc_class_name( 553 'Not class')) 554 # class is outside given module path 555 mock_checkoutput.return_value = uc.CC_FIND_ONE 556 t_infos = self.mod_finder.find_test_by_cc_class_name( 557 uc.CC_CLASS_NAME, 558 uc.CC_MODULE2_NAME, 559 uc.CC_CONFIG2_FILE) 560 unittest_utils.assert_equal_testinfos( 561 self, t_infos[0], 562 CC_CLASS_INFO_MODULE_2) 563 564 def test_get_testable_modules_with_ld(self): 565 """Test get_testable_modules_with_ld""" 566 self.mod_finder.module_info.get_testable_modules.return_value = [ 567 uc.MODULE_NAME, uc.MODULE2_NAME] 568 # Without a misfit constraint 569 ld1 = self.mod_finder.get_testable_modules_with_ld(uc.TYPO_MODULE_NAME) 570 self.assertEqual([[16, uc.MODULE2_NAME], [1, uc.MODULE_NAME]], ld1) 571 # With a misfit constraint 572 ld2 = self.mod_finder.get_testable_modules_with_ld(uc.TYPO_MODULE_NAME, 2) 573 self.assertEqual([[1, uc.MODULE_NAME]], ld2) 574 575 def test_get_fuzzy_searching_modules(self): 576 """Test get_fuzzy_searching_modules""" 577 self.mod_finder.module_info.get_testable_modules.return_value = [ 578 uc.MODULE_NAME, uc.MODULE2_NAME] 579 result = self.mod_finder.get_fuzzy_searching_results(uc.TYPO_MODULE_NAME) 580 self.assertEqual(uc.MODULE_NAME, result[0]) 581 582 def test_get_build_targets_w_vts_core(self): 583 """Test _get_build_targets.""" 584 self.mod_finder.module_info.is_auto_gen_test_config.return_value = True 585 self.mod_finder.module_info.get_paths.return_value = [] 586 mod_info = {constants.MODULE_COMPATIBILITY_SUITES: 587 [constants.VTS_CORE_SUITE]} 588 self.mod_finder.module_info.get_module_info.return_value = mod_info 589 self.assertEqual(self.mod_finder._get_build_targets('', ''), 590 {constants.VTS_CORE_TF_MODULE}) 591 592 593if __name__ == '__main__': 594 unittest.main() 595