xref: /aosp_15_r20/tools/asuite/aidegen/lib/module_info_unittest.py (revision c2e18aaa1096c836b086f94603d04f4eb9cf37f5)
1*c2e18aaaSAndroid Build Coastguard Worker#!/usr/bin/env python3
2*c2e18aaaSAndroid Build Coastguard Worker#
3*c2e18aaaSAndroid Build Coastguard Worker# Copyright 2019, The Android Open Source Project
4*c2e18aaaSAndroid Build Coastguard Worker#
5*c2e18aaaSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
6*c2e18aaaSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
7*c2e18aaaSAndroid Build Coastguard Worker# You may obtain a copy of the License at
8*c2e18aaaSAndroid Build Coastguard Worker#
9*c2e18aaaSAndroid Build Coastguard Worker#     http://www.apache.org/licenses/LICENSE-2.0
10*c2e18aaaSAndroid Build Coastguard Worker#
11*c2e18aaaSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
12*c2e18aaaSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
13*c2e18aaaSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14*c2e18aaaSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
15*c2e18aaaSAndroid Build Coastguard Worker# limitations under the License.
16*c2e18aaaSAndroid Build Coastguard Worker
17*c2e18aaaSAndroid Build Coastguard Worker"""Unittests for module_info."""
18*c2e18aaaSAndroid Build Coastguard Worker
19*c2e18aaaSAndroid Build Coastguard Workerimport os
20*c2e18aaaSAndroid Build Coastguard Workerimport unittest
21*c2e18aaaSAndroid Build Coastguard Worker
22*c2e18aaaSAndroid Build Coastguard Workerfrom unittest import mock
23*c2e18aaaSAndroid Build Coastguard Worker
24*c2e18aaaSAndroid Build Coastguard Workerfrom aidegen.lib import common_util
25*c2e18aaaSAndroid Build Coastguard Workerfrom aidegen.lib import module_info
26*c2e18aaaSAndroid Build Coastguard Workerfrom aidegen.lib import module_info_util
27*c2e18aaaSAndroid Build Coastguard Worker
28*c2e18aaaSAndroid Build Coastguard Worker
29*c2e18aaaSAndroid Build Coastguard Worker# pylint: disable=protected-access
30*c2e18aaaSAndroid Build Coastguard Workerclass AidegenModuleInfoUnittests(unittest.TestCase):
31*c2e18aaaSAndroid Build Coastguard Worker    """Unit tests for module_info.py"""
32*c2e18aaaSAndroid Build Coastguard Worker
33*c2e18aaaSAndroid Build Coastguard Worker    def test_is_target_module(self):
34*c2e18aaaSAndroid Build Coastguard Worker        """Test is_target_module with different conditions."""
35*c2e18aaaSAndroid Build Coastguard Worker        self.assertFalse(module_info.AidegenModuleInfo.is_target_module({}))
36*c2e18aaaSAndroid Build Coastguard Worker        self.assertFalse(module_info.AidegenModuleInfo.is_target_module(
37*c2e18aaaSAndroid Build Coastguard Worker            {'path': ''}))
38*c2e18aaaSAndroid Build Coastguard Worker        self.assertFalse(module_info.AidegenModuleInfo.is_target_module(
39*c2e18aaaSAndroid Build Coastguard Worker            {'class': ''}))
40*c2e18aaaSAndroid Build Coastguard Worker        self.assertTrue(module_info.AidegenModuleInfo.is_target_module(
41*c2e18aaaSAndroid Build Coastguard Worker            {'class': ['APPS']}))
42*c2e18aaaSAndroid Build Coastguard Worker        self.assertTrue(
43*c2e18aaaSAndroid Build Coastguard Worker            module_info.AidegenModuleInfo.is_target_module(
44*c2e18aaaSAndroid Build Coastguard Worker                {'class': ['JAVA_LIBRARIES']}))
45*c2e18aaaSAndroid Build Coastguard Worker        self.assertTrue(
46*c2e18aaaSAndroid Build Coastguard Worker            module_info.AidegenModuleInfo.is_target_module(
47*c2e18aaaSAndroid Build Coastguard Worker                {'class': ['ROBOLECTRIC']}))
48*c2e18aaaSAndroid Build Coastguard Worker
49*c2e18aaaSAndroid Build Coastguard Worker    def test_is_project_path_relative_module(self):
50*c2e18aaaSAndroid Build Coastguard Worker        """Test is_project_path_relative_module handling."""
51*c2e18aaaSAndroid Build Coastguard Worker        mod_info = {'class': ['APPS']}
52*c2e18aaaSAndroid Build Coastguard Worker        self.assertFalse(
53*c2e18aaaSAndroid Build Coastguard Worker            module_info.AidegenModuleInfo.is_project_path_relative_module(
54*c2e18aaaSAndroid Build Coastguard Worker                mod_info, ''))
55*c2e18aaaSAndroid Build Coastguard Worker        mod_info = {'class': ['APPS'], 'path': []}
56*c2e18aaaSAndroid Build Coastguard Worker        self.assertFalse(
57*c2e18aaaSAndroid Build Coastguard Worker            module_info.AidegenModuleInfo.is_project_path_relative_module(
58*c2e18aaaSAndroid Build Coastguard Worker                mod_info, ''))
59*c2e18aaaSAndroid Build Coastguard Worker        mod_info = {'class': ['APPS'], 'path': ['path_to_a']}
60*c2e18aaaSAndroid Build Coastguard Worker        self.assertTrue(
61*c2e18aaaSAndroid Build Coastguard Worker            module_info.AidegenModuleInfo.is_project_path_relative_module(
62*c2e18aaaSAndroid Build Coastguard Worker                mod_info, ''))
63*c2e18aaaSAndroid Build Coastguard Worker        self.assertFalse(
64*c2e18aaaSAndroid Build Coastguard Worker            module_info.AidegenModuleInfo.is_project_path_relative_module(
65*c2e18aaaSAndroid Build Coastguard Worker                mod_info, 'test'))
66*c2e18aaaSAndroid Build Coastguard Worker        mod_info = {'path': ['path_to_a']}
67*c2e18aaaSAndroid Build Coastguard Worker        self.assertFalse(
68*c2e18aaaSAndroid Build Coastguard Worker            module_info.AidegenModuleInfo.is_project_path_relative_module(
69*c2e18aaaSAndroid Build Coastguard Worker                mod_info, 'test'))
70*c2e18aaaSAndroid Build Coastguard Worker        self.assertFalse(
71*c2e18aaaSAndroid Build Coastguard Worker            module_info.AidegenModuleInfo.is_project_path_relative_module(
72*c2e18aaaSAndroid Build Coastguard Worker                mod_info, 'path_to_a'))
73*c2e18aaaSAndroid Build Coastguard Worker        mod_info = {'class': ['APPS'], 'path': ['test/path_to_a']}
74*c2e18aaaSAndroid Build Coastguard Worker        self.assertTrue(
75*c2e18aaaSAndroid Build Coastguard Worker            module_info.AidegenModuleInfo.is_project_path_relative_module(
76*c2e18aaaSAndroid Build Coastguard Worker                mod_info, 'test'))
77*c2e18aaaSAndroid Build Coastguard Worker        self.assertFalse(
78*c2e18aaaSAndroid Build Coastguard Worker            module_info.AidegenModuleInfo.is_project_path_relative_module(
79*c2e18aaaSAndroid Build Coastguard Worker                mod_info, 'tes'))
80*c2e18aaaSAndroid Build Coastguard Worker
81*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch.object(common_util, 'dump_json_dict')
82*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch('logging.debug')
83*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch.object(module_info_util, 'generate_merged_module_info')
84*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch.object(os.path, 'isfile')
85*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch('os.remove')
86*c2e18aaaSAndroid Build Coastguard Worker    def test_discover_mod_file_and_target(self, mock_remove, mock_is_file,
87*c2e18aaaSAndroid Build Coastguard Worker                                          mock_generate, mock_log, mock_dump):
88*c2e18aaaSAndroid Build Coastguard Worker        """Test _discover_mod_file_and_target with conditions."""
89*c2e18aaaSAndroid Build Coastguard Worker        # Test not force build case.
90*c2e18aaaSAndroid Build Coastguard Worker        mock_generate.return_value = None
91*c2e18aaaSAndroid Build Coastguard Worker        force_build = False
92*c2e18aaaSAndroid Build Coastguard Worker        mock_is_file.return_value = True
93*c2e18aaaSAndroid Build Coastguard Worker        module_info.AidegenModuleInfo._discover_mod_file_and_target(force_build)
94*c2e18aaaSAndroid Build Coastguard Worker        self.assertFalse(mock_remove.called)
95*c2e18aaaSAndroid Build Coastguard Worker        self.assertFalse(mock_log.called)
96*c2e18aaaSAndroid Build Coastguard Worker        self.assertTrue(mock_generate.called)
97*c2e18aaaSAndroid Build Coastguard Worker        self.assertTrue(mock_dump.called)
98*c2e18aaaSAndroid Build Coastguard Worker
99*c2e18aaaSAndroid Build Coastguard Worker        # Test force_build case.
100*c2e18aaaSAndroid Build Coastguard Worker        force_build = True
101*c2e18aaaSAndroid Build Coastguard Worker        self.assertTrue(mock_is_file.called)
102*c2e18aaaSAndroid Build Coastguard Worker        mock_is_file.return_value = False
103*c2e18aaaSAndroid Build Coastguard Worker        module_info.AidegenModuleInfo._discover_mod_file_and_target(force_build)
104*c2e18aaaSAndroid Build Coastguard Worker        self.assertFalse(mock_remove.called)
105*c2e18aaaSAndroid Build Coastguard Worker        self.assertTrue(mock_log.called)
106*c2e18aaaSAndroid Build Coastguard Worker        self.assertTrue(mock_dump.called)
107*c2e18aaaSAndroid Build Coastguard Worker
108*c2e18aaaSAndroid Build Coastguard Worker        mock_is_file.return_value = True
109*c2e18aaaSAndroid Build Coastguard Worker        module_info.AidegenModuleInfo._discover_mod_file_and_target(force_build)
110*c2e18aaaSAndroid Build Coastguard Worker        self.assertTrue(mock_remove.called)
111*c2e18aaaSAndroid Build Coastguard Worker        self.assertTrue(mock_dump.called)
112*c2e18aaaSAndroid Build Coastguard Worker
113*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch.object(module_info.AidegenModuleInfo,
114*c2e18aaaSAndroid Build Coastguard Worker                       '_discover_mod_file_and_target')
115*c2e18aaaSAndroid Build Coastguard Worker    def test_load_module_info_file(self, mock_discover):
116*c2e18aaaSAndroid Build Coastguard Worker        """Test _load_module_info_file with conditions."""
117*c2e18aaaSAndroid Build Coastguard Worker        json_path = os.path.join(os.path.dirname(os.path.dirname(__file__)),
118*c2e18aaaSAndroid Build Coastguard Worker                                 'test_data/out/soong/merged_module_info.json')
119*c2e18aaaSAndroid Build Coastguard Worker        # Test file exist case.
120*c2e18aaaSAndroid Build Coastguard Worker        module_file = json_path
121*c2e18aaaSAndroid Build Coastguard Worker        module_info.AidegenModuleInfo._load_module_info_file(self, module_file)
122*c2e18aaaSAndroid Build Coastguard Worker        self.assertFalse(mock_discover.called)
123*c2e18aaaSAndroid Build Coastguard Worker
124*c2e18aaaSAndroid Build Coastguard Worker
125*c2e18aaaSAndroid Build Coastguard Workerif __name__ == '__main__':
126*c2e18aaaSAndroid Build Coastguard Worker    unittest.main()
127