lin
2025-07-31 065ea569db06206874bbfa18eb25ff6121aec09b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env python3
#
# Copyright 2019, The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
 
"""Unittests for aidegen_main."""
 
from __future__ import print_function
 
import os
import unittest
from unittest import mock
 
import aidegen.unittest_constants as uc
from aidegen import aidegen_main
from aidegen.lib import aidegen_metrics
from aidegen import constant
from aidegen.lib import common_util
from aidegen.lib.common_util import COLORED_INFO
from aidegen.lib.errors import IDENotExistError
from aidegen.lib.errors import ProjectPathNotExistError
from aidegen.lib.ide_util import IdeUtil
from atest import module_info
 
 
# pylint: disable=protected-access
# pylint: disable=invalid-name
class AidegenMainUnittests(unittest.TestCase):
    """Unit tests for aidegen_main.py"""
 
    def test_parse_args(self):
        """Test _parse_args with different conditions."""
        args = aidegen_main._parse_args([])
        self.assertEqual(args.targets, [''])
        self.assertEqual(args.ide[0], 'j')
        target = 'tradefed'
        args = aidegen_main._parse_args([target])
        self.assertEqual(args.targets, [target])
        depth = '2'
        args = aidegen_main._parse_args(['-d', depth])
        self.assertEqual(args.depth, int(depth))
        args = aidegen_main._parse_args(['-v'])
        self.assertEqual(args.verbose, True)
        args = aidegen_main._parse_args(['-v'])
        self.assertEqual(args.verbose, True)
        args = aidegen_main._parse_args(['-i', 's'])
        self.assertEqual(args.ide[0], 's')
        args = aidegen_main._parse_args(['-i', 'e'])
        self.assertEqual(args.ide[0], 'e')
        args = aidegen_main._parse_args(['-p', uc.TEST_MODULE])
        self.assertEqual(args.ide_installed_path, uc.TEST_MODULE)
        args = aidegen_main._parse_args(['-n'])
        self.assertEqual(args.no_launch, True)
        args = aidegen_main._parse_args(['-r'])
        self.assertEqual(args.config_reset, True)
        args = aidegen_main._parse_args(['-s'])
        self.assertEqual(args.skip_build, True)
 
    @mock.patch('aidegen_main.logging.basicConfig')
    def test_configure_logging(self, mock_log_config):
        """Test _configure_logging with different arguments."""
        aidegen_main._configure_logging(True)
        log_format = aidegen_main._LOG_FORMAT
        datefmt = aidegen_main._DATE_FORMAT
        level = aidegen_main.logging.DEBUG
        self.assertTrue(
            mock_log_config.called_with(
                level=level, format=log_format, datefmt=datefmt))
        aidegen_main._configure_logging(False)
        level = aidegen_main.logging.INFO
        self.assertTrue(
            mock_log_config.called_with(
                level=level, format=log_format, datefmt=datefmt))
 
    @mock.patch.object(IdeUtil, 'is_ide_installed')
    def test_get_ide_util_instance(self, mock_installed):
        """Test _get_ide_util_instance with different conditions."""
        target = 'tradefed'
        args = aidegen_main._parse_args([target, '-n'])
        self.assertEqual(aidegen_main._get_ide_util_instance(args), None)
        args = aidegen_main._parse_args([target])
        self.assertIsInstance(
            aidegen_main._get_ide_util_instance(args), IdeUtil)
        mock_installed.return_value = False
        with self.assertRaises(IDENotExistError):
            aidegen_main._get_ide_util_instance(args)
 
    @mock.patch('builtins.print')
    def test_check_skip_build(self, mock_print):
        """Test _check_skip_build with different conditions."""
        target = 'tradefed'
        args = aidegen_main._parse_args([target, '-s'])
        aidegen_main._check_skip_build(args)
        self.assertFalse(mock_print.called)
        args = aidegen_main._parse_args([target])
        aidegen_main._check_skip_build(args)
        msg = aidegen_main._SKIP_BUILD_INFO.format(
            COLORED_INFO(
                aidegen_main._SKIP_BUILD_CMD.format(' '.join(args.targets))))
        info = '\n{} {}\n'.format(aidegen_main._INFO, msg)
        self.assertTrue(mock_print.called_with(info))
 
    @mock.patch.object(aidegen_main, 'generate_ide_project_files')
    @mock.patch.object(aidegen_main, 'generate_eclipse_project_files')
    def test_generate_project_files(self, mock_eclipse, mock_ide):
        """Test _generate_project_files with different conditions."""
        projects = ['module_a', 'module_v']
        aidegen_main._generate_project_files('e', projects)
        self.assertTrue(mock_eclipse.called_with(projects))
        aidegen_main._generate_project_files('s', projects)
        self.assertTrue(mock_ide.called_with(projects))
        aidegen_main._generate_project_files('j', projects)
        self.assertTrue(mock_ide.called_with(projects))
 
    @mock.patch.object(common_util, 'get_atest_module_info')
    @mock.patch.object(aidegen_metrics, 'starts_asuite_metrics')
    def test_show_collect_data_notice(self, mock_metrics, mock_get):
        """Test main process always run through the target test function."""
        target = 'nothing'
        args = aidegen_main._parse_args([target, '-s', '-n'])
        with self.assertRaises(ProjectPathNotExistError):
            err = common_util.PATH_NOT_EXISTS_ERROR.format(target)
            mock_get.side_effect = ProjectPathNotExistError(err)
            aidegen_main.main_without_message(args)
            self.assertTrue(mock_metrics.called)
 
    @mock.patch.object(common_util, 'get_related_paths')
    def test_compile_targets_for_whole_android_tree(self, mock_get):
        """Test _add_whole_android_tree_project with different conditions."""
        mod_info = module_info.ModuleInfo()
        targets = ['']
        cwd = constant.ANDROID_ROOT_PATH
        self.assertEqual(
            targets,
            aidegen_main._compile_targets_for_whole_android_tree(
                mod_info, targets, cwd))
        base_dir = 'frameworks/base'
        expected_targets = ['', base_dir]
        cwd = os.path.join(constant.ANDROID_ROOT_PATH, base_dir)
        mock_get.return_value = None, cwd
        self.assertEqual(
            expected_targets,
            aidegen_main._compile_targets_for_whole_android_tree(
                mod_info, targets, cwd))
        targets = [base_dir]
        cwd = constant.ANDROID_ROOT_PATH
        self.assertEqual(
            expected_targets,
            aidegen_main._compile_targets_for_whole_android_tree(
                mod_info, targets, cwd))
 
 
if __name__ == '__main__':
    unittest.main()