def test_load_engine_options__blank_option_name__error_when_loading(self): with self.assertRaises(ValueError) as error: with tempfile.NamedTemporaryFile(mode='r+') as options_file: options_file.write('=test # type=string\n') options_file.seek(0) settings_loader.load_engine_options(TEST_ENGINE_OPTIONS, options_file) self.assertTrue('Missing option name on line \'=test # type=string\'' in str(error.exception))
def test_load_engine_options__duplicate_option__error_when_loading(self): with self.assertRaises(ValueError) as error: with tempfile.NamedTemporaryFile(mode='r+') as options_file: file_lines = [ 'string_none= # type=string\n', 'string_none= # type=string\n', ] options_file.writelines(file_lines) options_file.seek(0) settings_loader.load_engine_options(TEST_ENGINE_OPTIONS, options_file) self.assertTrue('Duplicate engine option \'string_none\'' in str(error.exception))
def test_load_engine_options__exlucde_defaults_false__after_save__loads_defaults(self): with tempfile.NamedTemporaryFile(mode='r+') as options_file: options_file.writelines( settings_loader.engine_options_file_lines(TEST_ENGINE_OPTIONS, TEST_ENGINE_OPTIONS_DICT)) options_file.seek(0) options = settings_loader.load_engine_options(TEST_ENGINE_OPTIONS, options_file, False) self.assertEqual(TEST_ENGINE_OPTIONS_DICT, options)
def test_load_engine_options__empty_changed_to_blank__loads_empty_options(self): with tempfile.NamedTemporaryFile(mode='r+') as options_file: modified_options = {'string_empty': ''} options_file.writelines(settings_loader.engine_options_file_lines(TEST_ENGINE_OPTIONS, modified_options)) options_file.seek(0) options = settings_loader.load_engine_options(TEST_ENGINE_OPTIONS, options_file) self.assertEqual(dict(), options)
def test_load_engine_options__empty_changed_to_blank__exlucde_defaults_false__loads_modified_options(self): with tempfile.NamedTemporaryFile(mode='r+') as options_file: modified_options = {'string_empty': ''} options_file.writelines(settings_loader.engine_options_file_lines(TEST_ENGINE_OPTIONS, modified_options)) options_file.seek(0) options = settings_loader.load_engine_options(TEST_ENGINE_OPTIONS, options_file, False) expected_options = dict(TEST_ENGINE_OPTIONS_DICT) expected_options['string_empty'] = '' self.assertEqual(expected_options, options)
def test_load_engine_options__after_save__default_option_added__loads_updated_options(self): with tempfile.NamedTemporaryFile(mode='r+') as options_file: options_file.writelines( settings_loader.engine_options_file_lines(TEST_ENGINE_OPTIONS, TEST_ENGINE_OPTIONS_DICT)) options_file.seek(0) updated_default_options = TEST_ENGINE_OPTIONS.copy() updated_default_options.append(engine.Option('new', 'string', None, None, None, None)) options = settings_loader.load_engine_options(updated_default_options, options_file, False) expected_options = dict(TEST_ENGINE_OPTIONS_DICT) expected_options['new'] = '' self.assertEqual(expected_options, options)
def test_load_engine_options__exlucde_defaults_false__modified_options__after_save__loads_modified_options(self): with tempfile.NamedTemporaryFile(mode='r+') as options_file: modified_options = {'string_none': 'test', 'spin': 100, 'combo': 'two', 'check_true': False} options_file.writelines(settings_loader.engine_options_file_lines(TEST_ENGINE_OPTIONS, modified_options)) options_file.seek(0) options = settings_loader.load_engine_options(TEST_ENGINE_OPTIONS, options_file, False) all_options = { name: modified_options[name] if name in modified_options else value for name, value in TEST_ENGINE_OPTIONS_DICT.items() } self.assertEqual(all_options, options)
def test_load_engine_options__edited_file__skip_blank_and_hashed_lines(self): with tempfile.NamedTemporaryFile(mode='r+') as options_file: file_lines = [ '\n', '#string_none=123 # type=string\n', '\n', 'check_true=False # type=string\n', ] options_file.writelines(file_lines) options_file.seek(0) self.assertEqual( {'check_true': False}, settings_loader.load_engine_options(TEST_ENGINE_OPTIONS, options_file))
def test_load_engine_options__modified_options__after_save__loads_modified_options(self): with tempfile.NamedTemporaryFile(mode='r+') as options_file: modified_options: engine.ConfigMapping = { 'string_none': 'test', 'spin': 100, 'combo': 'two', 'check_true': False } options_file.writelines(settings_loader.engine_options_file_lines(TEST_ENGINE_OPTIONS, modified_options)) options_file.seek(0) options = settings_loader.load_engine_options(TEST_ENGINE_OPTIONS, options_file) self.assertEqual(modified_options, options)
def load_all_engine_options(settings: Json) -> Dict[str, engine.ConfigMapping]: """Loads engine options.""" engine_options: Dict[str, engine.ConfigMapping] = {} for engine_setting in typing.cast(List[Json], settings['engines']): nickname = typing.cast(str, engine_setting['nickname']) path = typing.cast(str, engine_setting['path']) with engine.SimpleEngine.popen_uci(path) as uci_engine: default_options = [option for _, option in uci_engine.options.items()] engine_options_directory = settings['engine_options_directory'] options_file_path = f'{engine_options_directory}\\{nickname}.uci' ensure_file_exists(options_file_path) with open(options_file_path, 'r+') as options_file: simple_options = settings_loader.load_engine_options_simple(options_file) settings_loader.check_engine_options(default_options, simple_options) options_file.seek(0) options_with_defaults = settings_loader.load_engine_options(default_options, options_file, False) options_file.seek(0) if simple_options != options_with_defaults: options_file.writelines( settings_loader.engine_options_file_lines(default_options, options_with_defaults)) options_file.seek(0) engine_options[nickname] = settings_loader.load_engine_options(default_options, options_file) return engine_options
def test_load_engine_options__edited_file__no_comment_is_ok(self): with tempfile.NamedTemporaryFile(mode='r+') as options_file: options_file.write('check_true=False\n') options_file.seek(0) self.assertEqual( {'check_true': False}, settings_loader.load_engine_options(TEST_ENGINE_OPTIONS, options_file))
def test_load_engine_options__exlucde_defaults_false__empty_file__loads_defaults(self): with tempfile.NamedTemporaryFile() as options_file: options = settings_loader.load_engine_options(TEST_ENGINE_OPTIONS, options_file, False) self.assertEqual(TEST_ENGINE_OPTIONS_DICT, options)
def test_load_engine_options__empty_file__loads_empty_options(self): with tempfile.NamedTemporaryFile() as options_file: options = settings_loader.load_engine_options(TEST_ENGINE_OPTIONS, options_file) self.assertEqual(dict(), options)