def get_repo_config(self, branch='master', path_to_file='pyproject.toml'): """ Load configuration from the repository. Parameters ---------- branch : `str` The branch to read the config file from. (Will default to 'master') path_to_file : `str` Path to the ``pyproject.toml`` file in the repository. Will default to the root of the repository. Returns ------- cfg : `baldrick.config.Config` Configuration parameters. """ # Also default to 'master' if branch is None branch = branch or 'master' app_config = current_app.conf.copy() fallback_config = Config() repo_config = Config() try: file_content = self.get_file_contents(path_to_file, branch=branch) except FileNotFoundError: logger.debug(f"No config file found in {self.repo}@{branch}.") file_content = None if file_content: repo_config = loads(file_content, tool=current_app.bot_username) or {} logger.trace( f"Got the following config from {self.repo}@{branch}: {repo_config}" ) if len(repo_config) == 0: logger.exception( f"Failed to load config in {self.repo} on branch {branch}, despite finding a pyproject.toml file." ) if getattr(current_app, "fall_back_config", None): fallback_config = loads( file_content, tool=current_app.fall_back_config) or {} if len(fallback_config) == 0: logger.trace( f"Didn't find a fallback config in {self.repo}@{branch}." ) # Priority is 1) repo_config 2) fallback_config 3) app_config app_config.update_from_config(fallback_config) app_config.update_from_config(repo_config) logger.debug( f"Got this combined config from {self.repo}@{branch}: {app_config}" ) return app_config
def test_update_override(): config_global = loads(GLOBAL_TOML) config_repo = loads(REPO_TOML, tool='testbot') config_global.update_from_config(config_repo) assert config_global == {'plugin1': {'setting1': 'a', 'setting2': 'c'}, 'plugin2': {'setting3': 4, 'setting4': 1.5}, 'plugin3': {'setting5': 't'}}
def get_repo_config(self, branch=None, path_to_file='pyproject.toml', warn_on_failure=True): """ Load configuration from the repository. Parameters ---------- branch : `str` The branch to read the config file from. (Will default to 'master') path_to_file : `str` Path to the ``pyproject.toml`` file in the repository. Will default to the root of the repository. warn_on_failure : `bool` Emit warning on failure to load the pyproject file. Returns ------- cfg : `baldrick.config.Config` Configuration parameters. """ # Allow non-existent file but raise error when cannot parse try: file_content = self.get_file_contents(path_to_file, branch=branch) return loads(file_content, tool=current_app.bot_username) except Exception as e: # Attempt to load the fallback config just in case if getattr(current_app, "fall_back_config", None): try: return loads(file_content, tool=current_app.fall_back_config) except Exception: # pragma: no cover pass if warn_on_failure: warnings.warn(str(e)) # Empty dict means calling code set the default repo_config = current_app.conf.copy() return repo_config
def test_get_config_with_app_defaults(self, app): with app.app_context(): with patch.object(self.repo, 'get_file_contents') as mock_get: # noqa mock_get.return_value = TEST_CONFIG # These are set to False in YAML; defaults must not be used. assert self.repo.get_config_value('pr') == { 'setting1': 2, 'setting2': 3 } assert self.repo.get_config_value('other') is None app.conf = loads(TEST_GLOBAL_CONFIG, tool='testbot') assert self.repo.get_config_value('pr') == { 'setting1': 2, 'setting2': 3, 'setting3': 6 } assert self.repo.get_config_value('other') == {'setting4': 5}
def test_loads_invalid_tool(): with pytest.raises(KeyError): loads(GLOBAL_TOML, tool='testbot')
def test_load(tmpdir): filename = tmpdir.join('pyproject.toml').strpath with open(filename, 'w') as f: f.write(GLOBAL_TOML) assert load(filename) == loads(GLOBAL_TOML)
def test_loads(): config = loads(GLOBAL_TOML) assert config == {'plugin1': {'setting1': 'a', 'setting2': 'b'}, 'plugin2': {'setting3': 1}}
def test_loads_invalid_tool(): conf = loads(GLOBAL_TOML, tool='testbot') assert conf is None