Skip to content

Commit

Permalink
Build missing yaml message (#2995)
Browse files Browse the repository at this point in the history
* Create user-friendly error message on missing porter.yaml

Improve the error message when porter.yaml is missing during build

Signed-off-by: Kim Christensen <kimworking@gmail.com>

---------

Signed-off-by: Kim Christensen <kimworking@gmail.com>
  • Loading branch information
kichristensen authored Feb 13, 2024
1 parent f9116ae commit 754ecdd
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,5 @@ and we will add you. **All** contributors belong here. 💯
- [Xin Fu](https://github.com/imfing)
- [KallyDev](https://github.com/kallydev)
- [Salman Shah](https://github.com/sbshah97)
- [Ray Terrill](https://github.com/rayterrill)
- [Ray Terrill](https://github.com/rayterrill)
- [Kim Christensen](https://github.com/kichristensen)
2 changes: 2 additions & 0 deletions cmd/porter/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"
"testing"

"get.porter.sh/porter/pkg"
"get.porter.sh/porter/tests"

"get.porter.sh/porter/pkg/porter"
Expand Down Expand Up @@ -189,6 +190,7 @@ func TestBuildValidate_Driver(t *testing.T) {
// noop
return nil
}
p.FileSystem.WriteFile("porter.yaml", []byte(""), pkg.FileModeWritable)

err := rootCmd.Execute()
if tc.wantError == "" {
Expand Down
11 changes: 10 additions & 1 deletion pkg/porter/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,16 @@ func (o *BuildOptions) Validate(p *Porter) error {
return err
}

return o.BundleDefinitionOptions.Validate(p.Context)
err = o.BundleDefinitionOptions.Validate(p.Context)
if err != nil {
return err
}

if o.File == "" {
return fmt.Errorf("could not find porter.yaml in the current directory %s, make sure you are in the right directory or specify the porter manifest with --file", o.Dir)
}

return nil
}

func stringSliceContains(allowedValues []string, value string) bool {
Expand Down
27 changes: 27 additions & 0 deletions pkg/porter/build_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package porter

import (
"fmt"
"testing"

"get.porter.sh/porter/pkg"
"get.porter.sh/porter/pkg/manifest"
"get.porter.sh/porter/pkg/mixin"
"get.porter.sh/porter/pkg/pkgmgmt"
Expand Down Expand Up @@ -36,3 +38,28 @@ func TestPorter_GetUsedMixins(t *testing.T) {
assert.Len(t, results, 1)
assert.Equal(t, 1, testMixins.GetCalled("exec"), "expected the exec mixin to be called once")
}

func TestPorter_ErrorMessageOnMissingPorterYaml(t *testing.T) {
p := NewTestPorter(t)
defer p.Close()

o := BuildOptions{
BundleDefinitionOptions: BundleDefinitionOptions{},
}

err := o.Validate(p.Porter)
require.ErrorContains(t, err, fmt.Sprintf("could not find porter.yaml in the current directory %s, make sure you are in the right directory or specify the porter manifest with --file", o.Dir))
}

func TestPorter_NoErrorWhenPorterYamlIsPresent(t *testing.T) {
p := NewTestPorter(t)
defer p.Close()

o := BuildOptions{
BundleDefinitionOptions: BundleDefinitionOptions{},
}
p.FileSystem.WriteFile("porter.yaml", []byte(""), pkg.FileModeWritable)

err := o.Validate(p.Porter)
require.NoError(t, err, "validate BuildOptions failed")
}

0 comments on commit 754ecdd

Please sign in to comment.