Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error message on malformed step #3052

Merged
merged 4 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: This package seems to be not very maintained, but it doesn't seem to really need a lot. We've been using it already, so it's really nbd, but with recent security things going on I think I'm just hyper-aware for a bit :)
Just thinking out loud here.

"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
Loading