• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 module_info."""
18
19import os
20import unittest
21
22from unittest import mock
23
24from aidegen.lib import common_util
25from aidegen.lib import module_info
26from aidegen.lib import module_info_util
27
28
29# pylint: disable=protected-access
30class AidegenModuleInfoUnittests(unittest.TestCase):
31    """Unit tests for module_info.py"""
32
33    def test_is_target_module(self):
34        """Test is_target_module with different conditions."""
35        self.assertFalse(module_info.AidegenModuleInfo.is_target_module({}))
36        self.assertFalse(module_info.AidegenModuleInfo.is_target_module(
37            {'path': ''}))
38        self.assertFalse(module_info.AidegenModuleInfo.is_target_module(
39            {'class': ''}))
40        self.assertTrue(module_info.AidegenModuleInfo.is_target_module(
41            {'class': ['APPS']}))
42        self.assertTrue(
43            module_info.AidegenModuleInfo.is_target_module(
44                {'class': ['JAVA_LIBRARIES']}))
45        self.assertTrue(
46            module_info.AidegenModuleInfo.is_target_module(
47                {'class': ['ROBOLECTRIC']}))
48
49    def test_is_project_path_relative_module(self):
50        """Test is_project_path_relative_module handling."""
51        mod_info = {'class': ['APPS']}
52        self.assertFalse(
53            module_info.AidegenModuleInfo.is_project_path_relative_module(
54                mod_info, ''))
55        mod_info = {'class': ['APPS'], 'path': []}
56        self.assertFalse(
57            module_info.AidegenModuleInfo.is_project_path_relative_module(
58                mod_info, ''))
59        mod_info = {'class': ['APPS'], 'path': ['path_to_a']}
60        self.assertTrue(
61            module_info.AidegenModuleInfo.is_project_path_relative_module(
62                mod_info, ''))
63        self.assertFalse(
64            module_info.AidegenModuleInfo.is_project_path_relative_module(
65                mod_info, 'test'))
66        mod_info = {'path': ['path_to_a']}
67        self.assertFalse(
68            module_info.AidegenModuleInfo.is_project_path_relative_module(
69                mod_info, 'test'))
70        self.assertFalse(
71            module_info.AidegenModuleInfo.is_project_path_relative_module(
72                mod_info, 'path_to_a'))
73        mod_info = {'class': ['APPS'], 'path': ['test/path_to_a']}
74        self.assertTrue(
75            module_info.AidegenModuleInfo.is_project_path_relative_module(
76                mod_info, 'test'))
77        self.assertFalse(
78            module_info.AidegenModuleInfo.is_project_path_relative_module(
79                mod_info, 'tes'))
80
81    @mock.patch.object(common_util, 'dump_json_dict')
82    @mock.patch('logging.debug')
83    @mock.patch.object(module_info_util, 'generate_merged_module_info')
84    @mock.patch.object(os.path, 'isfile')
85    @mock.patch('os.remove')
86    def test_discover_mod_file_and_target(self, mock_remove, mock_is_file,
87                                          mock_generate, mock_log, mock_dump):
88        """Test _discover_mod_file_and_target with conditions."""
89        # Test not force build case.
90        mock_generate.return_value = None
91        force_build = False
92        mock_is_file.return_value = True
93        module_info.AidegenModuleInfo._discover_mod_file_and_target(force_build)
94        self.assertFalse(mock_remove.called)
95        self.assertFalse(mock_log.called)
96        self.assertTrue(mock_generate.called)
97        self.assertTrue(mock_dump.called)
98
99        # Test force_build case.
100        force_build = True
101        self.assertTrue(mock_is_file.called)
102        mock_is_file.return_value = False
103        module_info.AidegenModuleInfo._discover_mod_file_and_target(force_build)
104        self.assertFalse(mock_remove.called)
105        self.assertTrue(mock_log.called)
106        self.assertTrue(mock_dump.called)
107
108        mock_is_file.return_value = True
109        module_info.AidegenModuleInfo._discover_mod_file_and_target(force_build)
110        self.assertTrue(mock_remove.called)
111        self.assertTrue(mock_dump.called)
112
113    @mock.patch.object(module_info.AidegenModuleInfo,
114                       '_discover_mod_file_and_target')
115    def test_load_module_info_file(self, mock_discover):
116        """Test _load_module_info_file with conditions."""
117        json_path = os.path.join(os.path.dirname(os.path.dirname(__file__)),
118                                 'test_data/out/soong/merged_module_info.json')
119        # Test file exist case.
120        module_file = json_path
121        module_info.AidegenModuleInfo._load_module_info_file(self, module_file)
122        self.assertFalse(mock_discover.called)
123
124
125if __name__ == '__main__':
126    unittest.main()
127