• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2022 The Pigweed Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7#     https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14"""Tests for pw_ide.editors"""
15
16import unittest
17
18from pw_ide.vscode import VscSettingsManager, VscSettingsType
19
20from test_cases import PwIdeTestCase
21
22
23class TestVscSettingsManager(PwIdeTestCase):
24    """Tests VscSettingsManager"""
25
26    def test_setup(self):
27        """Test realistic setup procedure. Success == doesn't raise."""
28        ide_settings = self.make_ide_settings()
29        manager = VscSettingsManager(ide_settings, self.temp_dir_path)
30
31        with manager.active(
32            VscSettingsType.SETTINGS
33        ).modify() as active_settings:
34            manager.default(VscSettingsType.SETTINGS).sync_to(active_settings)
35            manager.project(VscSettingsType.SETTINGS).sync_to(active_settings)
36            manager.user(VscSettingsType.SETTINGS).sync_to(active_settings)
37
38        with manager.active(VscSettingsType.TASKS).modify() as active_settings:
39            manager.default(VscSettingsType.TASKS).sync_to(active_settings)
40            manager.project(VscSettingsType.TASKS).sync_to(active_settings)
41            manager.user(VscSettingsType.TASKS).sync_to(active_settings)
42
43        with manager.active(
44            VscSettingsType.EXTENSIONS
45        ).modify() as active_settings:
46            manager.default(VscSettingsType.EXTENSIONS).sync_to(active_settings)
47            manager.project(VscSettingsType.EXTENSIONS).sync_to(active_settings)
48            manager.user(VscSettingsType.EXTENSIONS).sync_to(active_settings)
49
50    def test_json5(self):
51        """Test that we can parse JSON5 files."""
52        content = """{
53    // This is a comment, and this list has a trailing comma.
54    "_pw": [
55        "foo",
56        "bar",
57        "baz",
58    ]
59}
60        """
61
62        self.touch_temp_file('pw_project_settings.json', content)
63        ide_settings = self.make_ide_settings()
64        manager = VscSettingsManager(ide_settings, self.temp_dir_path)
65
66        with manager.active(
67            VscSettingsType.SETTINGS
68        ).modify() as active_settings:
69            manager.default(VscSettingsType.SETTINGS).sync_to(active_settings)
70            manager.project(VscSettingsType.SETTINGS).sync_to(active_settings)
71            manager.user(VscSettingsType.SETTINGS).sync_to(active_settings)
72
73        active_settings = manager.active(VscSettingsType.SETTINGS).get()
74        self.assertIn('_pw', active_settings.keys())
75        self.assertEqual(len(active_settings['_pw']), 3)
76
77
78if __name__ == '__main__':
79    unittest.main()
80