1*c2e18aaaSAndroid Build Coastguard Worker# Copyright 2017, The Android Open Source Project 2*c2e18aaaSAndroid Build Coastguard Worker# 3*c2e18aaaSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); 4*c2e18aaaSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License. 5*c2e18aaaSAndroid Build Coastguard Worker# You may obtain a copy of the License at 6*c2e18aaaSAndroid Build Coastguard Worker# 7*c2e18aaaSAndroid Build Coastguard Worker# http://www.apache.org/licenses/LICENSE-2.0 8*c2e18aaaSAndroid Build Coastguard Worker# 9*c2e18aaaSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software 10*c2e18aaaSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, 11*c2e18aaaSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12*c2e18aaaSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and 13*c2e18aaaSAndroid Build Coastguard Worker# limitations under the License. 14*c2e18aaaSAndroid Build Coastguard Worker 15*c2e18aaaSAndroid Build Coastguard Worker"""Utility functions for unit tests.""" 16*c2e18aaaSAndroid Build Coastguard Worker 17*c2e18aaaSAndroid Build Coastguard Workerimport os 18*c2e18aaaSAndroid Build Coastguard Worker 19*c2e18aaaSAndroid Build Coastguard Workerfrom atest import constants 20*c2e18aaaSAndroid Build Coastguard Workerfrom atest import unittest_constants as uc 21*c2e18aaaSAndroid Build Coastguard Worker 22*c2e18aaaSAndroid Build Coastguard Worker 23*c2e18aaaSAndroid Build Coastguard Workerdef assert_strict_equal(test_class, first, second): 24*c2e18aaaSAndroid Build Coastguard Worker """Check for strict equality and strict equality of nametuple elements. 25*c2e18aaaSAndroid Build Coastguard Worker 26*c2e18aaaSAndroid Build Coastguard Worker assertEqual considers types equal to their subtypes, but we want to 27*c2e18aaaSAndroid Build Coastguard Worker not consider set() and frozenset() equal for testing. 28*c2e18aaaSAndroid Build Coastguard Worker """ 29*c2e18aaaSAndroid Build Coastguard Worker # Allow 2 lists with different order but the same content equal. 30*c2e18aaaSAndroid Build Coastguard Worker if isinstance(first, list) and isinstance(second, list): 31*c2e18aaaSAndroid Build Coastguard Worker first.sort() 32*c2e18aaaSAndroid Build Coastguard Worker second.sort() 33*c2e18aaaSAndroid Build Coastguard Worker test_class.assertEqual(first, second) 34*c2e18aaaSAndroid Build Coastguard Worker # allow byte and unicode string equality. 35*c2e18aaaSAndroid Build Coastguard Worker if not (isinstance(first, str) and isinstance(second, str)): 36*c2e18aaaSAndroid Build Coastguard Worker test_class.assertIsInstance(first, type(second)) 37*c2e18aaaSAndroid Build Coastguard Worker test_class.assertIsInstance(second, type(first)) 38*c2e18aaaSAndroid Build Coastguard Worker # Recursively check elements of namedtuples for strict equals. 39*c2e18aaaSAndroid Build Coastguard Worker if isinstance(first, tuple) and hasattr(first, '_fields'): 40*c2e18aaaSAndroid Build Coastguard Worker # pylint: disable=invalid-name 41*c2e18aaaSAndroid Build Coastguard Worker for f in first._fields: 42*c2e18aaaSAndroid Build Coastguard Worker assert_strict_equal(test_class, getattr(first, f), getattr(second, f)) 43*c2e18aaaSAndroid Build Coastguard Worker 44*c2e18aaaSAndroid Build Coastguard Worker 45*c2e18aaaSAndroid Build Coastguard Workerdef assert_equal_testinfos(test_class, test_info_a, test_info_b): 46*c2e18aaaSAndroid Build Coastguard Worker """Check that the passed in TestInfos are equal.""" 47*c2e18aaaSAndroid Build Coastguard Worker # Use unittest.assertEqual to do checks when None is involved. 48*c2e18aaaSAndroid Build Coastguard Worker if test_info_a is None or test_info_b is None: 49*c2e18aaaSAndroid Build Coastguard Worker test_class.assertEqual(test_info_a, test_info_b) 50*c2e18aaaSAndroid Build Coastguard Worker return 51*c2e18aaaSAndroid Build Coastguard Worker 52*c2e18aaaSAndroid Build Coastguard Worker for attr in test_info_a.__dict__: 53*c2e18aaaSAndroid Build Coastguard Worker test_info_a_attr = getattr(test_info_a, attr) 54*c2e18aaaSAndroid Build Coastguard Worker test_info_b_attr = getattr(test_info_b, attr) 55*c2e18aaaSAndroid Build Coastguard Worker test_class.assertEqual( 56*c2e18aaaSAndroid Build Coastguard Worker test_info_a_attr, 57*c2e18aaaSAndroid Build Coastguard Worker test_info_b_attr, 58*c2e18aaaSAndroid Build Coastguard Worker msg=( 59*c2e18aaaSAndroid Build Coastguard Worker 'TestInfo.%s mismatch: %s != %s' 60*c2e18aaaSAndroid Build Coastguard Worker % (attr, test_info_a_attr, test_info_b_attr) 61*c2e18aaaSAndroid Build Coastguard Worker ), 62*c2e18aaaSAndroid Build Coastguard Worker ) 63*c2e18aaaSAndroid Build Coastguard Worker 64*c2e18aaaSAndroid Build Coastguard Worker 65*c2e18aaaSAndroid Build Coastguard Workerdef assert_equal_testinfo_sets(test_class, test_info_set_a, test_info_set_b): 66*c2e18aaaSAndroid Build Coastguard Worker """Check that the sets of TestInfos are equal.""" 67*c2e18aaaSAndroid Build Coastguard Worker test_class.assertEqual( 68*c2e18aaaSAndroid Build Coastguard Worker len(test_info_set_a), 69*c2e18aaaSAndroid Build Coastguard Worker len(test_info_set_b), 70*c2e18aaaSAndroid Build Coastguard Worker msg=( 71*c2e18aaaSAndroid Build Coastguard Worker 'mismatch # of TestInfos: %d != %d' 72*c2e18aaaSAndroid Build Coastguard Worker % (len(test_info_set_a), len(test_info_set_b)) 73*c2e18aaaSAndroid Build Coastguard Worker ), 74*c2e18aaaSAndroid Build Coastguard Worker ) 75*c2e18aaaSAndroid Build Coastguard Worker # Iterate over a set and pop them out as you compare them. 76*c2e18aaaSAndroid Build Coastguard Worker while test_info_set_a: 77*c2e18aaaSAndroid Build Coastguard Worker test_info_a = test_info_set_a.pop() 78*c2e18aaaSAndroid Build Coastguard Worker test_info_b_to_remove = None 79*c2e18aaaSAndroid Build Coastguard Worker for test_info_b in test_info_set_b: 80*c2e18aaaSAndroid Build Coastguard Worker try: 81*c2e18aaaSAndroid Build Coastguard Worker assert_equal_testinfos(test_class, test_info_a, test_info_b) 82*c2e18aaaSAndroid Build Coastguard Worker test_info_b_to_remove = test_info_b 83*c2e18aaaSAndroid Build Coastguard Worker break 84*c2e18aaaSAndroid Build Coastguard Worker except AssertionError: 85*c2e18aaaSAndroid Build Coastguard Worker pass 86*c2e18aaaSAndroid Build Coastguard Worker if test_info_b_to_remove: 87*c2e18aaaSAndroid Build Coastguard Worker test_info_set_b.remove(test_info_b_to_remove) 88*c2e18aaaSAndroid Build Coastguard Worker else: 89*c2e18aaaSAndroid Build Coastguard Worker # We haven't found a match, raise an assertion error. 90*c2e18aaaSAndroid Build Coastguard Worker raise AssertionError( 91*c2e18aaaSAndroid Build Coastguard Worker 'No matching TestInfo (%s) in [%s]' 92*c2e18aaaSAndroid Build Coastguard Worker % (test_info_a, ';'.join([str(t) for t in test_info_set_b])) 93*c2e18aaaSAndroid Build Coastguard Worker ) 94*c2e18aaaSAndroid Build Coastguard Worker 95*c2e18aaaSAndroid Build Coastguard Worker 96*c2e18aaaSAndroid Build Coastguard Workerdef assert_equal_testinfo_lists(test_class, test_info_list_a, test_info_list_b): 97*c2e18aaaSAndroid Build Coastguard Worker """Check that the passed in TestInfos are equal.""" 98*c2e18aaaSAndroid Build Coastguard Worker # Use unittest.assertEqual to do checks when None is involved. 99*c2e18aaaSAndroid Build Coastguard Worker if test_info_list_a is None or test_info_list_a is None: 100*c2e18aaaSAndroid Build Coastguard Worker test_class.assertEqual(test_info_list_a, test_info_list_a) 101*c2e18aaaSAndroid Build Coastguard Worker return 102*c2e18aaaSAndroid Build Coastguard Worker 103*c2e18aaaSAndroid Build Coastguard Worker for i, test_info_a in enumerate(test_info_list_a): 104*c2e18aaaSAndroid Build Coastguard Worker assert_equal_testinfos(test_class, test_info_a, test_info_list_b[i]) 105*c2e18aaaSAndroid Build Coastguard Worker 106*c2e18aaaSAndroid Build Coastguard Worker 107*c2e18aaaSAndroid Build Coastguard Worker# pylint: disable=too-many-return-statements 108*c2e18aaaSAndroid Build Coastguard Workerdef isfile_side_effect(value): 109*c2e18aaaSAndroid Build Coastguard Worker """Mock return values for os.path.isfile.""" 110*c2e18aaaSAndroid Build Coastguard Worker value = str(value) 111*c2e18aaaSAndroid Build Coastguard Worker if value == '/%s/%s' % (uc.CC_MODULE_DIR, constants.MODULE_CONFIG): 112*c2e18aaaSAndroid Build Coastguard Worker return True 113*c2e18aaaSAndroid Build Coastguard Worker if value == '/%s/%s' % (uc.MODULE_DIR, constants.MODULE_CONFIG): 114*c2e18aaaSAndroid Build Coastguard Worker return True 115*c2e18aaaSAndroid Build Coastguard Worker if value.endswith('.cc'): 116*c2e18aaaSAndroid Build Coastguard Worker return True 117*c2e18aaaSAndroid Build Coastguard Worker if value.endswith('.cpp'): 118*c2e18aaaSAndroid Build Coastguard Worker return True 119*c2e18aaaSAndroid Build Coastguard Worker if value.endswith('.java'): 120*c2e18aaaSAndroid Build Coastguard Worker return True 121*c2e18aaaSAndroid Build Coastguard Worker if value.endswith('.kt'): 122*c2e18aaaSAndroid Build Coastguard Worker return True 123*c2e18aaaSAndroid Build Coastguard Worker if value.endswith(uc.INT_NAME + '.xml'): 124*c2e18aaaSAndroid Build Coastguard Worker return True 125*c2e18aaaSAndroid Build Coastguard Worker if value.endswith(uc.GTF_INT_NAME + '.xml'): 126*c2e18aaaSAndroid Build Coastguard Worker return True 127*c2e18aaaSAndroid Build Coastguard Worker if value.endswith( 128*c2e18aaaSAndroid Build Coastguard Worker '/%s/%s' % (uc.ANDTEST_CONFIG_PATH, constants.MODULE_CONFIG) 129*c2e18aaaSAndroid Build Coastguard Worker ): 130*c2e18aaaSAndroid Build Coastguard Worker return True 131*c2e18aaaSAndroid Build Coastguard Worker if value.endswith('/%s/%s' % (uc.SINGLE_CONFIG_PATH, uc.SINGLE_CONFIG_NAME)): 132*c2e18aaaSAndroid Build Coastguard Worker return True 133*c2e18aaaSAndroid Build Coastguard Worker if value.endswith('/%s/%s' % (uc.MULTIPLE_CONFIG_PATH, uc.MAIN_CONFIG_NAME)): 134*c2e18aaaSAndroid Build Coastguard Worker return True 135*c2e18aaaSAndroid Build Coastguard Worker if value.endswith('/%s/%s' % (uc.MULTIPLE_CONFIG_PATH, uc.SUB_CONFIG_NAME_2)): 136*c2e18aaaSAndroid Build Coastguard Worker return True 137*c2e18aaaSAndroid Build Coastguard Worker return False 138*c2e18aaaSAndroid Build Coastguard Worker 139*c2e18aaaSAndroid Build Coastguard Worker 140*c2e18aaaSAndroid Build Coastguard Workerdef realpath_side_effect(path): 141*c2e18aaaSAndroid Build Coastguard Worker """Mock return values for os.path.realpath.""" 142*c2e18aaaSAndroid Build Coastguard Worker return os.path.join(uc.ROOT, path) 143