1# Copyright 2021 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_console.console_app""" 15 16from pathlib import Path 17import tempfile 18import unittest 19 20import yaml 21 22# pylint: disable=protected-access 23from pw_console.console_prefs import ( 24 ConsolePrefs, 25 _DEFAULT_CONFIG, 26) 27 28 29def _create_tempfile(content: str) -> Path: 30 with tempfile.NamedTemporaryFile( 31 prefix=f'{__package__}', delete=False 32 ) as output_file: 33 output_file.write(content.encode('UTF-8')) 34 return Path(output_file.name) 35 36 37class TestConsolePrefs(unittest.TestCase): 38 """Tests for ConsolePrefs.""" 39 40 def setUp(self): 41 self.maxDiff = None # pylint: disable=invalid-name 42 43 def test_load_no_existing_files(self) -> None: 44 prefs = ConsolePrefs( 45 project_file=False, project_user_file=False, user_file=False 46 ) 47 self.assertEqual(_DEFAULT_CONFIG, prefs._config) 48 self.assertTrue(str(prefs.repl_history).endswith('pw_console_history')) 49 self.assertTrue(str(prefs.search_history).endswith('pw_console_search')) 50 51 def test_load_empty_file(self) -> None: 52 # Create an empty file 53 project_config_file = _create_tempfile('') 54 try: 55 prefs = ConsolePrefs( 56 project_file=project_config_file, 57 project_user_file=False, 58 user_file=False, 59 ) 60 result_settings = { 61 k: v 62 for k, v in prefs._config.items() 63 if k in _DEFAULT_CONFIG.keys() 64 } 65 other_settings = { 66 k: v 67 for k, v in prefs._config.items() 68 if k not in _DEFAULT_CONFIG.keys() 69 } 70 # Check that only the default config was loaded. 71 self.assertEqual(_DEFAULT_CONFIG, result_settings) 72 self.assertEqual(0, len(other_settings)) 73 finally: 74 project_config_file.unlink() 75 76 def test_load_project_file(self) -> None: 77 project_config = { 78 'pw_console': { 79 'ui_theme': 'light', 80 'code_theme': 'cool-code', 81 'swap_light_and_dark': True, 82 }, 83 } 84 project_config_file = _create_tempfile(yaml.dump(project_config)) 85 try: 86 prefs = ConsolePrefs( 87 project_file=project_config_file, 88 project_user_file=False, 89 user_file=False, 90 ) 91 result_settings = { 92 k: v 93 for k, v in prefs._config.items() 94 if k in project_config['pw_console'].keys() 95 } 96 other_settings = { 97 k: v 98 for k, v in prefs._config.items() 99 if k not in project_config['pw_console'].keys() 100 } 101 self.assertEqual(project_config['pw_console'], result_settings) 102 self.assertNotEqual(0, len(other_settings)) 103 finally: 104 project_config_file.unlink() 105 106 def test_load_project_and_user_file(self) -> None: 107 """Test user settings override project settings.""" 108 project_config = { 109 'pw_console': { 110 'ui_theme': 'light', 111 'code_theme': 'cool-code', 112 'swap_light_and_dark': True, 113 'repl_history': '~/project_history', 114 'search_history': '~/project_search', 115 }, 116 } 117 project_config_file = _create_tempfile(yaml.dump(project_config)) 118 119 project_user_config = { 120 'pw_console': { 121 'ui_theme': 'nord', 122 'repl_history': '~/project_user_history', 123 'search_history': '~/project_user_search', 124 }, 125 } 126 project_user_config_file = _create_tempfile( 127 yaml.dump(project_user_config) 128 ) 129 130 user_config = { 131 'pw_console': { 132 'ui_theme': 'dark', 133 'search_history': '~/user_search', 134 }, 135 } 136 user_config_file = _create_tempfile(yaml.dump(user_config)) 137 try: 138 prefs = ConsolePrefs( 139 project_file=project_config_file, 140 project_user_file=project_user_config_file, 141 user_file=user_config_file, 142 ) 143 # Set by the project 144 self.assertEqual( 145 project_config['pw_console']['code_theme'], prefs.code_theme 146 ) 147 self.assertEqual( 148 project_config['pw_console']['swap_light_and_dark'], 149 prefs.swap_light_and_dark, 150 ) 151 152 # Project user setting, result should not be project only setting. 153 project_history = project_config['pw_console']['repl_history'] 154 assert isinstance(project_history, str) 155 self.assertNotEqual( 156 Path(project_history).expanduser(), prefs.repl_history 157 ) 158 159 history = project_user_config['pw_console']['repl_history'] 160 assert isinstance(history, str) 161 self.assertEqual(Path(history).expanduser(), prefs.repl_history) 162 163 # User config overrides project and project_user 164 self.assertEqual( 165 user_config['pw_console']['ui_theme'], prefs.ui_theme 166 ) 167 self.assertEqual( 168 Path(user_config['pw_console']['search_history']).expanduser(), 169 prefs.search_history, 170 ) 171 # ui_theme should not be the project_user file setting 172 project_user_theme = project_user_config['pw_console']['ui_theme'] 173 self.assertNotEqual(project_user_theme, prefs.ui_theme) 174 finally: 175 project_config_file.unlink() 176 project_user_config_file.unlink() 177 user_config_file.unlink() 178 179 180if __name__ == '__main__': 181 unittest.main() 182