diff --git a/internal/pkg/variable/parser/parser_v2.go b/internal/pkg/variable/parser/parser_v2.go index cb9174b2..48c942e6 100644 --- a/internal/pkg/variable/parser/parser_v2.go +++ b/internal/pkg/variable/parser/parser_v2.go @@ -42,10 +42,13 @@ type ParserV2 struct { } func NewParserV2(cfg *config.ParserConfig) (*ParserV2, error) { + if cfg == nil { + return nil, errors.New("nil parser configuration") + } // Ensure the parent name is set, otherwise we can't parse correctly. - if cfg.ParentPackID == "" { - return nil, errors.New("variable parser config requires ParentName to be set") + if cfg.ParentPack == nil { + return nil, errors.New("nil ParentPack") } // Sort the file overrides to ensure variable merging is consistent on @@ -54,7 +57,7 @@ func NewParserV2(cfg *config.ParserConfig) (*ParserV2, error) { for _, file := range cfg.FileOverrides { _, err := os.Stat(file) if err != nil { - return nil, fmt.Errorf("variable file %q not found", file) + return nil, fmt.Errorf("error loading variable file %q: %w", file, err) } } diff --git a/internal/pkg/variable/parser/parser_v2_test.go b/internal/pkg/variable/parser/parser_v2_test.go index da773fc6..d0b99c59 100644 --- a/internal/pkg/variable/parser/parser_v2_test.go +++ b/internal/pkg/variable/parser/parser_v2_test.go @@ -36,6 +36,36 @@ func testpack(p ...string) *pack.Pack { } } +func TestParserV2_NewParserV2(t *testing.T) { + t.Run("fails/with nil config set", func(t *testing.T) { + p, err := NewParserV2(nil) + must.Nil(t, p) + must.Error(t, err) + must.ErrorContains(t, err, "nil parser configuration") + }) + t.Run("fails/without ParentPack set", func(t *testing.T) { + p, err := NewParserV2(&config.ParserConfig{}) + must.Nil(t, p) + must.Error(t, err) + must.ErrorContains(t, err, "nil ParentPack") + }) + t.Run("fails/with missing override file", func(t *testing.T) { + p, err := NewParserV2(&config.ParserConfig{ + ParentPack: testpack("example"), + FileOverrides: []string{"/not/a/real/path/foo.hcl"}, + }) + must.Nil(t, p) + must.Error(t, err) + must.ErrorContains(t, err, "error loading variable file") + }) + t.Run("passes", func(t *testing.T) { + p, err := NewParserV2(&config.ParserConfig{ + ParentPack: testpack("example"), + }) + must.NotNil(t, p) + must.NoError(t, err) + }) +} func TestParserV2_parseFlagVariable(t *testing.T) { testCases := []struct {