1#!/usr/bin/env python3 2# 3# Copyright 2019, 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 native_util.""" 18 19import os 20import unittest 21from unittest import mock 22 23from aidegen import unittest_constants 24from aidegen.lib import common_util 25from aidegen.lib import native_module_info 26from aidegen.lib import native_util 27 28 29# pylint: disable=protected-access 30# pylint: disable=invalid-name 31class AidegenNativeUtilUnittests(unittest.TestCase): 32 """Unit tests for native_util.py""" 33 34 @mock.patch.object(native_util, '_check_native_project_exists') 35 @mock.patch.object(common_util, 'check_java_or_kotlin_file_exists') 36 @mock.patch.object(common_util, 'get_related_paths') 37 def test_analyze_native_and_java_projects( 38 self, mock_get_related, mock_check_java, mock_check_native): 39 """Test analyze_native_and_java_projects function.""" 40 mock_get_related.return_value = None, None 41 mock_check_java.return_value = True 42 mock_check_native.return_value = True 43 targets = ['a'] 44 self.assertEqual((targets, targets), 45 native_util._analyze_native_and_java_projects( 46 None, None, targets)) 47 mock_check_native.return_value = False 48 self.assertEqual((targets, []), 49 native_util._analyze_native_and_java_projects( 50 None, None, targets)) 51 mock_check_java.return_value = False 52 mock_check_native.return_value = True 53 self.assertEqual(([], targets), 54 native_util._analyze_native_and_java_projects( 55 None, None, targets)) 56 57 def test_check_native_project_exists(self): 58 """Test _check_native_project_exists function.""" 59 rel_path = 'a/b' 60 path_to_module_info = {'a/b/c': {}} 61 self.assertTrue( 62 native_util._check_native_project_exists(path_to_module_info, 63 rel_path)) 64 rel_path = 'a/b/c/d' 65 self.assertFalse( 66 native_util._check_native_project_exists(path_to_module_info, 67 rel_path)) 68 69 def test_find_parent(self): 70 """Test _find_parent function with conditions.""" 71 current_parent = None 72 abs_path = 'a/b/c/d' 73 expected = abs_path 74 result = native_util._find_parent(abs_path, current_parent) 75 self.assertEqual(result, expected) 76 current_parent = 'a/b/c/d/e' 77 result = native_util._find_parent(abs_path, current_parent) 78 self.assertEqual(result, expected) 79 current_parent = 'a/b/c' 80 expected = current_parent 81 result = native_util._find_parent(abs_path, current_parent) 82 self.assertEqual(result, expected) 83 current_parent = 'a/b/f' 84 expected = 'a/b' 85 result = native_util._find_parent(abs_path, current_parent) 86 self.assertEqual(result, expected) 87 88 @mock.patch.object(native_module_info.NativeModuleInfo, 89 '_load_module_info_file') 90 @mock.patch.object(native_util, '_find_parent') 91 @mock.patch.object(common_util, 'get_related_paths') 92 def test_get_merged_native_target_is_module( 93 self, mock_get_related, mock_find_parent, mock_load_info): 94 """Test _get_merged_native_target function if the target is a module.""" 95 mock_get_related.return_value = 'c/d', 'a/b/c/d' 96 parent = 'a/b' 97 mock_find_parent.return_value = parent 98 targets = ['multiarch'] 99 expected = (parent, targets) 100 mock_load_info.return_value = ( 101 None, unittest_constants.CC_NAME_TO_MODULE_INFO) 102 cc_mod_info = native_module_info.NativeModuleInfo() 103 new_parent, new_targets = native_util._get_merged_native_target( 104 cc_mod_info, targets) 105 result = (new_parent, new_targets) 106 self.assertEqual(result, expected) 107 108 @mock.patch.object(native_module_info.NativeModuleInfo, 109 '_load_module_info_file') 110 @mock.patch.object(native_util, '_find_parent') 111 @mock.patch.object(common_util, 'get_related_paths') 112 def test_get_merged_native_target_is_path(self, mock_get_related, 113 mock_find_parent, mock_load_info): 114 """Test _get_merged_native_target function if the target is a path.""" 115 parent = 'a/b' 116 rel_path = 'shared/path/to/be/used2' 117 mock_get_related.return_value = rel_path, os.path.join(parent, rel_path) 118 mock_find_parent.return_value = parent 119 mock_load_info.return_value = ( 120 None, unittest_constants.CC_NAME_TO_MODULE_INFO) 121 targets = [rel_path] 122 result_targets = unittest_constants.TESTABLE_MODULES_WITH_SHARED_PATH 123 expected = (parent, result_targets) 124 cc_mod_info = native_module_info.NativeModuleInfo() 125 new_parent, new_targets = native_util._get_merged_native_target( 126 cc_mod_info, targets) 127 result = (new_parent, new_targets) 128 self.assertEqual(result, expected) 129 130 131 def test_filter_out_modules(self): 132 """Test _filter_out_modules with conditions.""" 133 targets = ['shared/path/to/be/used2'] 134 result = ([], targets) 135 self.assertEqual( 136 result, native_util._filter_out_modules(targets, lambda x: False)) 137 targets = ['multiarch'] 138 result = (targets, []) 139 self.assertEqual( 140 result, native_util._filter_out_modules(targets, lambda x: True)) 141 142 @mock.patch.object(native_util, '_filter_out_rust_projects') 143 @mock.patch.object(native_util, '_analyze_native_and_java_projects') 144 @mock.patch.object(native_util, '_filter_out_modules') 145 def test_get_java_cc_and_rust_projects(self, mock_fil, mock_ana, 146 mock_fil_rust): 147 """Test get_java_cc_and_rust_projects handling.""" 148 targets = ['multiarch'] 149 mock_fil_rust.return_value = [] 150 mock_fil.return_value = [], targets 151 cc_mod_info = mock.Mock() 152 cc_mod_info.is_module = mock.Mock() 153 cc_mod_info.is_module.return_value = True 154 at_mod_info = mock.Mock() 155 at_mod_info.is_module = mock.Mock() 156 at_mod_info.is_module.return_value = True 157 mock_ana.return_value = [], targets 158 native_util.get_java_cc_and_rust_projects( 159 at_mod_info, cc_mod_info, targets) 160 self.assertEqual(mock_fil.call_count, 2) 161 self.assertEqual(mock_ana.call_count, 1) 162 163 @mock.patch.object(native_util, '_get_rust_targets') 164 @mock.patch.object(common_util, 'get_json_dict') 165 @mock.patch('builtins.print') 166 @mock.patch('os.path.isfile') 167 @mock.patch('os.path.join') 168 @mock.patch.object(common_util, 'get_blueprint_json_path') 169 @mock.patch.object(common_util, 'get_android_root_dir') 170 def test_filter_out_rust_projects(self, mock_get_root, mock_get_json, 171 mock_join, mock_is_file, mock_print, 172 mock_get_dict, mock_get_rust): 173 """Test _filter_out_rust_projects with conditions.""" 174 mock_is_file.return_value = False 175 native_util._filter_out_rust_projects(['a/b/rust']) 176 self.assertTrue(mock_get_root.called) 177 self.assertTrue(mock_get_json.called) 178 self.assertTrue(mock_join.called) 179 self.assertTrue(mock_print.called) 180 self.assertFalse(mock_get_dict.called) 181 self.assertFalse(mock_get_rust.called) 182 183 mock_get_root.mock_reset() 184 mock_get_json.mock_reset() 185 mock_join.mock_reset() 186 mock_print.mock_reset() 187 mock_get_dict.mock_reset() 188 mock_get_rust.mock_reset() 189 mock_is_file.return_value = True 190 mock_get_dict.return_value = {} 191 native_util._filter_out_rust_projects(['a/b/rust']) 192 self.assertTrue(mock_get_root.called) 193 self.assertTrue(mock_get_json.called) 194 self.assertTrue(mock_join.called) 195 self.assertTrue(mock_print.called) 196 self.assertFalse(mock_get_rust.called) 197 198 mock_get_root.mock_reset() 199 mock_get_json.mock_reset() 200 mock_join.mock_reset() 201 mock_print.mock_reset() 202 mock_get_rust.mock_reset() 203 mock_is_file.return_value = True 204 crates = [{native_util._ROOT_MODULE_KEY: 'a/b/rust/src'}] 205 mock_get_dict.return_value = {native_util._CRATES_KEY: crates} 206 mock_get_root.return_value = 'a/b' 207 native_util._filter_out_rust_projects(['a/b/rust']) 208 self.assertTrue(mock_get_json.called) 209 self.assertTrue(mock_join.called) 210 self.assertTrue(mock_get_rust.called) 211 mock_get_rust.assert_called_with(['a/b/rust'], crates, 'a/b') 212 213 @mock.patch.object(common_util, 'is_source_under_relative_path') 214 @mock.patch('os.path.isdir') 215 def test_get_rust_targets(self, mock_is_dir, mock_is_under): 216 """Test _get_rust_targets with conditions.""" 217 mock_is_dir.return_value = True 218 mock_is_under.return_value = True 219 targets = ['a/b/rust'] 220 self.assertEqual( 221 targets, 222 native_util._get_rust_targets( 223 targets, [{native_util._ROOT_MODULE_KEY: 'a/b/rust/src'}], 224 'a/b')) 225 226 227if __name__ == '__main__': 228 unittest.main() 229