Skip to content

Commit

Permalink
config: make parsing configurations without usable values invalid (ba…
Browse files Browse the repository at this point in the history
…ckport) (#4358)

This makes configuration files that are empty, or read and processed by
the parser but with no detected values now return an error.

Fixes #4343 
Backport from dev branch
(#4347)
  • Loading branch information
wallyqs committed Aug 2, 2023
2 parents aa6ac2d + ba596f7 commit 7c9a91f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
4 changes: 3 additions & 1 deletion conf/parse.go
Expand Up @@ -146,7 +146,9 @@ func parse(data, fp string, pedantic bool) (p *parser, err error) {
return nil, err
}
}

if len(p.mapping) == 0 {
return nil, fmt.Errorf("config has no values or is empty")
}
return p, nil
}

Expand Down
27 changes: 27 additions & 0 deletions conf/parse_test.go
Expand Up @@ -403,3 +403,30 @@ func TestParserNoInfiniteLoop(t *testing.T) {
}
}
}

func TestParseWithNoValues(t *testing.T) {
for _, test := range []string{
``,
`aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`,
` aaaaaaaaaaaaaaaaaaaaaaaaaaa`,
` aaaaaaaaaaaaaaaaaaaaaaaaaaa `,
`
# just comments with no values
# is also invalid.
`,
`
# with comments and no spaces to create key values
# is also an invalid config.
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
`,
`
a,a,a,a,a,a,a,a,a,a,a
`,
} {
if _, err := parse(test, "", true); err == nil {
t.Fatal("expected an error")
} else if !strings.Contains(err.Error(), "config has no values or is empty") {
t.Fatal("expected invalid conf error")
}
}
}

0 comments on commit 7c9a91f

Please sign in to comment.