Skip to content

Commit

Permalink
Improve error message on malformed step (#3052)
Browse files Browse the repository at this point in the history
Signed-off-by: Kim Christensen <kimworking@gmail.com>
  • Loading branch information
kichristensen authored Apr 9, 2024
1 parent 76980c4 commit 7310b18
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
7 changes: 4 additions & 3 deletions pkg/manifest/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/cbroglie/mustache"
"github.com/cnabio/cnab-go/bundle"
"github.com/cnabio/cnab-go/bundle/definition"
"github.com/dustin/go-humanize"
"github.com/hashicorp/go-multierror"
"github.com/opencontainers/go-digest"
"go.opentelemetry.io/otel/attribute"
Expand Down Expand Up @@ -986,10 +987,10 @@ type BundleOutput struct {
type Steps []*Step

func (s Steps) Validate(m *Manifest) error {
for _, step := range s {
for i, step := range s {
err := step.Validate(m)
if err != nil {
return err
return fmt.Errorf("failed to validate %s step: %s", humanize.Ordinal(i+1), err)
}
}
return nil
Expand All @@ -1007,7 +1008,7 @@ func (s *Step) Validate(m *Manifest) error {
return errors.New("no mixin specified")
}
if len(s.Data) > 1 {
return errors.New("more than one mixin specified")
return errors.New("malformed step, possibly incorrect indentation")
}

mixinDeclared := false
Expand Down
14 changes: 7 additions & 7 deletions pkg/manifest/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func TestAction_Validate_RequireMixinDeclaration(t *testing.T) {
m.Mixins = []MixinDeclaration{}

err = m.Install.Validate(m)
assert.EqualError(t, err, "mixin (exec) was not declared")
assert.EqualError(t, err, "failed to validate 1st step: mixin (exec) was not declared")
}

func TestAction_Validate_RequireMixinData(t *testing.T) {
Expand All @@ -159,7 +159,7 @@ func TestAction_Validate_RequireMixinData(t *testing.T) {
m.Install[0].Data = nil

err = m.Install.Validate(m)
assert.EqualError(t, err, "no mixin specified")
assert.EqualError(t, err, "failed to validate 1st step: no mixin specified")
}

func TestAction_Validate_RequireSingleMixinData(t *testing.T) {
Expand All @@ -174,7 +174,7 @@ func TestAction_Validate_RequireSingleMixinData(t *testing.T) {
m.Install[0].Data["rando-mixin"] = ""

err = m.Install.Validate(m)
assert.EqualError(t, err, "more than one mixin specified")
assert.EqualError(t, err, "failed to validate 1st step: malformed step, possibly incorrect indentation")
}

func TestAction_Validate_RequireSingleMixinData_Actions(t *testing.T) {
Expand Down Expand Up @@ -211,7 +211,7 @@ func TestAction_Validate_RequireSingleMixinData_Actions(t *testing.T) {
(*step)[0].Data["rando-mixin"] = ""

err = m.Validate(ctx, c.Config)
assert.ErrorContains(t, err, "more than one mixin specified")
assert.ErrorContains(t, err, "malformed step, possibly incorrect indentation")
})
}
}
Expand All @@ -222,7 +222,7 @@ func TestManifest_Empty_Steps(t *testing.T) {
c.TestContext.AddTestFile("testdata/empty-steps.yaml", config.Name)

_, err := LoadManifestFrom(context.Background(), c.Config, config.Name)
assert.EqualError(t, err, "3 errors occurred:\n\t* validation of action \"install\" failed: found an empty step\n\t* validation of action \"uninstall\" failed: found an empty step\n\t* validation of action \"status\" failed: found an empty step\n\n")
assert.EqualError(t, err, "3 errors occurred:\n\t* validation of action \"install\" failed: failed to validate 2nd step: found an empty step\n\t* validation of action \"uninstall\" failed: failed to validate 2nd step: found an empty step\n\t* validation of action \"status\" failed: failed to validate 1st step: found an empty step\n\n")
}

func TestManifest_Validate_Name(t *testing.T) {
Expand All @@ -240,7 +240,7 @@ func TestManifest_Validate_Description(t *testing.T) {
c.TestContext.AddTestFile("testdata/porter-with-bad-description.yaml", config.Name)

_, err := LoadManifestFrom(context.Background(), c.Config, config.Name)
assert.ErrorContains(t, err, "validation of action \"install\" failed: invalid description type (string) for mixin step (exec)")
assert.ErrorContains(t, err, "validation of action \"install\" failed: failed to validate 1st step: invalid description type (string) for mixin step (exec)")
}

func TestManifest_Validate_InvalidType(t *testing.T) {
Expand All @@ -250,7 +250,7 @@ func TestManifest_Validate_InvalidType(t *testing.T) {

assert.NotPanics(t, func() {
_, err := LoadManifestFrom(context.Background(), c.Config, config.Name)
assert.ErrorContains(t, err, "validation of action \"install\" failed: invalid mixin type (string) for mixin step (exec)")
assert.ErrorContains(t, err, "validation of action \"install\" failed: failed to validate 1st step: invalid mixin type (string) for mixin step (exec)")
})
}

Expand Down

0 comments on commit 7310b18

Please sign in to comment.