def test_load_settings_default_not_found(config_dir): parser = argparse.ArgumentParser() add_common_arguments(parser) options = parser.parse_args(['--config-dir', config_dir.strpath]) with pytest.raises(config.ConfigurationNotFound): load_settings(options)
def test_load_settings_invalid(config_dir): parser = argparse.ArgumentParser() add_common_arguments(parser) options = parser.parse_args([ '--config-dir', config_dir.strpath, '-c', 'config.cfg', ]) with pytest.raises(ValueError): # no [core] section load_settings(options)
def test_load_settings_default(config_dir): config_dir.join('default.cfg').write(TMP_CONFIG) parser = argparse.ArgumentParser() add_common_arguments(parser) options = parser.parse_args(['--config-dir', config_dir.strpath]) settings = load_settings(options) assert isinstance(settings, config.Config)
def test_load_settings(config_dir): config_dir.join('config.cfg').write(TMP_CONFIG) parser = argparse.ArgumentParser() add_common_arguments(parser) options = parser.parse_args([ '--config-dir', config_dir.strpath, '-c', 'config.cfg', ]) settings = load_settings(options) assert isinstance(settings, config.Config) assert settings.basename == 'config'
def test_load_settings_default_env_var(monkeypatch, config_dir, env_dir): monkeypatch.setenv('SOPEL_CONFIG', 'fromenv') monkeypatch.setenv('SOPEL_CONFIG_DIR', env_dir.strpath) config_dir.join('config.cfg').write(TMP_CONFIG) env_dir.join('fromenv.cfg').write(TMP_CONFIG) parser = argparse.ArgumentParser() add_common_arguments(parser) options = parser.parse_args([]) settings = load_settings(options) assert isinstance(settings, config.Config) assert settings.basename == 'fromenv' assert os.path.dirname(settings.filename) == env_dir.strpath
def test_load_settings_arg_priority_over_env(monkeypatch, config_dir, env_dir): monkeypatch.setenv('SOPEL_CONFIG', 'fromenv') monkeypatch.setenv('SOPEL_CONFIG_DIR', env_dir.strpath) config_dir.join('fromenv.cfg').write(TMP_CONFIG) config_dir.join('fromarg.cfg').write(TMP_CONFIG) parser = argparse.ArgumentParser() add_common_arguments(parser) options = parser.parse_args([ '--config-dir', config_dir.strpath, '-c', 'fromarg', ]) settings = load_settings(options) assert isinstance(settings, config.Config) assert settings.basename == 'fromarg' assert os.path.dirname(settings.filename) == config_dir.strpath
def get_configuration(options): """Get an instance of configuration from options. This may raise a ``sopel.config.ConfigurationError`` if the file is an invalid configuration file. """ try: bot_config = utils.load_settings(options) except ConfigurationNotFound as error: print( "Welcome to Sopel!\n" "I can't seem to find the configuration file, " "so let's generate it!\n") config_path = error.filename if not config_path.endswith('.cfg'): config_path = config_path + '.cfg' config_path = _create_config(config_path) # try to reload it now that it's created bot_config = Config(config_path) bot_config._is_daemonized = options.daemonize return bot_config