xref: /aosp_15_r20/tools/asuite/aidegen/vscode/vscode_native_project_file_gen_unittest.py (revision c2e18aaa1096c836b086f94603d04f4eb9cf37f5)
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 vscode_native_project_file_gen."""
18
19import os
20import unittest
21from unittest import mock
22
23from aidegen.lib import common_util
24from aidegen.vscode import vscode_native_project_file_gen
25
26
27# pylint: disable=protected-access
28class VSCodeNativeProjectFileGenUnittests(unittest.TestCase):
29    """Unit tests for vscode_native_project_file_gen.py"""
30
31    @mock.patch.object(os, 'mkdir')
32    @mock.patch.object(os.path, 'isdir')
33    def test_init(self, mock_isdir, mock_mkdir):
34        """Test initializing VSCodeNativeProjectFileGenerator."""
35        mod_dir = 'a/b/packages/apps/Settings'
36        mock_isdir.return_value = True
37        vscode_native_project_file_gen.VSCodeNativeProjectFileGenerator(mod_dir)
38        self.assertFalse(mock_mkdir.called)
39        mock_mkdir.mock_reset()
40        mock_isdir.return_value = False
41        vscode_native_project_file_gen.VSCodeNativeProjectFileGenerator(mod_dir)
42        self.assertTrue(mock_mkdir.called)
43
44    @mock.patch.object(os.path, 'isdir')
45    @mock.patch.object(os.path, 'isfile')
46    @mock.patch.object(common_util, 'get_android_root_dir')
47    def test_create_c_cpp_properties_dict(self, mock_get_root, mock_isfile,
48                                          mock_isdir):
49        """Test _create_c_cpp_properties_dict with conditions."""
50        mock_get_root.return_value = '/root'
51        mock_isdir.return_value = True
52        includes = ['a/b/includes', 'c/d/includes']
53        mod_dir = 'a/b/shared/path/to/be/used2/multiarch'
54        ccgen = vscode_native_project_file_gen.VSCodeNativeProjectFileGenerator(
55            mod_dir)
56        cc_mod_info = mock.Mock()
57        cc_mod_info.get_module_includes.return_value = includes
58
59        mock_isfile.return_value = True  # Compiler path exists.
60        data = ccgen._create_c_cpp_properties_dict(cc_mod_info, ['multiarch'])
61        config = data[vscode_native_project_file_gen._CONFIG][0]
62        self.assertCountEqual(
63            ['/root/a/b/includes', '/root/c/d/includes'],
64            config[vscode_native_project_file_gen._INC_PATH])
65        self.assertEqual(
66            vscode_native_project_file_gen._COMPILER_PATH,
67            config[vscode_native_project_file_gen._COMPILE_PATH])
68
69        mock_isfile.return_value = False  # Compiler path doesn't exist.
70        data = ccgen._create_c_cpp_properties_dict(cc_mod_info, ['multiarch'])
71        config = data[vscode_native_project_file_gen._CONFIG][0]
72        self.assertCountEqual(
73            ['/root/a/b/includes', '/root/c/d/includes'],
74            config[vscode_native_project_file_gen._INC_PATH])
75        self.assertEqual('',
76                         config[vscode_native_project_file_gen._COMPILE_PATH])
77
78
79if __name__ == '__main__':
80    unittest.main()
81