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 project_config."""
18
19from __future__ import print_function
20
21import os
22import unittest
23from unittest import mock
24
25from aidegen import aidegen_main
26from aidegen import constant
27from aidegen.lib import errors
28from aidegen import unittest_constants
29from aidegen.lib import project_config
30from aidegen.lib import common_util
31
32
33# pylint: disable=protected-access
34class AidegenProjectConfigUnittests(unittest.TestCase):
35    """Unit tests for project_config.py"""
36
37    @mock.patch.object(os, 'getcwd')
38    def test_is_whole_android_tree(self, mock_getcwd):
39        """Test _is_whole_android_tree with different conditions."""
40        self.assertTrue(project_config.is_whole_android_tree(['a'], True))
41        mock_getcwd.return_value = common_util.get_android_root_dir()
42        self.assertTrue(project_config.is_whole_android_tree([''], False))
43        self.assertFalse(project_config.is_whole_android_tree(['a'], False))
44
45    @mock.patch.object(common_util, 'is_android_root')
46    def test_check_whole_android_tree(self, mock_is_android_root):
47        """Test _check_whole_android_tree with different conditions."""
48        mock_is_android_root.return_value = True
49        exspected = [constant.WHOLE_ANDROID_TREE_TARGET]
50        self.assertEqual(
51            exspected, project_config._check_whole_android_tree([''], True))
52        self.assertEqual(
53            exspected, project_config._check_whole_android_tree([''], False))
54        exspected = constant.WHOLE_ANDROID_TREE_TARGET
55        self.assertEqual(
56            exspected, project_config._check_whole_android_tree(['a'], True)[0])
57        exspected = ['a']
58        self.assertEqual(
59            exspected, project_config._check_whole_android_tree(['a'], False))
60
61    def test_init_with_no_launch_ide(self):
62        """Test __init__ method without launching IDE."""
63        args = aidegen_main._parse_args(['a', '-n', '-s'])
64        config = project_config.ProjectConfig(args)
65        self.assertEqual(config.ide_name, constant.IDE_INTELLIJ)
66        self.assertFalse(config.is_launch_ide)
67        self.assertEqual(config.depth, 0)
68        self.assertFalse(config.full_repo)
69        self.assertTrue(config.is_skip_build)
70        self.assertEqual(config.targets, ['a'])
71        self.assertFalse(config.verbose)
72        self.assertEqual(config.ide_installed_path, None)
73        self.assertFalse(config.config_reset)
74        self.assertEqual(config.exclude_paths, None)
75        config_obj = project_config.ProjectConfig.get_instance()
76        self.assertEqual(config_obj.ide_name, constant.IDE_INTELLIJ)
77        self.assertFalse(config_obj.is_launch_ide)
78        self.assertEqual(config_obj.depth, 0)
79        self.assertFalse(config_obj.full_repo)
80        self.assertTrue(config_obj.is_skip_build)
81        self.assertEqual(config_obj.targets, ['a'])
82        self.assertFalse(config_obj.verbose)
83        self.assertEqual(config_obj.ide_installed_path, None)
84        self.assertFalse(config_obj.config_reset)
85        self.assertEqual(config.exclude_paths, None)
86
87    def test_init_with_diff_arguments(self):
88        """Test __init__ method with different arguments."""
89        args = aidegen_main._parse_args([])
90        config = project_config.ProjectConfig(args)
91        self.assertEqual(config.ide_name, constant.IDE_INTELLIJ)
92        self.assertEqual(config.targets, [''])
93        config_obj = project_config.ProjectConfig.get_instance()
94        self.assertEqual(config_obj.targets, [''])
95        self.assertEqual(config_obj.ide_name, constant.IDE_INTELLIJ)
96        target = 'tradefed'
97        args = aidegen_main._parse_args([target])
98        config = project_config.ProjectConfig(args)
99        self.assertEqual(config.targets, [target])
100        config_obj = project_config.ProjectConfig.get_instance()
101        self.assertEqual(config_obj.targets, [target])
102        depth = '2'
103        args = aidegen_main._parse_args(['-d', depth])
104        config = project_config.ProjectConfig(args)
105        self.assertEqual(config.depth, int(depth))
106        config_obj = project_config.ProjectConfig.get_instance()
107        self.assertEqual(config_obj.depth, int(depth))
108
109    def test_init_with_other_arguments(self):
110        """Test __init__ method with other arguments."""
111        args = aidegen_main._parse_args(['-v'])
112        config = project_config.ProjectConfig(args)
113        self.assertTrue(config.verbose)
114        config_obj = project_config.ProjectConfig.get_instance()
115        self.assertTrue(config_obj.verbose)
116        args = aidegen_main._parse_args(['-i', 's'])
117        config = project_config.ProjectConfig(args)
118        self.assertEqual(config.ide_name, constant.IDE_ANDROID_STUDIO)
119        config_obj = project_config.ProjectConfig.get_instance()
120        self.assertEqual(config_obj.ide_name, constant.IDE_ANDROID_STUDIO)
121        args = aidegen_main._parse_args(['-i', 'e'])
122        config = project_config.ProjectConfig(args)
123        self.assertEqual(config.ide_name, constant.IDE_ECLIPSE)
124        config_obj = project_config.ProjectConfig.get_instance()
125        self.assertEqual(config_obj.ide_name, constant.IDE_ECLIPSE)
126        args = aidegen_main._parse_args(['-p', unittest_constants.TEST_MODULE])
127        config = project_config.ProjectConfig(args)
128        self.assertEqual(
129            config.ide_installed_path, unittest_constants.TEST_MODULE)
130        config_obj = project_config.ProjectConfig.get_instance()
131        self.assertEqual(
132            config_obj.ide_installed_path, unittest_constants.TEST_MODULE)
133        args = aidegen_main._parse_args(['-n'])
134        config = project_config.ProjectConfig(args)
135        self.assertFalse(config.is_launch_ide)
136        config_obj = project_config.ProjectConfig.get_instance()
137        self.assertFalse(config_obj.is_launch_ide)
138        args = aidegen_main._parse_args(['-r'])
139        config = project_config.ProjectConfig(args)
140        self.assertTrue(config.config_reset)
141        config_obj = project_config.ProjectConfig.get_instance()
142        self.assertTrue(config_obj.config_reset)
143        args = aidegen_main._parse_args(['-s'])
144        config = project_config.ProjectConfig(args)
145        self.assertTrue(config.is_skip_build)
146        config_obj = project_config.ProjectConfig.get_instance()
147        self.assertTrue(config_obj.is_skip_build)
148
149    @mock.patch.object(common_util, 'get_related_paths')
150    def test_transform_exclusive_paths(self, mock_get_rel):
151        """Test _transform_exclusive_paths with conditions."""
152        result = project_config._transform_exclusive_paths(mock.Mock(), [])
153        self.assertFalse(mock_get_rel.called)
154        self.assertEqual(None, result)
155        rel_path = 'to/path'
156        abs_path = 'a/b/to/path'
157        mock_get_rel.reset_mock()
158        mock_get_rel.return_value = rel_path, abs_path
159        result = project_config._transform_exclusive_paths(
160            mock.Mock(), [abs_path])
161        self.assertEqual([rel_path], result)
162
163    @mock.patch.object(project_config, '_check_whole_android_tree')
164    @mock.patch.object(project_config, '_transform_exclusive_paths')
165    @mock.patch.object(common_util, 'get_atest_module_info')
166    @mock.patch.object(project_config.ProjectConfig, '_show_skip_build_msg')
167    def test_init_environment(self, mock_show_skip, mock_get_atest, mock_trans,
168                              mock_check_whole):
169        """Test init_environment method."""
170        args = aidegen_main._parse_args(['-v'])
171        config = project_config.ProjectConfig(args)
172        config.init_environment()
173        self.assertTrue(mock_show_skip.called)
174        self.assertTrue(mock_get_atest.called)
175        self.assertTrue(mock_trans.called)
176        self.assertTrue(mock_check_whole.called)
177        mock_get_atest.mock_reset()
178        args = aidegen_main._parse_args(['-i', 'c'])
179        config = project_config.ProjectConfig(args)
180        config.init_environment()
181        mock_get_atest.assert_called_with(None)
182
183    @mock.patch('builtins.print')
184    def test_show_skip_build_msg_with_skip(self, mock_print):
185        """Test _show_skip_build_msg method with skip build."""
186        args = aidegen_main._parse_args(['-s'])
187        config = project_config.ProjectConfig(args)
188        config._show_skip_build_msg()
189        self.assertTrue(mock_print.called_with(
190            '\n{} {}\n'.format(common_util.COLORED_INFO('Warning:'),
191                               project_config._SKIP_BUILD_WARN)))
192
193    @mock.patch('builtins.print')
194    def test_show_skip_build_msg_without_skip(self, mock_print):
195        """Test _show_skip_build_msg method without skip build."""
196        targets = ['Settings']
197        args = aidegen_main._parse_args(targets)
198        config = project_config.ProjectConfig(args)
199        config._show_skip_build_msg()
200        msg = project_config.SKIP_BUILD_INFO.format(common_util.COLORED_INFO(
201            project_config._SKIP_BUILD_CMD.format(' '.join(config.targets))))
202        self.assertTrue(mock_print.called_with(
203            '\n{} {}\n'.format(common_util.COLORED_INFO('INFO:'), msg)))
204
205    def test_get_instance_without_instance(self):
206        """Test get_instance method without initialize an instance."""
207        project_config.ProjectConfig._instance = None
208        with self.assertRaises(errors.InstanceNotExistError):
209            project_config.ProjectConfig.get_instance()
210
211
212if __name__ == '__main__':
213    unittest.main()
214