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# pylint: disable=protected-access 29#pylint: disable=invalid-name 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':['path_to_a']} 56 self.assertTrue( 57 module_info.AidegenModuleInfo.is_project_path_relative_module( 58 mod_info, '')) 59 self.assertFalse( 60 module_info.AidegenModuleInfo.is_project_path_relative_module( 61 mod_info, 'test')) 62 mod_info = {'path':['path_to_a']} 63 self.assertFalse( 64 module_info.AidegenModuleInfo.is_project_path_relative_module( 65 mod_info, 'test')) 66 self.assertFalse( 67 module_info.AidegenModuleInfo.is_project_path_relative_module( 68 mod_info, 'path_to_a')) 69 mod_info = {'class':['APPS'], 'path':['test/path_to_a']} 70 self.assertTrue( 71 module_info.AidegenModuleInfo.is_project_path_relative_module( 72 mod_info, 'test')) 73 self.assertFalse( 74 module_info.AidegenModuleInfo.is_project_path_relative_module( 75 mod_info, 'tes')) 76 77 @mock.patch.object(common_util, 'dump_json_dict') 78 @mock.patch('logging.debug') 79 @mock.patch.object(module_info_util, 'generate_merged_module_info') 80 @mock.patch.object(os.path, 'isfile') 81 @mock.patch('os.remove') 82 def test_discover_mod_file_and_target(self, mock_remove, mock_is_file, 83 mock_generate, mock_log, mock_dump): 84 """Test _discover_mod_file_and_target with conditions.""" 85 # Test not force build case. 86 mock_generate.return_value = None 87 force_build = False 88 mock_is_file.return_value = True 89 module_info.AidegenModuleInfo._discover_mod_file_and_target(force_build) 90 self.assertFalse(mock_remove.called) 91 self.assertFalse(mock_log.called) 92 self.assertTrue(mock_generate.called) 93 self.assertTrue(mock_dump.called) 94 95 # Test force_build case. 96 force_build = True 97 self.assertTrue(mock_is_file.called) 98 mock_is_file.return_value = False 99 module_info.AidegenModuleInfo._discover_mod_file_and_target(force_build) 100 self.assertFalse(mock_remove.called) 101 self.assertTrue(mock_log.called) 102 self.assertTrue(mock_dump.called) 103 104 mock_is_file.return_value = True 105 module_info.AidegenModuleInfo._discover_mod_file_and_target(force_build) 106 self.assertTrue(mock_remove.called) 107 self.assertTrue(mock_dump.called) 108 109 110if __name__ == '__main__': 111 unittest.main() 112