1*c2e18aaaSAndroid Build Coastguard Worker#!/usr/bin/env python3 2*c2e18aaaSAndroid Build Coastguard Worker# 3*c2e18aaaSAndroid Build Coastguard Worker# Copyright 2018, The Android Open Source Project 4*c2e18aaaSAndroid Build Coastguard Worker# 5*c2e18aaaSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); 6*c2e18aaaSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License. 7*c2e18aaaSAndroid Build Coastguard Worker# You may obtain a copy of the License at 8*c2e18aaaSAndroid Build Coastguard Worker# 9*c2e18aaaSAndroid Build Coastguard Worker# http://www.apache.org/licenses/LICENSE-2.0 10*c2e18aaaSAndroid Build Coastguard Worker# 11*c2e18aaaSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software 12*c2e18aaaSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, 13*c2e18aaaSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14*c2e18aaaSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and 15*c2e18aaaSAndroid Build Coastguard Worker# limitations under the License. 16*c2e18aaaSAndroid Build Coastguard Worker 17*c2e18aaaSAndroid Build Coastguard Worker"""Unittests for common_util.""" 18*c2e18aaaSAndroid Build Coastguard Worker 19*c2e18aaaSAndroid Build Coastguard Workerimport logging 20*c2e18aaaSAndroid Build Coastguard Workerimport os 21*c2e18aaaSAndroid Build Coastguard Workerimport unittest 22*c2e18aaaSAndroid Build Coastguard Workerfrom unittest import mock 23*c2e18aaaSAndroid Build Coastguard Workerfrom xml.etree import ElementTree 24*c2e18aaaSAndroid Build Coastguard Worker 25*c2e18aaaSAndroid Build Coastguard Workerfrom aidegen import constant 26*c2e18aaaSAndroid Build Coastguard Workerfrom aidegen import unittest_constants 27*c2e18aaaSAndroid Build Coastguard Workerfrom aidegen.lib import common_util 28*c2e18aaaSAndroid Build Coastguard Workerfrom aidegen.lib import errors 29*c2e18aaaSAndroid Build Coastguard Worker 30*c2e18aaaSAndroid Build Coastguard Workerfrom atest import module_info 31*c2e18aaaSAndroid Build Coastguard Worker 32*c2e18aaaSAndroid Build Coastguard Worker 33*c2e18aaaSAndroid Build Coastguard Worker# pylint: disable=unused-argument 34*c2e18aaaSAndroid Build Coastguard Worker# pylint: disable=protected-access 35*c2e18aaaSAndroid Build Coastguard Workerclass AidegenCommonUtilUnittests(unittest.TestCase): 36*c2e18aaaSAndroid Build Coastguard Worker """Unit tests for common_util.py""" 37*c2e18aaaSAndroid Build Coastguard Worker 38*c2e18aaaSAndroid Build Coastguard Worker _TEST_XML_CONTENT = """\ 39*c2e18aaaSAndroid Build Coastguard Worker<application><component name="ProjectJdkTable"> 40*c2e18aaaSAndroid Build Coastguard Worker 41*c2e18aaaSAndroid Build Coastguard Worker <jdk version="2"> <name value="JDK_OTHER" /> 42*c2e18aaaSAndroid Build Coastguard Worker <type value="JavaSDK" /> </jdk> </component> 43*c2e18aaaSAndroid Build Coastguard Worker</application> 44*c2e18aaaSAndroid Build Coastguard Worker""" 45*c2e18aaaSAndroid Build Coastguard Worker _SAMPLE_XML_CONTENT = """\ 46*c2e18aaaSAndroid Build Coastguard Worker<application> 47*c2e18aaaSAndroid Build Coastguard Worker <component name="ProjectJdkTable"> 48*c2e18aaaSAndroid Build Coastguard Worker <jdk version="2"> 49*c2e18aaaSAndroid Build Coastguard Worker <name value="JDK_OTHER"/> 50*c2e18aaaSAndroid Build Coastguard Worker <type value="JavaSDK"/> 51*c2e18aaaSAndroid Build Coastguard Worker </jdk> 52*c2e18aaaSAndroid Build Coastguard Worker </component> 53*c2e18aaaSAndroid Build Coastguard Worker</application>""" 54*c2e18aaaSAndroid Build Coastguard Worker 55*c2e18aaaSAndroid Build Coastguard Worker @mock.patch('os.getcwd') 56*c2e18aaaSAndroid Build Coastguard Worker @mock.patch('os.path.isabs') 57*c2e18aaaSAndroid Build Coastguard Worker @mock.patch.object(common_util, 'get_android_root_dir') 58*c2e18aaaSAndroid Build Coastguard Worker def test_get_related_paths(self, mock_get_root, mock_is_abspath, mock_cwd): 59*c2e18aaaSAndroid Build Coastguard Worker """Test get_related_paths with different conditions.""" 60*c2e18aaaSAndroid Build Coastguard Worker mod_info = mock.MagicMock() 61*c2e18aaaSAndroid Build Coastguard Worker mod_info.is_module.return_value = True 62*c2e18aaaSAndroid Build Coastguard Worker mod_info.get_paths.return_value = {} 63*c2e18aaaSAndroid Build Coastguard Worker mock_is_abspath.return_value = False 64*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual((None, None), 65*c2e18aaaSAndroid Build Coastguard Worker common_util.get_related_paths( 66*c2e18aaaSAndroid Build Coastguard Worker mod_info, unittest_constants.TEST_MODULE)) 67*c2e18aaaSAndroid Build Coastguard Worker mock_get_root.return_value = unittest_constants.TEST_PATH 68*c2e18aaaSAndroid Build Coastguard Worker mod_info.get_paths.return_value = [unittest_constants.TEST_MODULE] 69*c2e18aaaSAndroid Build Coastguard Worker expected = (unittest_constants.TEST_MODULE, os.path.join( 70*c2e18aaaSAndroid Build Coastguard Worker unittest_constants.TEST_PATH, unittest_constants.TEST_MODULE)) 71*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual( 72*c2e18aaaSAndroid Build Coastguard Worker expected, common_util.get_related_paths( 73*c2e18aaaSAndroid Build Coastguard Worker mod_info, unittest_constants.TEST_MODULE)) 74*c2e18aaaSAndroid Build Coastguard Worker mod_info.is_module.return_value = False 75*c2e18aaaSAndroid Build Coastguard Worker mod_info.get_module_names.return_value = True 76*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(expected, common_util.get_related_paths( 77*c2e18aaaSAndroid Build Coastguard Worker mod_info, unittest_constants.TEST_MODULE)) 78*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(('', unittest_constants.TEST_PATH), 79*c2e18aaaSAndroid Build Coastguard Worker common_util.get_related_paths( 80*c2e18aaaSAndroid Build Coastguard Worker mod_info, constant.WHOLE_ANDROID_TREE_TARGET)) 81*c2e18aaaSAndroid Build Coastguard Worker 82*c2e18aaaSAndroid Build Coastguard Worker mod_info.is_module.return_value = False 83*c2e18aaaSAndroid Build Coastguard Worker mod_info.get_module_names.return_value = False 84*c2e18aaaSAndroid Build Coastguard Worker mock_is_abspath.return_value = True 85*c2e18aaaSAndroid Build Coastguard Worker mock_get_root.return_value = '/a' 86*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(('b/c', '/a/b/c'), 87*c2e18aaaSAndroid Build Coastguard Worker common_util.get_related_paths(mod_info, '/a/b/c')) 88*c2e18aaaSAndroid Build Coastguard Worker 89*c2e18aaaSAndroid Build Coastguard Worker mock_is_abspath.return_value = False 90*c2e18aaaSAndroid Build Coastguard Worker mock_cwd.return_value = '/a' 91*c2e18aaaSAndroid Build Coastguard Worker mock_get_root.return_value = '/a' 92*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(('b/c', '/a/b/c'), 93*c2e18aaaSAndroid Build Coastguard Worker common_util.get_related_paths(mod_info, 'b/c')) 94*c2e18aaaSAndroid Build Coastguard Worker 95*c2e18aaaSAndroid Build Coastguard Worker @mock.patch('os.getcwd') 96*c2e18aaaSAndroid Build Coastguard Worker @mock.patch.object(common_util, 'is_android_root') 97*c2e18aaaSAndroid Build Coastguard Worker @mock.patch.object(common_util, 'get_android_root_dir') 98*c2e18aaaSAndroid Build Coastguard Worker def test_get_related_paths_2( 99*c2e18aaaSAndroid Build Coastguard Worker self, mock_get_root, mock_is_root, mock_getcwd): 100*c2e18aaaSAndroid Build Coastguard Worker """Test get_related_paths with different conditions.""" 101*c2e18aaaSAndroid Build Coastguard Worker 102*c2e18aaaSAndroid Build Coastguard Worker mock_get_root.return_value = '/a' 103*c2e18aaaSAndroid Build Coastguard Worker mod_info = mock.MagicMock() 104*c2e18aaaSAndroid Build Coastguard Worker 105*c2e18aaaSAndroid Build Coastguard Worker # Test get_module_names returns False, user inputs a relative path of 106*c2e18aaaSAndroid Build Coastguard Worker # current directory. 107*c2e18aaaSAndroid Build Coastguard Worker mod_info.is_mod.return_value = False 108*c2e18aaaSAndroid Build Coastguard Worker rel_path = 'b/c/d' 109*c2e18aaaSAndroid Build Coastguard Worker abs_path = '/a/b/c/d' 110*c2e18aaaSAndroid Build Coastguard Worker mod_info.get_paths.return_value = [rel_path] 111*c2e18aaaSAndroid Build Coastguard Worker mod_info.get_module_names.return_value = False 112*c2e18aaaSAndroid Build Coastguard Worker mock_getcwd.return_value = '/a/b/c' 113*c2e18aaaSAndroid Build Coastguard Worker input_target = 'd' 114*c2e18aaaSAndroid Build Coastguard Worker # expected tuple: (rel_path, abs_path) 115*c2e18aaaSAndroid Build Coastguard Worker expected = (rel_path, abs_path) 116*c2e18aaaSAndroid Build Coastguard Worker result = common_util.get_related_paths(mod_info, input_target) 117*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(expected, result) 118*c2e18aaaSAndroid Build Coastguard Worker 119*c2e18aaaSAndroid Build Coastguard Worker # Test user doesn't input target and current working directory is the 120*c2e18aaaSAndroid Build Coastguard Worker # android root folder. 121*c2e18aaaSAndroid Build Coastguard Worker mock_getcwd.return_value = '/a' 122*c2e18aaaSAndroid Build Coastguard Worker mock_is_root.return_value = True 123*c2e18aaaSAndroid Build Coastguard Worker expected = ('', '/a') 124*c2e18aaaSAndroid Build Coastguard Worker result = common_util.get_related_paths(mod_info, target=None) 125*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(expected, result) 126*c2e18aaaSAndroid Build Coastguard Worker 127*c2e18aaaSAndroid Build Coastguard Worker # Test user doesn't input target and current working directory is not 128*c2e18aaaSAndroid Build Coastguard Worker # android root folder. 129*c2e18aaaSAndroid Build Coastguard Worker mock_getcwd.return_value = '/a/b' 130*c2e18aaaSAndroid Build Coastguard Worker mock_is_root.return_value = False 131*c2e18aaaSAndroid Build Coastguard Worker expected = ('b', '/a/b') 132*c2e18aaaSAndroid Build Coastguard Worker result = common_util.get_related_paths(mod_info, target=None) 133*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(expected, result) 134*c2e18aaaSAndroid Build Coastguard Worker result = common_util.get_related_paths(mod_info, target='.') 135*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(expected, result) 136*c2e18aaaSAndroid Build Coastguard Worker 137*c2e18aaaSAndroid Build Coastguard Worker @mock.patch.object(common_util, 'is_android_root') 138*c2e18aaaSAndroid Build Coastguard Worker @mock.patch.object(common_util, 'get_related_paths') 139*c2e18aaaSAndroid Build Coastguard Worker def test_is_target_android_root(self, mock_get_rel, mock_get_root): 140*c2e18aaaSAndroid Build Coastguard Worker """Test is_target_android_root with different conditions.""" 141*c2e18aaaSAndroid Build Coastguard Worker mod_info = mock.MagicMock() 142*c2e18aaaSAndroid Build Coastguard Worker mock_get_rel.return_value = None, unittest_constants.TEST_PATH 143*c2e18aaaSAndroid Build Coastguard Worker mock_get_root.return_value = True 144*c2e18aaaSAndroid Build Coastguard Worker self.assertTrue( 145*c2e18aaaSAndroid Build Coastguard Worker common_util.is_target_android_root( 146*c2e18aaaSAndroid Build Coastguard Worker mod_info, [unittest_constants.TEST_MODULE])) 147*c2e18aaaSAndroid Build Coastguard Worker mock_get_rel.return_value = None, '' 148*c2e18aaaSAndroid Build Coastguard Worker mock_get_root.return_value = False 149*c2e18aaaSAndroid Build Coastguard Worker self.assertFalse( 150*c2e18aaaSAndroid Build Coastguard Worker common_util.is_target_android_root( 151*c2e18aaaSAndroid Build Coastguard Worker mod_info, [unittest_constants.TEST_MODULE])) 152*c2e18aaaSAndroid Build Coastguard Worker 153*c2e18aaaSAndroid Build Coastguard Worker @mock.patch.object(common_util, 'get_android_root_dir') 154*c2e18aaaSAndroid Build Coastguard Worker @mock.patch.object(common_util, 'has_build_target') 155*c2e18aaaSAndroid Build Coastguard Worker @mock.patch('os.path.isdir') 156*c2e18aaaSAndroid Build Coastguard Worker @mock.patch.object(common_util, 'get_related_paths') 157*c2e18aaaSAndroid Build Coastguard Worker def test_check_module(self, mock_get, mock_isdir, mock_has_target, 158*c2e18aaaSAndroid Build Coastguard Worker mock_get_root): 159*c2e18aaaSAndroid Build Coastguard Worker """Test if _check_module raises errors with different conditions.""" 160*c2e18aaaSAndroid Build Coastguard Worker mod_info = mock.MagicMock() 161*c2e18aaaSAndroid Build Coastguard Worker mock_get.return_value = None, None 162*c2e18aaaSAndroid Build Coastguard Worker with self.assertRaises(errors.FakeModuleError) as ctx: 163*c2e18aaaSAndroid Build Coastguard Worker common_util.check_module(mod_info, unittest_constants.TEST_MODULE) 164*c2e18aaaSAndroid Build Coastguard Worker expected = common_util.FAKE_MODULE_ERROR.format( 165*c2e18aaaSAndroid Build Coastguard Worker unittest_constants.TEST_MODULE) 166*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(expected, str(ctx.exception)) 167*c2e18aaaSAndroid Build Coastguard Worker mock_get_root.return_value = unittest_constants.TEST_PATH 168*c2e18aaaSAndroid Build Coastguard Worker mock_get.return_value = None, unittest_constants.TEST_MODULE 169*c2e18aaaSAndroid Build Coastguard Worker with self.assertRaises(errors.ProjectOutsideAndroidRootError) as ctx: 170*c2e18aaaSAndroid Build Coastguard Worker common_util.check_module(mod_info, unittest_constants.TEST_MODULE) 171*c2e18aaaSAndroid Build Coastguard Worker expected = common_util.OUTSIDE_ROOT_ERROR.format( 172*c2e18aaaSAndroid Build Coastguard Worker unittest_constants.TEST_MODULE) 173*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(expected, str(ctx.exception)) 174*c2e18aaaSAndroid Build Coastguard Worker mock_get.return_value = None, unittest_constants.TEST_PATH 175*c2e18aaaSAndroid Build Coastguard Worker mock_isdir.return_value = False 176*c2e18aaaSAndroid Build Coastguard Worker with self.assertRaises(errors.ProjectPathNotExistError) as ctx: 177*c2e18aaaSAndroid Build Coastguard Worker common_util.check_module(mod_info, unittest_constants.TEST_MODULE) 178*c2e18aaaSAndroid Build Coastguard Worker expected = common_util.PATH_NOT_EXISTS_ERROR.format( 179*c2e18aaaSAndroid Build Coastguard Worker unittest_constants.TEST_MODULE) 180*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(expected, str(ctx.exception)) 181*c2e18aaaSAndroid Build Coastguard Worker mock_isdir.return_value = True 182*c2e18aaaSAndroid Build Coastguard Worker mock_has_target.return_value = False 183*c2e18aaaSAndroid Build Coastguard Worker mock_get.return_value = None, os.path.join(unittest_constants.TEST_PATH, 184*c2e18aaaSAndroid Build Coastguard Worker 'test.jar') 185*c2e18aaaSAndroid Build Coastguard Worker with self.assertRaises(errors.NoModuleDefinedInModuleInfoError) as ctx: 186*c2e18aaaSAndroid Build Coastguard Worker common_util.check_module(mod_info, unittest_constants.TEST_MODULE) 187*c2e18aaaSAndroid Build Coastguard Worker expected = common_util.NO_MODULE_DEFINED_ERROR.format( 188*c2e18aaaSAndroid Build Coastguard Worker unittest_constants.TEST_MODULE) 189*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(expected, str(ctx.exception)) 190*c2e18aaaSAndroid Build Coastguard Worker self.assertFalse(common_util.check_module(mod_info, '', False)) 191*c2e18aaaSAndroid Build Coastguard Worker self.assertFalse(common_util.check_module(mod_info, 'nothing', False)) 192*c2e18aaaSAndroid Build Coastguard Worker 193*c2e18aaaSAndroid Build Coastguard Worker @mock.patch.object(common_util, 'check_module') 194*c2e18aaaSAndroid Build Coastguard Worker def test_check_modules(self, mock_check): 195*c2e18aaaSAndroid Build Coastguard Worker """Test _check_modules with different module lists.""" 196*c2e18aaaSAndroid Build Coastguard Worker mod_info = mock.MagicMock() 197*c2e18aaaSAndroid Build Coastguard Worker common_util._check_modules(mod_info, []) 198*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(mock_check.call_count, 0) 199*c2e18aaaSAndroid Build Coastguard Worker common_util._check_modules(mod_info, ['module1', 'module2']) 200*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(mock_check.call_count, 2) 201*c2e18aaaSAndroid Build Coastguard Worker target = 'nothing' 202*c2e18aaaSAndroid Build Coastguard Worker mock_check.return_value = False 203*c2e18aaaSAndroid Build Coastguard Worker self.assertFalse(common_util._check_modules(mod_info, [target], False)) 204*c2e18aaaSAndroid Build Coastguard Worker 205*c2e18aaaSAndroid Build Coastguard Worker @mock.patch.object(common_util, 'get_android_root_dir') 206*c2e18aaaSAndroid Build Coastguard Worker def test_get_abs_path(self, mock_get_root): 207*c2e18aaaSAndroid Build Coastguard Worker """Test get_abs_path handling.""" 208*c2e18aaaSAndroid Build Coastguard Worker mock_get_root.return_value = unittest_constants.TEST_DATA_PATH 209*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(unittest_constants.TEST_DATA_PATH, 210*c2e18aaaSAndroid Build Coastguard Worker common_util.get_abs_path('')) 211*c2e18aaaSAndroid Build Coastguard Worker test_path = os.path.join(unittest_constants.TEST_DATA_PATH, 'test.jar') 212*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(test_path, common_util.get_abs_path(test_path)) 213*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(test_path, common_util.get_abs_path('test.jar')) 214*c2e18aaaSAndroid Build Coastguard Worker 215*c2e18aaaSAndroid Build Coastguard Worker def test_is_target(self): 216*c2e18aaaSAndroid Build Coastguard Worker """Test is_target handling.""" 217*c2e18aaaSAndroid Build Coastguard Worker self.assertTrue( 218*c2e18aaaSAndroid Build Coastguard Worker common_util.is_target('packages/apps/tests/test.a', ['.so', '.a'])) 219*c2e18aaaSAndroid Build Coastguard Worker self.assertTrue( 220*c2e18aaaSAndroid Build Coastguard Worker common_util.is_target('packages/apps/tests/test.so', ['.so', '.a'])) 221*c2e18aaaSAndroid Build Coastguard Worker self.assertFalse( 222*c2e18aaaSAndroid Build Coastguard Worker common_util.is_target( 223*c2e18aaaSAndroid Build Coastguard Worker 'packages/apps/tests/test.jar', ['.so', '.a'])) 224*c2e18aaaSAndroid Build Coastguard Worker 225*c2e18aaaSAndroid Build Coastguard Worker @mock.patch.object(logging, 'basicConfig') 226*c2e18aaaSAndroid Build Coastguard Worker def test_configure_logging(self, mock_log_config): 227*c2e18aaaSAndroid Build Coastguard Worker """Test configure_logging with different arguments.""" 228*c2e18aaaSAndroid Build Coastguard Worker common_util.configure_logging(True) 229*c2e18aaaSAndroid Build Coastguard Worker log_format = common_util._LOG_FORMAT 230*c2e18aaaSAndroid Build Coastguard Worker datefmt = common_util._DATE_FORMAT 231*c2e18aaaSAndroid Build Coastguard Worker level = common_util.logging.DEBUG 232*c2e18aaaSAndroid Build Coastguard Worker self.assertTrue( 233*c2e18aaaSAndroid Build Coastguard Worker mock_log_config.called_with( 234*c2e18aaaSAndroid Build Coastguard Worker level=level, format=log_format, datefmt=datefmt)) 235*c2e18aaaSAndroid Build Coastguard Worker common_util.configure_logging(False) 236*c2e18aaaSAndroid Build Coastguard Worker level = common_util.logging.INFO 237*c2e18aaaSAndroid Build Coastguard Worker self.assertTrue( 238*c2e18aaaSAndroid Build Coastguard Worker mock_log_config.called_with( 239*c2e18aaaSAndroid Build Coastguard Worker level=level, format=log_format, datefmt=datefmt)) 240*c2e18aaaSAndroid Build Coastguard Worker 241*c2e18aaaSAndroid Build Coastguard Worker @mock.patch.object(common_util, '_check_modules') 242*c2e18aaaSAndroid Build Coastguard Worker @mock.patch.object(module_info, 'ModuleInfo') 243*c2e18aaaSAndroid Build Coastguard Worker def test_get_atest_module_info(self, mock_modinfo, mock_check_modules): 244*c2e18aaaSAndroid Build Coastguard Worker """Test get_atest_module_info handling.""" 245*c2e18aaaSAndroid Build Coastguard Worker common_util.get_atest_module_info() 246*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(mock_modinfo.call_count, 1) 247*c2e18aaaSAndroid Build Coastguard Worker mock_modinfo.reset_mock() 248*c2e18aaaSAndroid Build Coastguard Worker mock_check_modules.return_value = False 249*c2e18aaaSAndroid Build Coastguard Worker common_util.get_atest_module_info(['nothing']) 250*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(mock_modinfo.call_count, 2) 251*c2e18aaaSAndroid Build Coastguard Worker 252*c2e18aaaSAndroid Build Coastguard Worker @mock.patch('builtins.open', create=True) 253*c2e18aaaSAndroid Build Coastguard Worker def test_read_file_content(self, mock_open): 254*c2e18aaaSAndroid Build Coastguard Worker """Test read_file_content handling.""" 255*c2e18aaaSAndroid Build Coastguard Worker expected_data1 = 'Data1' 256*c2e18aaaSAndroid Build Coastguard Worker file_a = 'fileA' 257*c2e18aaaSAndroid Build Coastguard Worker mock_open.side_effect = [ 258*c2e18aaaSAndroid Build Coastguard Worker mock.mock_open(read_data=expected_data1).return_value 259*c2e18aaaSAndroid Build Coastguard Worker ] 260*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(expected_data1, common_util.read_file_content(file_a)) 261*c2e18aaaSAndroid Build Coastguard Worker mock_open.assert_called_once_with(file_a, 'r', encoding='utf8') 262*c2e18aaaSAndroid Build Coastguard Worker 263*c2e18aaaSAndroid Build Coastguard Worker @mock.patch('os.getenv') 264*c2e18aaaSAndroid Build Coastguard Worker @mock.patch.object(common_util, 'get_android_root_dir') 265*c2e18aaaSAndroid Build Coastguard Worker def test_get_android_out_dir(self, mock_get_android_root_dir, mock_getenv): 266*c2e18aaaSAndroid Build Coastguard Worker """Test get_android_out_dir handling.""" 267*c2e18aaaSAndroid Build Coastguard Worker root = 'my/path-to-root/master' 268*c2e18aaaSAndroid Build Coastguard Worker default_root = 'out' 269*c2e18aaaSAndroid Build Coastguard Worker android_out_root = 'eng_out' 270*c2e18aaaSAndroid Build Coastguard Worker mock_get_android_root_dir.return_value = root 271*c2e18aaaSAndroid Build Coastguard Worker mock_getenv.side_effect = ['', ''] 272*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(default_root, common_util.get_android_out_dir()) 273*c2e18aaaSAndroid Build Coastguard Worker mock_getenv.side_effect = [android_out_root, ''] 274*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(android_out_root, common_util.get_android_out_dir()) 275*c2e18aaaSAndroid Build Coastguard Worker mock_getenv.side_effect = ['', default_root] 276*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(os.path.join(default_root, os.path.basename(root)), 277*c2e18aaaSAndroid Build Coastguard Worker common_util.get_android_out_dir()) 278*c2e18aaaSAndroid Build Coastguard Worker mock_getenv.side_effect = [android_out_root, default_root] 279*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(android_out_root, common_util.get_android_out_dir()) 280*c2e18aaaSAndroid Build Coastguard Worker 281*c2e18aaaSAndroid Build Coastguard Worker def test_has_build_target(self): 282*c2e18aaaSAndroid Build Coastguard Worker """Test has_build_target handling.""" 283*c2e18aaaSAndroid Build Coastguard Worker mod_info = mock.MagicMock() 284*c2e18aaaSAndroid Build Coastguard Worker mod_info.path_to_module_info = {'a/b/c': {}} 285*c2e18aaaSAndroid Build Coastguard Worker rel_path = 'a/b' 286*c2e18aaaSAndroid Build Coastguard Worker self.assertTrue(common_util.has_build_target(mod_info, rel_path)) 287*c2e18aaaSAndroid Build Coastguard Worker rel_path = 'd/e' 288*c2e18aaaSAndroid Build Coastguard Worker self.assertFalse(common_util.has_build_target(mod_info, rel_path)) 289*c2e18aaaSAndroid Build Coastguard Worker 290*c2e18aaaSAndroid Build Coastguard Worker @mock.patch('os.path.expanduser') 291*c2e18aaaSAndroid Build Coastguard Worker def test_remove_user_home_path(self, mock_expanduser): 292*c2e18aaaSAndroid Build Coastguard Worker """ Test replace the user home path to a constant string.""" 293*c2e18aaaSAndroid Build Coastguard Worker mock_expanduser.return_value = '/usr/home/a' 294*c2e18aaaSAndroid Build Coastguard Worker test_string = '/usr/home/a/test/dir' 295*c2e18aaaSAndroid Build Coastguard Worker expected_string = '$USER_HOME$/test/dir' 296*c2e18aaaSAndroid Build Coastguard Worker result_path = common_util.remove_user_home_path(test_string) 297*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(result_path, expected_string) 298*c2e18aaaSAndroid Build Coastguard Worker 299*c2e18aaaSAndroid Build Coastguard Worker def test_io_error_handle(self): 300*c2e18aaaSAndroid Build Coastguard Worker """Test io_error_handle handling.""" 301*c2e18aaaSAndroid Build Coastguard Worker err = "It's an IO error." 302*c2e18aaaSAndroid Build Coastguard Worker 303*c2e18aaaSAndroid Build Coastguard Worker def some_io_error_func(): 304*c2e18aaaSAndroid Build Coastguard Worker raise IOError(err) 305*c2e18aaaSAndroid Build Coastguard Worker with self.assertRaises(IOError) as context: 306*c2e18aaaSAndroid Build Coastguard Worker decorator = common_util.io_error_handle(some_io_error_func) 307*c2e18aaaSAndroid Build Coastguard Worker decorator() 308*c2e18aaaSAndroid Build Coastguard Worker self.assertTrue(err in context.exception) 309*c2e18aaaSAndroid Build Coastguard Worker 310*c2e18aaaSAndroid Build Coastguard Worker @mock.patch.object(common_util, '_show_env_setup_msg_and_exit') 311*c2e18aaaSAndroid Build Coastguard Worker @mock.patch('os.environ.get') 312*c2e18aaaSAndroid Build Coastguard Worker def test_get_android_root_dir(self, mock_get_env, mock_show_msg): 313*c2e18aaaSAndroid Build Coastguard Worker """Test get_android_root_dir handling.""" 314*c2e18aaaSAndroid Build Coastguard Worker root = 'root' 315*c2e18aaaSAndroid Build Coastguard Worker mock_get_env.return_value = root 316*c2e18aaaSAndroid Build Coastguard Worker expected = common_util.get_android_root_dir() 317*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(root, expected) 318*c2e18aaaSAndroid Build Coastguard Worker root = '' 319*c2e18aaaSAndroid Build Coastguard Worker mock_get_env.return_value = root 320*c2e18aaaSAndroid Build Coastguard Worker common_util.get_android_root_dir() 321*c2e18aaaSAndroid Build Coastguard Worker self.assertTrue(mock_show_msg.called) 322*c2e18aaaSAndroid Build Coastguard Worker 323*c2e18aaaSAndroid Build Coastguard Worker # pylint: disable=no-value-for-parameter 324*c2e18aaaSAndroid Build Coastguard Worker def test_check_args(self): 325*c2e18aaaSAndroid Build Coastguard Worker """Test check_args handling.""" 326*c2e18aaaSAndroid Build Coastguard Worker with self.assertRaises(TypeError): 327*c2e18aaaSAndroid Build Coastguard Worker decorator = common_util.check_args(name=str, text=str) 328*c2e18aaaSAndroid Build Coastguard Worker decorator(parse_rule(None, 'text')) 329*c2e18aaaSAndroid Build Coastguard Worker with self.assertRaises(TypeError): 330*c2e18aaaSAndroid Build Coastguard Worker decorator = common_util.check_args(name=str, text=str) 331*c2e18aaaSAndroid Build Coastguard Worker decorator(parse_rule('Paul', '')) 332*c2e18aaaSAndroid Build Coastguard Worker with self.assertRaises(TypeError): 333*c2e18aaaSAndroid Build Coastguard Worker decorator = common_util.check_args(name=str, text=str) 334*c2e18aaaSAndroid Build Coastguard Worker decorator(parse_rule(1, 2)) 335*c2e18aaaSAndroid Build Coastguard Worker 336*c2e18aaaSAndroid Build Coastguard Worker @mock.patch.object(common_util, 'get_blueprint_json_path') 337*c2e18aaaSAndroid Build Coastguard Worker @mock.patch.object(common_util, 'get_android_out_dir') 338*c2e18aaaSAndroid Build Coastguard Worker @mock.patch.object(common_util, 'get_android_root_dir') 339*c2e18aaaSAndroid Build Coastguard Worker def test_get_blueprint_json_files_relative_dict( 340*c2e18aaaSAndroid Build Coastguard Worker self, mock_get_root, mock_get_out, mock_get_path): 341*c2e18aaaSAndroid Build Coastguard Worker """Test get_blueprint_json_files_relative_dict function,""" 342*c2e18aaaSAndroid Build Coastguard Worker mock_get_root.return_value = 'a/b' 343*c2e18aaaSAndroid Build Coastguard Worker mock_get_out.return_value = 'out' 344*c2e18aaaSAndroid Build Coastguard Worker mock_get_path.return_value = 'out/soong/bp_java_file' 345*c2e18aaaSAndroid Build Coastguard Worker path_compdb = os.path.join('a/b', 'out', 'soong', 346*c2e18aaaSAndroid Build Coastguard Worker constant.RELATIVE_COMPDB_PATH, 347*c2e18aaaSAndroid Build Coastguard Worker constant.COMPDB_JSONFILE_NAME) 348*c2e18aaaSAndroid Build Coastguard Worker data = { 349*c2e18aaaSAndroid Build Coastguard Worker constant.GEN_JAVA_DEPS: 'a/b/out/soong/bp_java_file', 350*c2e18aaaSAndroid Build Coastguard Worker constant.GEN_CC_DEPS: 'a/b/out/soong/bp_java_file', 351*c2e18aaaSAndroid Build Coastguard Worker constant.GEN_COMPDB: path_compdb, 352*c2e18aaaSAndroid Build Coastguard Worker constant.GEN_RUST: 'a/b/out/soong/bp_java_file' 353*c2e18aaaSAndroid Build Coastguard Worker } 354*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual( 355*c2e18aaaSAndroid Build Coastguard Worker data, common_util.get_blueprint_json_files_relative_dict()) 356*c2e18aaaSAndroid Build Coastguard Worker 357*c2e18aaaSAndroid Build Coastguard Worker @mock.patch('os.environ.get') 358*c2e18aaaSAndroid Build Coastguard Worker def test_get_lunch_target(self, mock_get_env): 359*c2e18aaaSAndroid Build Coastguard Worker """Test get_lunch_target.""" 360*c2e18aaaSAndroid Build Coastguard Worker mock_get_env.return_value = "test" 361*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual( 362*c2e18aaaSAndroid Build Coastguard Worker common_util.get_lunch_target(), '{"lunch target": "test-test"}') 363*c2e18aaaSAndroid Build Coastguard Worker 364*c2e18aaaSAndroid Build Coastguard Worker def test_to_pretty_xml(self): 365*c2e18aaaSAndroid Build Coastguard Worker """Test to_pretty_xml.""" 366*c2e18aaaSAndroid Build Coastguard Worker root = ElementTree.fromstring(self._TEST_XML_CONTENT) 367*c2e18aaaSAndroid Build Coastguard Worker pretty_xml = common_util.to_pretty_xml(root) 368*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(pretty_xml, self._SAMPLE_XML_CONTENT) 369*c2e18aaaSAndroid Build Coastguard Worker 370*c2e18aaaSAndroid Build Coastguard Worker def test_to_to_boolean(self): 371*c2e18aaaSAndroid Build Coastguard Worker """Test to_boolean function with conditions.""" 372*c2e18aaaSAndroid Build Coastguard Worker self.assertTrue(common_util.to_boolean('True')) 373*c2e18aaaSAndroid Build Coastguard Worker self.assertTrue(common_util.to_boolean('true')) 374*c2e18aaaSAndroid Build Coastguard Worker self.assertTrue(common_util.to_boolean('T')) 375*c2e18aaaSAndroid Build Coastguard Worker self.assertTrue(common_util.to_boolean('t')) 376*c2e18aaaSAndroid Build Coastguard Worker self.assertTrue(common_util.to_boolean('1')) 377*c2e18aaaSAndroid Build Coastguard Worker self.assertFalse(common_util.to_boolean('False')) 378*c2e18aaaSAndroid Build Coastguard Worker self.assertFalse(common_util.to_boolean('false')) 379*c2e18aaaSAndroid Build Coastguard Worker self.assertFalse(common_util.to_boolean('F')) 380*c2e18aaaSAndroid Build Coastguard Worker self.assertFalse(common_util.to_boolean('f')) 381*c2e18aaaSAndroid Build Coastguard Worker self.assertFalse(common_util.to_boolean('0')) 382*c2e18aaaSAndroid Build Coastguard Worker self.assertFalse(common_util.to_boolean('')) 383*c2e18aaaSAndroid Build Coastguard Worker 384*c2e18aaaSAndroid Build Coastguard Worker @mock.patch.object(os.path, 'exists') 385*c2e18aaaSAndroid Build Coastguard Worker @mock.patch.object(common_util, 'get_android_root_dir') 386*c2e18aaaSAndroid Build Coastguard Worker def test_find_git_root(self, mock_get_root, mock_exist): 387*c2e18aaaSAndroid Build Coastguard Worker """Test find_git_root.""" 388*c2e18aaaSAndroid Build Coastguard Worker mock_get_root.return_value = '/a/b' 389*c2e18aaaSAndroid Build Coastguard Worker mock_exist.return_value = True 390*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(common_util.find_git_root('c/d'), '/a/b/c/d') 391*c2e18aaaSAndroid Build Coastguard Worker mock_exist.return_value = False 392*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(common_util.find_git_root('c/d'), None) 393*c2e18aaaSAndroid Build Coastguard Worker 394*c2e18aaaSAndroid Build Coastguard Worker def test_determine_language_ide(self): 395*c2e18aaaSAndroid Build Coastguard Worker """Test determine_language_ide function.""" 396*c2e18aaaSAndroid Build Coastguard Worker ide = 'u' 397*c2e18aaaSAndroid Build Coastguard Worker lang = 'u' 398*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual((constant.JAVA, constant.IDE_INTELLIJ), 399*c2e18aaaSAndroid Build Coastguard Worker common_util.determine_language_ide(lang, ide)) 400*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual((constant.JAVA, constant.IDE_INTELLIJ), 401*c2e18aaaSAndroid Build Coastguard Worker common_util.determine_language_ide( 402*c2e18aaaSAndroid Build Coastguard Worker lang, ide, ['some_module'])) 403*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual((constant.C_CPP, constant.IDE_CLION), 404*c2e18aaaSAndroid Build Coastguard Worker common_util.determine_language_ide( 405*c2e18aaaSAndroid Build Coastguard Worker lang, ide, None, ['some_module'])) 406*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual((constant.RUST, constant.IDE_VSCODE), 407*c2e18aaaSAndroid Build Coastguard Worker common_util.determine_language_ide( 408*c2e18aaaSAndroid Build Coastguard Worker lang, ide, None, None, ['some_module'])) 409*c2e18aaaSAndroid Build Coastguard Worker lang = 'j' 410*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual((constant.JAVA, constant.IDE_INTELLIJ), 411*c2e18aaaSAndroid Build Coastguard Worker common_util.determine_language_ide(lang, ide)) 412*c2e18aaaSAndroid Build Coastguard Worker ide = 'c' 413*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual((constant.C_CPP, constant.IDE_CLION), 414*c2e18aaaSAndroid Build Coastguard Worker common_util.determine_language_ide(lang, ide)) 415*c2e18aaaSAndroid Build Coastguard Worker ide = 'j' 416*c2e18aaaSAndroid Build Coastguard Worker lang = 'u' 417*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual((constant.JAVA, constant.IDE_INTELLIJ), 418*c2e18aaaSAndroid Build Coastguard Worker common_util.determine_language_ide(lang, ide)) 419*c2e18aaaSAndroid Build Coastguard Worker lang = 'j' 420*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual((constant.JAVA, constant.IDE_INTELLIJ), 421*c2e18aaaSAndroid Build Coastguard Worker common_util.determine_language_ide(lang, ide)) 422*c2e18aaaSAndroid Build Coastguard Worker ide = 'c' 423*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual((constant.C_CPP, constant.IDE_CLION), 424*c2e18aaaSAndroid Build Coastguard Worker common_util.determine_language_ide(lang, ide)) 425*c2e18aaaSAndroid Build Coastguard Worker lang = 'c' 426*c2e18aaaSAndroid Build Coastguard Worker ide = 'u' 427*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual((constant.C_CPP, constant.IDE_CLION), 428*c2e18aaaSAndroid Build Coastguard Worker common_util.determine_language_ide(lang, ide)) 429*c2e18aaaSAndroid Build Coastguard Worker ide = 'j' 430*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual((constant.JAVA, constant.IDE_INTELLIJ), 431*c2e18aaaSAndroid Build Coastguard Worker common_util.determine_language_ide(lang, ide)) 432*c2e18aaaSAndroid Build Coastguard Worker 433*c2e18aaaSAndroid Build Coastguard Worker @mock.patch('zipfile.ZipFile.extractall') 434*c2e18aaaSAndroid Build Coastguard Worker @mock.patch('zipfile.ZipFile') 435*c2e18aaaSAndroid Build Coastguard Worker def test_unzip_file(self, mock_zipfile, mock_extract): 436*c2e18aaaSAndroid Build Coastguard Worker """Test unzip_file function.""" 437*c2e18aaaSAndroid Build Coastguard Worker src = 'a/b/c.zip' 438*c2e18aaaSAndroid Build Coastguard Worker dest = 'a/b/d' 439*c2e18aaaSAndroid Build Coastguard Worker common_util.unzip_file(src, dest) 440*c2e18aaaSAndroid Build Coastguard Worker mock_zipfile.assert_called_with(src, 'r') 441*c2e18aaaSAndroid Build Coastguard Worker self.assertFalse(mock_extract.called) 442*c2e18aaaSAndroid Build Coastguard Worker 443*c2e18aaaSAndroid Build Coastguard Worker @mock.patch('os.walk') 444*c2e18aaaSAndroid Build Coastguard Worker def test_check_java_or_kotlin_file_exists(self, mock_walk): 445*c2e18aaaSAndroid Build Coastguard Worker """Test check_java_or_kotlin_file_exists with conditions.""" 446*c2e18aaaSAndroid Build Coastguard Worker root_dir = 'a/path/to/dir' 447*c2e18aaaSAndroid Build Coastguard Worker folder = 'path/to/dir' 448*c2e18aaaSAndroid Build Coastguard Worker target = 'test.java' 449*c2e18aaaSAndroid Build Coastguard Worker abs_path = os.path.join(root_dir, folder) 450*c2e18aaaSAndroid Build Coastguard Worker mock_walk.return_value = [(root_dir, [folder], [target])] 451*c2e18aaaSAndroid Build Coastguard Worker self.assertTrue(common_util.check_java_or_kotlin_file_exists(abs_path)) 452*c2e18aaaSAndroid Build Coastguard Worker target = 'test.kt' 453*c2e18aaaSAndroid Build Coastguard Worker abs_path = os.path.join(root_dir, folder) 454*c2e18aaaSAndroid Build Coastguard Worker mock_walk.return_value = [(root_dir, [folder], [target])] 455*c2e18aaaSAndroid Build Coastguard Worker self.assertTrue(common_util.check_java_or_kotlin_file_exists(abs_path)) 456*c2e18aaaSAndroid Build Coastguard Worker target = 'test.cpp' 457*c2e18aaaSAndroid Build Coastguard Worker mock_walk.return_value = [(root_dir, [folder], [target])] 458*c2e18aaaSAndroid Build Coastguard Worker self.assertFalse(common_util.check_java_or_kotlin_file_exists(abs_path)) 459*c2e18aaaSAndroid Build Coastguard Worker 460*c2e18aaaSAndroid Build Coastguard Worker # Only VS Code IDE supports Rust projects right now. 461*c2e18aaaSAndroid Build Coastguard Worker lang = 'r' 462*c2e18aaaSAndroid Build Coastguard Worker ide = 'u' 463*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual((constant.RUST, constant.IDE_VSCODE), 464*c2e18aaaSAndroid Build Coastguard Worker common_util.determine_language_ide(lang, ide)) 465*c2e18aaaSAndroid Build Coastguard Worker lang = 'r' 466*c2e18aaaSAndroid Build Coastguard Worker ide = 'v' 467*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual((constant.RUST, constant.IDE_VSCODE), 468*c2e18aaaSAndroid Build Coastguard Worker common_util.determine_language_ide(lang, ide)) 469*c2e18aaaSAndroid Build Coastguard Worker lang = 'r' 470*c2e18aaaSAndroid Build Coastguard Worker ide = 'j' 471*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual((constant.RUST, constant.IDE_VSCODE), 472*c2e18aaaSAndroid Build Coastguard Worker common_util.determine_language_ide(lang, ide)) 473*c2e18aaaSAndroid Build Coastguard Worker lang = 'r' 474*c2e18aaaSAndroid Build Coastguard Worker ide = 'c' 475*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual((constant.RUST, constant.IDE_VSCODE), 476*c2e18aaaSAndroid Build Coastguard Worker common_util.determine_language_ide(lang, ide)) 477*c2e18aaaSAndroid Build Coastguard Worker 478*c2e18aaaSAndroid Build Coastguard Worker 479*c2e18aaaSAndroid Build Coastguard Workerdef parse_rule(self, name, text): 480*c2e18aaaSAndroid Build Coastguard Worker """A test function for test_check_args.""" 481*c2e18aaaSAndroid Build Coastguard Worker 482*c2e18aaaSAndroid Build Coastguard Worker 483*c2e18aaaSAndroid Build Coastguard Workerif __name__ == '__main__': 484*c2e18aaaSAndroid Build Coastguard Worker unittest.main() 485