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        folk_new_process = False
117        test_result = ' '.join([
118            constant.NOHUP, test_script_path, test_project_path,
119            constant.IGNORE_STD_OUT_ERR_CMD, ''
120        ])
121        self.assertEqual(test_result, ide_common_util.get_run_ide_cmd(
122            test_script_path, test_project_path, folk_new_process))
123
124    @mock.patch('builtins.sorted')
125    @mock.patch('glob.glob')
126    def test_get_scripts_from_file_path(self, mock_list, mock_sort):
127        """Test _get_scripts_from_file_path."""
128        test_file = 'a/b/c/d.e'
129        ide_common_util._get_scripts_from_file_path(test_file, 'd.e')
130        mock_list.return_value = [test_file]
131        self.assertTrue(mock_sort.called)
132        mock_list.return_value = None
133        self.assertEqual(ide_common_util._get_scripts_from_file_path(
134            test_file, 'd.e'), None)
135
136    @mock.patch('builtins.sorted')
137    @mock.patch('builtins.list')
138    def test_get_scripts_from_dir_path(self, mock_list, mock_sort):
139        """Test get_scripts_from_dir_path."""
140        test_path = 'a/b/c/d.e'
141        mock_list.return_value = ['a', 'b', 'c']
142        ide_common_util.get_scripts_from_dir_path(test_path, 'd.e')
143        self.assertTrue(mock_sort.called)
144        mock_list.return_value = []
145        self.assertEqual(ide_common_util.get_scripts_from_dir_path(
146            test_path, 'd.e'), None)
147
148
149if __name__ == '__main__':
150    unittest.main()
151