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