From ba596f7de14056c819683a904de42c9c5fac9ca0 Mon Sep 17 00:00:00 2001 From: Waldemar Quevedo Date: Fri, 28 Jul 2023 15:46:11 -0700 Subject: [PATCH] config: make parsing configurations without usable values invalid 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 --- conf/parse.go | 4 +++- conf/parse_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/conf/parse.go b/conf/parse.go index 612b1643..4d2a20ad 100644 --- a/conf/parse.go +++ b/conf/parse.go @@ -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 } diff --git a/conf/parse_test.go b/conf/parse_test.go index 4c8c07fb..3fc7c927 100644 --- a/conf/parse_test.go +++ b/conf/parse_test.go @@ -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") + } + } +}