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 18"""Unittests for ide_common_util.""" 19 20import os 21import unittest 22 23from unittest import mock 24 25from aidegen import aidegen_main 26from aidegen import constant 27from aidegen import unittest_constants 28from aidegen.lib import ide_common_util 29from aidegen.lib import ide_util 30from aidegen.lib import project_config 31 32 33# pylint: disable=protected-access 34class IdeUtilCommonUnittests(unittest.TestCase): 35 """Unit tests for ide_util.py.""" 36 37 _TEST_PRJ_PATH1 = '' 38 _TEST_PRJ_PATH2 = '' 39 _TEST_PRJ_PATH3 = '' 40 _TEST_PRJ_PATH4 = '' 41 42 def setUp(self): 43 """Prepare the testdata related path.""" 44 IdeUtilCommonUnittests._TEST_PRJ_PATH1 = os.path.join( 45 unittest_constants.TEST_DATA_PATH, 'android_facet.iml') 46 IdeUtilCommonUnittests._TEST_PRJ_PATH2 = os.path.join( 47 unittest_constants.TEST_DATA_PATH, 'project/test.java') 48 test_path = unittest_constants.TEST_DATA_PATH 49 IdeUtilCommonUnittests._TEST_PRJ_PATH3 = test_path 50 IdeUtilCommonUnittests._TEST_PRJ_PATH4 = os.path.join( 51 unittest_constants.TEST_DATA_PATH, '.idea') 52 53 def tearDown(self): 54 """Clear the testdata related path.""" 55 IdeUtilCommonUnittests._TEST_PRJ_PATH1 = '' 56 IdeUtilCommonUnittests._TEST_PRJ_PATH2 = '' 57 IdeUtilCommonUnittests._TEST_PRJ_PATH3 = '' 58 IdeUtilCommonUnittests._TEST_PRJ_PATH4 = '' 59 60 def test_is_intellij_project(self): 61 """Test _is_intellij_project.""" 62 self.assertFalse( 63 ide_common_util.is_intellij_project( 64 IdeUtilCommonUnittests._TEST_PRJ_PATH2)) 65 self.assertTrue( 66 ide_common_util.is_intellij_project( 67 IdeUtilCommonUnittests._TEST_PRJ_PATH1)) 68 self.assertTrue( 69 ide_common_util.is_intellij_project( 70 IdeUtilCommonUnittests._TEST_PRJ_PATH3)) 71 self.assertFalse( 72 ide_common_util.is_intellij_project( 73 IdeUtilCommonUnittests._TEST_PRJ_PATH4)) 74 75 @mock.patch('glob.glob', return_value=unittest_constants.IDEA_SH_FIND_NONE) 76 def test_get_intellij_sh_none(self, mock_glob): 77 """Test with the cmd return none, test result should be None.""" 78 mock_glob.return_value = unittest_constants.IDEA_SH_FIND_NONE 79 args = aidegen_main._parse_args(['tradefed']) 80 project_config.ProjectConfig(args) 81 self.assertEqual( 82 None, 83 ide_common_util.get_intellij_version_path( 84 ide_util.IdeLinuxIntelliJ()._ls_ce_path)) 85 self.assertEqual( 86 None, 87 ide_common_util.get_intellij_version_path( 88 ide_util.IdeLinuxIntelliJ()._ls_ue_path)) 89 90 @mock.patch('builtins.input') 91 @mock.patch('glob.glob', return_value=unittest_constants.IDEA_SH_FIND) 92 def test_ask_preference(self, mock_glob, mock_input): 93 """Ask users' preference, the result should be equal to test data.""" 94 mock_glob.return_value = unittest_constants.IDEA_SH_FIND 95 mock_input.return_value = '1' 96 self.assertEqual( 97 ide_common_util.ask_preference(unittest_constants.IDEA_SH_FIND, 98 constant.IDE_INTELLIJ), 99 unittest_constants.IDEA_SH_FIND[0]) 100 mock_input.return_value = '2' 101 self.assertEqual( 102 ide_common_util.ask_preference(unittest_constants.IDEA_SH_FIND, 103 constant.IDE_INTELLIJ), 104 unittest_constants.IDEA_SH_FIND[1]) 105 106 def test_get_run_ide_cmd(self): 107 """Test get_run_ide_cmd.""" 108 test_script_path = 'a/b/c/d.sh' 109 test_project_path = 'xyz/.idea' 110 test_result = ' '.join([ 111 constant.NOHUP, test_script_path, test_project_path, 112 constant.IGNORE_STD_OUT_ERR_CMD 113 ]) 114 self.assertEqual(test_result, ide_common_util.get_run_ide_cmd( 115 test_script_path, test_project_path)) 116 117 @mock.patch('builtins.sorted') 118 @mock.patch('glob.glob') 119 def test_get_scripts_from_file_path(self, mock_list, mock_sort): 120 """Test _get_scripts_from_file_path.""" 121 test_file = 'a/b/c/d.e' 122 ide_common_util._get_scripts_from_file_path(test_file, 'd.e') 123 mock_list.return_value = [test_file] 124 self.assertTrue(mock_sort.called) 125 mock_list.return_value = None 126 self.assertEqual(ide_common_util._get_scripts_from_file_path( 127 test_file, 'd.e'), None) 128 129 @mock.patch('builtins.sorted') 130 @mock.patch('builtins.list') 131 def test_get_scripts_from_dir_path(self, mock_list, mock_sort): 132 """Test get_scripts_from_dir_path.""" 133 test_path = 'a/b/c/d.e' 134 mock_list.return_value = ['a', 'b', 'c'] 135 ide_common_util.get_scripts_from_dir_path(test_path, 'd.e') 136 self.assertTrue(mock_sort.called) 137 mock_list.return_value = [] 138 self.assertEqual(ide_common_util.get_scripts_from_dir_path( 139 test_path, 'd.e'), None) 140 141 142if __name__ == '__main__': 143 unittest.main() 144