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 VscSettingsManagerNoSideEffects, 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 29 ide_settings = self.make_ide_settings() 30 31 manager = VscSettingsManagerNoSideEffects( 32 ide_settings, self.temp_dir_path 33 ) 34 35 with manager.active( 36 VscSettingsType.SETTINGS 37 ).build() as active_settings: 38 manager.default(VscSettingsType.SETTINGS).sync_to(active_settings) 39 manager.project(VscSettingsType.SETTINGS).sync_to(active_settings) 40 manager.user(VscSettingsType.SETTINGS).sync_to(active_settings) 41 42 with manager.active(VscSettingsType.TASKS).build() as active_settings: 43 manager.default(VscSettingsType.TASKS).sync_to(active_settings) 44 manager.project(VscSettingsType.TASKS).sync_to(active_settings) 45 manager.user(VscSettingsType.TASKS).sync_to(active_settings) 46 47 with manager.active( 48 VscSettingsType.EXTENSIONS 49 ).build() as active_settings: 50 manager.default(VscSettingsType.EXTENSIONS).sync_to(active_settings) 51 manager.project(VscSettingsType.EXTENSIONS).sync_to(active_settings) 52 manager.user(VscSettingsType.EXTENSIONS).sync_to(active_settings) 53 54 def test_json5(self): 55 """Test that we can parse JSON5 files.""" 56 content = """{ 57 // This is a comment, and this list has a trailing comma. 58 "_pw": [ 59 "foo", 60 "bar", 61 "baz", 62 ] 63} 64 """ 65 66 self.touch_temp_file('pw_project_settings.json', content) 67 68 ide_settings = self.make_ide_settings() 69 70 manager = VscSettingsManagerNoSideEffects( 71 ide_settings, self.temp_dir_path 72 ) 73 74 with manager.active( 75 VscSettingsType.SETTINGS 76 ).build() as active_settings: 77 manager.default(VscSettingsType.SETTINGS).sync_to(active_settings) 78 manager.project(VscSettingsType.SETTINGS).sync_to(active_settings) 79 manager.user(VscSettingsType.SETTINGS).sync_to(active_settings) 80 81 active_settings = manager.active(VscSettingsType.SETTINGS).get() 82 self.assertIn('_pw', active_settings.keys()) 83 self.assertEqual(len(active_settings['_pw']), 3) 84 85 86if __name__ == '__main__': 87 unittest.main() 88