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
18"""Unittests for ide_util."""
19
20import os
21import unittest
22from unittest import mock
23
24from aidegen.lib import common_util
25from aidegen.vscode import vscode_workspace_file_gen
26
27
28# pylint: disable=protected-access
29class WorkspaceProjectFileGenUnittests(unittest.TestCase):
30    """Unit tests for ide_util.py."""
31
32    @mock.patch.object(vscode_workspace_file_gen, '_get_unique_project_name')
33    @mock.patch.object(
34        vscode_workspace_file_gen, '_create_code_workspace_file_content')
35    @mock.patch.object(common_util, 'get_android_root_dir')
36    def test_vdcode_apply_optional_config(
37            self, mock_get_root, mock_ws_content, mock_get_unique):
38        """Test IdeVSCode's apply_optional_config method."""
39        mock_get_root.return_value = 'a/b'
40        vscode_workspace_file_gen.generate_code_workspace_file(
41            ['a/b/path_to_project1', 'a/b/path_to_project2'])
42        self.assertTrue(mock_get_root.called)
43        self.assertTrue(mock_ws_content.called)
44        self.assertTrue(mock_get_unique.called)
45
46    @mock.patch.object(common_util, 'dump_json_dict')
47    def test_create_code_workspace_file_content(self, mock_dump):
48        """Test _create_code_workspace_file_content function."""
49        abs_path = 'a/b'
50        folder_name = 'Settings'
51        res = vscode_workspace_file_gen._create_code_workspace_file_content(
52            {'folders': [{'name': folder_name, 'path': abs_path}]})
53        self.assertTrue(mock_dump.called)
54        file_name = ''.join(
55            [folder_name, vscode_workspace_file_gen._VSCODE_WORKSPACE_EXT])
56        file_path = os.path.join(abs_path, file_name)
57        self.assertEqual(res, file_path)
58
59    def test_get_unique_project_name(self):
60        """Test _get_unique_project_name function with conditions."""
61        abs_path = 'a/b/to/project'
62        root_dir = 'a/b'
63        expected = 'to.project'
64        result = vscode_workspace_file_gen._get_unique_project_name(
65            abs_path, root_dir)
66        self.assertEqual(result, expected)
67        abs_path = 'a/b'
68        expected = 'b'
69        result = vscode_workspace_file_gen._get_unique_project_name(
70            abs_path, root_dir)
71        self.assertEqual(result, expected)
72
73
74if __name__ == '__main__':
75    unittest.main()
76