Skip to content

Commit

Permalink
buildconfig: error out on unknown toml keys
Browse files Browse the repository at this point in the history
This should remove some confusion when people use old bib with
recently added customizations.
  • Loading branch information
ondrejbudai authored and mvo5 committed Jul 29, 2024
1 parent e98d774 commit 8dd4cfd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
8 changes: 7 additions & 1 deletion bib/internal/buildconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,15 @@ func decodeTomlBuildConfig(r io.Reader, what string) (*BuildConfig, error) {
dec := toml.NewDecoder(r)

var conf BuildConfig
if _, err := dec.Decode(&conf); err != nil {
metadata, err := dec.Decode(&conf)
if err != nil {
return nil, fmt.Errorf("cannot decode %q: %w", what, err)
}

if len(metadata.Undecoded()) > 0 {
return nil, fmt.Errorf("cannot decode %q: unknown keys found: %v", what, metadata.Undecoded())
}

return &conf, nil
}

Expand Down
25 changes: 25 additions & 0 deletions bib/internal/buildconfig/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,28 @@ func TestReadLegacyJSONConfig(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, expectedBuildConfig, cfg)
}

func TestTomlUnknownKeysError(t *testing.T) {
fakeUserCnfPath := makeFakeConfig(t, "config.toml", `
[[birds]]
name = "toucan"
`)
_, err := buildconfig.ReadWithFallback(fakeUserCnfPath)

assert.ErrorContains(t, err, "unknown keys found: [birds birds.name]")
}

func TestJsonUnknownKeysError(t *testing.T) {
fakeUserCnfPath := makeFakeConfig(t, "config.json", `
{
"birds": [
{
"name": "toucan"
}
]
}
`)
_, err := buildconfig.ReadWithFallback(fakeUserCnfPath)

assert.ErrorContains(t, err, `json: unknown field "birds"`)
}

0 comments on commit 8dd4cfd

Please sign in to comment.