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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/usr/bin/env python3
#
# Copyright 2018 - 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.
 
"""Functional test for aidegen project files."""
 
from __future__ import absolute_import
 
import argparse
import itertools
import json
import os
import sys
import xml.etree.ElementTree
import xml.parsers.expat
 
import aidegen.lib.errors
 
from aidegen import aidegen_main
from aidegen.lib.common_util import get_related_paths
from aidegen.lib.common_util import time_logged
from atest import constants
from atest import module_info
from atest import atest_utils
 
_ANDROID_ROOT_PATH = os.environ.get(constants.ANDROID_BUILD_TOP)
_ROOT_DIR = os.path.join(_ANDROID_ROOT_PATH,
                         'tools/asuite/aidegen_functional_test')
_TEST_DATA_PATH = os.path.join(_ROOT_DIR, 'test_data')
_ANDROID_SINGLE_PROJECT_JSON = os.path.join(_TEST_DATA_PATH,
                                            'single_module.json')
_VERIFY_COMMANDS_JSON = os.path.join(_TEST_DATA_PATH, 'verify_commands.json')
_PRODUCT_DIR = '$PROJECT_DIR$'
_ANDROID_COMMON = 'android_common'
_LINUX_GLIBC_COMMON = 'linux_glibc_common'
_SRCS = 'srcs'
_JARS = 'jars'
_URL = 'url'
_TEST_ERROR = ('AIDEGen functional test error: %s-%s is different.')
_MSG_NOT_IN_PROJECT_FILE = ('%s is expected, but not found in the created '
                            'project file: %s')
_MSG_NOT_IN_SAMPLE_DATA = ('%s is unexpected, but found in the created project '
                           'file: %s')
_TEST_IML_DICT = {
    'SystemUI': ['SystemUI.iml', 'dependencies-SystemUI.iml'],
    'tradefed': ['core.iml', 'dependencies-core.iml']
}
_ALL_PASS = 'All tests passed!'
 
 
def _parse_args(args):
    """Parse command line arguments.
 
    Args:
        args: A list of arguments.
 
    Returns:
        An argparse.Namespace class instance holding parsed args.
    """
    parser = argparse.ArgumentParser(
        description=__doc__,
        formatter_class=argparse.RawDescriptionHelpFormatter,
        usage='aidegen_functional_test [-c | -v]')
    group = parser.add_mutually_exclusive_group()
    parser.required = False
    group.add_argument(
        '-c',
        '--create-sample',
        action='store_true',
        dest='create_sample',
        help=('Create aidegen project files and write data to sample json file '
              'for aidegen_functional_test to compare.'))
    group.add_argument(
        '-v',
        '--verify',
        action='store_true',
        dest='verify_aidegen',
        help='Verify various use cases of executing aidegen.')
    return parser.parse_args(args)
 
 
def _import_project_file_xml_etree(filename):
    """Import iml project file and load data into a dictionary.
 
    Args:
        filename: The input project file name.
    """
    data = {}
    try:
        tree = xml.etree.ElementTree.parse(filename)
        data[_SRCS] = []
        root = tree.getroot()
        for element in root.iter('sourceFolder'):
            src = element.get(_URL).replace(_ANDROID_ROOT_PATH, _PRODUCT_DIR)
            data[_SRCS].append(src)
        data[_JARS] = []
        for element in root.iter('root'):
            jar = element.get(_URL).replace(_ANDROID_ROOT_PATH, _PRODUCT_DIR)
            data[_JARS].append(jar)
    except (EnvironmentError, ValueError, LookupError,
            xml.parsers.expat.ExpatError) as err:
        print("{0}: import error: {1}".format(os.path.basename(filename), err))
        raise
    return data
 
 
def _generate_sample_json():
    """Generate sample iml data and write into a json file."""
    atest_module_info = module_info.ModuleInfo()
    data = {}
    for target, filelist in _TEST_IML_DICT.items():
        aidegen_main.main([target, '-n'])
        _, abs_path = get_related_paths(atest_module_info, target)
        for filename in filelist:
            real_iml_file = os.path.join(abs_path, filename)
            item_name = os.path.basename(real_iml_file)
            data[item_name] = _import_project_file_xml_etree(real_iml_file)
    return data
 
 
def _create_sample_json_file():
    """Write samples' iml data into a json file.
 
    linked_function: _generate_sample_json()
    """
    data = _generate_sample_json()
    with open(_ANDROID_SINGLE_PROJECT_JSON, 'w') as outfile:
        json.dump(data, outfile, indent=4, sort_keys=False)
 
 
