Skip to content

Commit

Permalink
Add tests for NewParserV2
Browse files Browse the repository at this point in the history
  • Loading branch information
angrycub committed Sep 22, 2023
1 parent 6dcb3f6 commit f7a8846
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
9 changes: 6 additions & 3 deletions internal/pkg/variable/parser/parser_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
}

Expand Down
30 changes: 30 additions & 0 deletions internal/pkg/variable/parser/parser_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit f7a8846

Please sign in to comment.