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