From cfcf29e496de390cb41ef795d1b811dd6d4142a2 Mon Sep 17 00:00:00 2001 From: Antony Guinard Date: Fri, 20 Dec 2024 11:28:34 +0000 Subject: [PATCH] Add empty glob error when no files match --- internal/seclang/parser.go | 6 +++++- internal/seclang/parser_test.go | 5 +++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/internal/seclang/parser.go b/internal/seclang/parser.go index 2532d9eaf..2cd6d62fe 100644 --- a/internal/seclang/parser.go +++ b/internal/seclang/parser.go @@ -34,7 +34,8 @@ type Parser struct { // It will return error if any directive fails to parse // or the file does not exist. // If the path contains a *, it will be expanded to all -// files in the directory matching the pattern +// files in the directory matching the pattern. +// It will return an error if there are no files matching the pattern. func (p *Parser) FromFile(profilePath string) error { originalDir := p.currentDir @@ -45,6 +46,9 @@ func (p *Parser) FromFile(profilePath string) error { if err != nil { return fmt.Errorf("failed to glob: %s", err.Error()) } + if len(files) == 0 { + return fmt.Errorf("empty glob: %s does not match any file", profilePath) + } } else { files = append(files, profilePath) } diff --git a/internal/seclang/parser_test.go b/internal/seclang/parser_test.go index d2a0c5fe0..21514f2ac 100644 --- a/internal/seclang/parser_test.go +++ b/internal/seclang/parser_test.go @@ -102,6 +102,11 @@ func TestLoadConfigurationFile(t *testing.T) { if err != nil { t.Errorf("unexpected error: %s", err.Error()) } + + err = p.FromFile("./testdata/glob/*.comf") + if err == nil { + t.Errorf("expected an error as glob does not match any file") + } } // Connectors are supporting embedding github.com/corazawaf/coraza-coreruleset to ease CRS integration