def test_some_sample_iml():
    """Compare sample iml data to assure project iml file contents is right."""
    test_successful = True
    with open(_ANDROID_SINGLE_PROJECT_JSON, 'r') as outfile:
        data_sample = json.load(outfile)
    data_real = _generate_sample_json()
    for name in data_real:
        for item in [_SRCS, _JARS]:
            s_items = data_sample[name][item]
            r_items = data_real[name][item]
            if set(s_items) != set(r_items):
                diff_iter = _compare_content(name, item, s_items, r_items)
                if diff_iter:
                    print('\n%s\n%s' % (atest_utils.colorize(
                        'Test error...', constants.RED), _TEST_ERROR %
                                        (name, item)))
                    print('%s %s contents are different:' % (name, item))
                    for diff in diff_iter:
                        print(diff)
                    test_successful = False
    if test_successful:
        print(atest_utils.colorize(_ALL_PASS, constants.GREEN))
 
 
def _compare_content(module_name, item_type, s_items, r_items):
    """Compare src or jar files' data of two dictionaries.
 
    Args:
        module_name: the test module name.
        item_type: the type is src or jar.
        s_items: sample jars' items.
        r_items: real jars' items.
 
    Returns:
        An iterator of not equal sentences after comparison.
    """
    if item_type == _SRCS:
        cmp_iter1 = _compare_srcs_content(module_name, s_items, r_items,
                                          _MSG_NOT_IN_PROJECT_FILE)
        cmp_iter2 = _compare_srcs_content(module_name, r_items, s_items,
                                          _MSG_NOT_IN_SAMPLE_DATA)
    else:
        cmp_iter1 = _compare_jars_content(module_name, s_items, r_items,
                                          _MSG_NOT_IN_PROJECT_FILE)
        cmp_iter2 = _compare_jars_content(module_name, r_items, s_items,
                                          _MSG_NOT_IN_SAMPLE_DATA)
    return itertools.chain(cmp_iter1, cmp_iter2)
 
 
def _compare_srcs_content(module_name, s_items, r_items, msg):
    """Compare src or jar files' data of two dictionaries.
 
    Args:
        module_name: the test module name.
        s_items: sample jars' items.
        r_items: real jars' items.
        msg: the message will be written into log file.
 
    Returns:
        An iterator of not equal sentences after comparison.
    """
    for sample in s_items:
        if not sample in r_items:
            yield msg % (sample, module_name)
 
 
def _compare_jars_content(module_name, s_items, r_items, msg):
    """Compare src or jar files' data of two dictionaries.
 
    Args:
        module_name: the test module name.
        s_items: sample jars' items.
        r_items: real jars' items.
        msg: the message will be written into log file.
 
    Returns:
        An iterator of not equal sentences after comparison.
    """
    for sample in s_items:
        if not sample in r_items:
            lnew = sample
            if _LINUX_GLIBC_COMMON in sample:
                lnew = sample.replace(_LINUX_GLIBC_COMMON, _ANDROID_COMMON)
            else:
                lnew = sample.replace(_ANDROID_COMMON, _LINUX_GLIBC_COMMON)
            if not lnew in r_items:
                yield msg % (sample, module_name)
 
 
# pylint: disable=broad-except
# pylint: disable=eval-used
@time_logged
def _verify_aidegen():
    """Verify various use cases of executing aidegen."""
    with open(_VERIFY_COMMANDS_JSON, 'r') as jsfile:
        data = json.load(jsfile)
    for use_case in data:
        for cmd in data[use_case]:
            try:
                eval(cmd)
            except (aidegen.lib.errors.ProjectOutsideAndroidRootError,
                    aidegen.lib.errors.ProjectPathNotExistError,
                    aidegen.lib.errors.NoModuleDefinedInModuleInfoError,
                    aidegen.lib.errors.IDENotExistError) as err:
                print('{} command has raise error: {}.'.format(use_case, err))
            except Exception as exp:
                print('{}.{} command {}.'.format(
                    use_case, cmd,
                    atest_utils.colorize('executes failed', constants.RED)))
                raise Exception(
                    'Unexpected command {} exception {}.'.format(use_case, exp))
        print('{} command {}!'.format(
            use_case, atest_utils.colorize('test passed', constants.GREEN)))
    print(atest_utils.colorize(_ALL_PASS, constants.GREEN))
 
 
def main(argv):
    """Main entry.
 
    Compare iml project files to the data recorded in single_module.json.
 
    Args:
        argv: A list of system arguments.
    """
    args = _parse_args(argv)
    if args.create_sample:
        _create_sample_json_file()
    elif args.verify_aidegen:
        _verify_aidegen()
    else:
        test_some_sample_iml()
 
 
if __name__ == '__main__':
    main(sys.argv[1:])