• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2#
3# Copyright 2020, 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 IML class."""
18
19import os
20import shutil
21import tempfile
22import unittest
23from unittest import mock
24
25from aidegen import templates
26from aidegen.lib import common_util
27from aidegen.idea import iml
28from atest import module_info
29
30
31# pylint: disable=protected-access
32class IMLGenUnittests(unittest.TestCase):
33    """Unit tests for IMLGenerator class."""
34
35    _TEST_DIR = None
36
37    def setUp(self):
38        """Prepare the testdata related path."""
39        IMLGenUnittests._TEST_DIR = tempfile.mkdtemp()
40        module = {
41            'module_name': 'test',
42            'iml_name': 'test_iml',
43            'path': ['a/b'],
44            'srcs': ['a/b/src'],
45            'tests': ['a/b/tests'],
46            'srcjars': ['x/y.srcjar'],
47            'jars': ['s.jar'],
48            'dependencies': ['m1']
49        }
50        with mock.patch.object(common_util, 'get_android_root_dir') as obj:
51            obj.return_value = IMLGenUnittests._TEST_DIR
52            self.iml = iml.IMLGenerator(module)
53
54    def tearDown(self):
55        """Clear the testdata related path."""
56        self.iml = None
57        shutil.rmtree(IMLGenUnittests._TEST_DIR)
58
59    def test_init(self):
60        """Test initialize the attributes."""
61        self.assertEqual(self.iml._mod_info['module_name'], 'test')
62
63    @mock.patch.object(common_util, 'get_android_root_dir')
64    def test_iml_path(self, mock_root_path):
65        """Test iml_path."""
66        mock_root_path.return_value = IMLGenUnittests._TEST_DIR
67        iml_path = os.path.join(IMLGenUnittests._TEST_DIR, 'a/b/test_iml.iml')
68        self.assertEqual(self.iml.iml_path, iml_path)
69
70    @mock.patch.object(common_util, 'get_android_root_dir')
71    def test_create(self, mock_root_path):
72        """Test create."""
73        mock_root_path.return_value = IMLGenUnittests._TEST_DIR
74        module_path = os.path.join(IMLGenUnittests._TEST_DIR, 'a/b')
75        src_path = os.path.join(IMLGenUnittests._TEST_DIR, 'a/b/src')
76        test_path = os.path.join(IMLGenUnittests._TEST_DIR, 'a/b/tests')
77        srcjar_path = os.path.join(IMLGenUnittests._TEST_DIR, 'x/y.srcjar')
78        jar_path = os.path.join(IMLGenUnittests._TEST_DIR, 's.jar')
79        expected = """<?xml version="1.0" encoding="UTF-8"?>
80<module type="JAVA_MODULE" version="4">
81    <component name="NewModuleRootManager" inherit-compiler-output="true">
82        <exclude-output />
83        <content url="file://{MODULE_PATH}">
84            <sourceFolder url="file://{SRC_PATH}" isTestSource="false" />
85            <sourceFolder url="file://{TEST_PATH}" isTestSource="true" />
86        </content>
87        <orderEntry type="sourceFolder" forTests="false" />
88        <content url="jar://{SRCJAR}!/">
89            <sourceFolder url="jar://{SRCJAR}!/" isTestSource="False" />
90        </content>
91        <orderEntry type="module" module-name="m1" />
92        <orderEntry type="module-library" exported="">
93          <library>
94            <CLASSES>
95              <root url="jar://{JAR}!/" />
96            </CLASSES>
97            <JAVADOC />
98            <SOURCES />
99          </library>
100        </orderEntry>
101        <orderEntry type="inheritedJdk" />
102    </component>
103</module>
104""".format(MODULE_PATH=module_path,
105           SRC_PATH=src_path,
106           TEST_PATH=test_path,
107           SRCJAR=srcjar_path,
108           JAR=jar_path)
109        self.iml.create({'srcs': True, 'srcjars': True, 'dependencies': True,
110                         'jars': True})
111        gen_iml = os.path.join(IMLGenUnittests._TEST_DIR,
112                               self.iml._mod_info['path'][0],
113                               self.iml._mod_info['iml_name'] + '.iml')
114        result = common_util.read_file_content(gen_iml)
115        self.assertEqual(result, expected)
116
117    @mock.patch.object(common_util, 'get_android_root_dir')
118    def test_gen_dep_sources(self, mock_root_path):
119        """Test _generate_dep_srcs."""
120        mock_root_path.return_value = IMLGenUnittests._TEST_DIR
121        src_path = os.path.join(IMLGenUnittests._TEST_DIR, 'a/b/src')
122        test_path = os.path.join(IMLGenUnittests._TEST_DIR, 'a/b/tests')
123        expected = """<?xml version="1.0" encoding="UTF-8"?>
124<module type="JAVA_MODULE" version="4">
125    <component name="NewModuleRootManager" inherit-compiler-output="true">
126        <exclude-output />
127        <content url="file://{SRC_PATH}">
128            <sourceFolder url="file://{SRC_PATH}" isTestSource="false" />
129        </content>
130        <content url="file://{TEST_PATH}">
131            <sourceFolder url="file://{TEST_PATH}" isTestSource="true" />
132        </content>
133        <orderEntry type="sourceFolder" forTests="false" />
134        <orderEntry type="inheritedJdk" />
135    </component>
136</module>
137""".format(SRC_PATH=src_path,
138           TEST_PATH=test_path)
139        self.iml.create({'dep_srcs': True})
140        gen_iml = os.path.join(IMLGenUnittests._TEST_DIR,
141                               self.iml._mod_info['path'][0],
142                               self.iml._mod_info['iml_name'] + '.iml')
143        result = common_util.read_file_content(gen_iml)
144        self.assertEqual(result, expected)
145
146    @mock.patch.object(iml.IMLGenerator, '_create_iml')
147    @mock.patch.object(iml.IMLGenerator, '_generate_dependencies')
148    @mock.patch.object(iml.IMLGenerator, '_generate_srcjars')
149    def test_skip_create_iml(self, mock_gen_srcjars, mock_gen_dep,
150                             mock_create_iml):
151        """Test skipping create_iml."""
152        self.iml.create({'srcjars': False, 'dependencies': False})
153        self.assertFalse(mock_gen_srcjars.called)
154        self.assertFalse(mock_gen_dep.called)
155        self.assertFalse(mock_create_iml.called)
156
157    @mock.patch('os.path.exists')
158    def test_generate_facet(self, mock_exists):
159        """Test _generate_facet."""
160        mock_exists.return_value = False
161        self.iml._generate_facet()
162        self.assertEqual(self.iml._facet, '')
163        mock_exists.return_value = True
164        self.iml._generate_facet()
165        self.assertEqual(self.iml._facet, templates.FACET)
166
167    @mock.patch.object(common_util, 'get_android_root_dir')
168    def test_get_uniq_iml_name(self, mock_root_path):
169        """Test the unique name cache mechanism.
170
171        By using the path data in module info json as input, if the count of
172        name data set is the same as sub folder path count, then it means
173        there's no duplicated name, the test PASS.
174        """
175        root_path = '/usr/abc/code/root'
176        mock_root_path.return_value = root_path
177        # Add following test path.
178        test_paths = {
179            'cts/tests/tests/app',
180            'cts/tests/app',
181            'cts/tests/app/app1/../app',
182            'cts/tests/app/app2/../app',
183            'cts/tests/app/app3/../app',
184            'frameworks/base/tests/xxxxxxxxxxxx/base',
185            'frameworks/base',
186            'external/xxxxx-xxx/robolectric',
187            'external/robolectric',
188        }
189        # Add whole source tree test paths.
190        whole_source_tree_paths = {
191            root_path,
192            root_path + "/",
193        }
194
195        mod_info = module_info.ModuleInfo()
196        test_paths.update(mod_info._get_path_to_module_info(
197            mod_info.name_to_module_info).keys())
198        print('\n{} {}.'.format('Test_paths length:', len(test_paths)))
199
200        path_list = []
201        for path in test_paths:
202            path_list.append(path)
203        print('{} {}.'.format('path list with length:', len(path_list)))
204
205        names = [iml.IMLGenerator.get_unique_iml_name(f)
206                 for f in path_list]
207        print('{} {}.'.format('Names list with length:', len(names)))
208        self.assertEqual(len(names), len(path_list))
209        dic = {}
210        for i, path in enumerate(path_list):
211            dic[names[i]] = path
212        print('{} {}.'.format('The size of name set is:', len(dic)))
213        self.assertEqual(len(dic), len(path_list))
214
215        for path in whole_source_tree_paths:
216            self.assertEqual(os.path.basename(root_path),
217                             iml.IMLGenerator.get_unique_iml_name(path))
218
219
220if __name__ == '__main__':
221    unittest.main()
222