Skip to content

Commit

Permalink
config: make parsing configurations without usable values invalid
Browse files Browse the repository at this point in the history
This makes configuration files that are empty, or read and processed
by the parser but with no detected values now return an error.

Signed-off-by: Waldemar Quevedo <wally@nats.io>
  • Loading branch information
wallyqs committed Aug 2, 2023
1 parent aa6ac2d commit ba596f7
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 ba596f7

Please sign in to comment.