def test_read_pyproject_toml(value, ret_config, default_map, exp_ret, exp_defaults, mocker, monkeypatch): """Parse config from a given pyproject.toml file.""" monkeypatch.setattr(config, "find_pyproject_toml", lambda x: "pyproject.toml") monkeypatch.setattr(config, "parse_pyproject_toml", lambda x: ret_config) ctx = mocker.Mock(default_map=default_map, params={}) actual = config.read_pyproject_toml(ctx, "config", value) assert exp_ret == actual assert exp_defaults == ctx.default_map
def test_read_pyproject_toml_raises(mocker, monkeypatch): """Handle expected exceptions while reading pyproject.toml, if any.""" with pytest.raises(AssertionError, match="Invalid parameter type passed"): config.read_pyproject_toml({}, "foo", True) with pytest.raises(AssertionError, match="Invalid parameter type passed"): config.read_pyproject_toml({}, "foo", 123) toml_error = toml.TomlDecodeError("toml error", doc="foo", pos=0) os_error = OSError("os error") mock_parse_pyproject_toml = mocker.Mock() mock_parse_pyproject_toml.side_effect = (toml_error, os_error) monkeypatch.setattr(config, "parse_pyproject_toml", mock_parse_pyproject_toml) error_msg = "Error reading configuration file: toml error" with pytest.raises(click.FileError, match=error_msg): config.read_pyproject_toml({}, "foo", "bar") error_msg = "Error reading configuration file: os error" with pytest.raises(click.FileError, match=error_msg): config.read_pyproject_toml({}, "foo", "bar")
def test_read_pyproject_toml_none(mocker, monkeypatch): """Return nothing if no pyproject.toml is found.""" monkeypatch.setattr(config, "find_pyproject_toml", lambda x: None) ctx = mocker.Mock() actual = config.read_pyproject_toml(ctx, "config", None) assert actual is None