From 797f746d5edf02c88d2c9dd08d002a67dec4253e Mon Sep 17 00:00:00 2001 From: Kim Christensen Date: Mon, 12 Feb 2024 20:37:21 +0100 Subject: [PATCH 1/3] 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 --- pkg/porter/build.go | 11 ++++++++++- pkg/porter/build_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/pkg/porter/build.go b/pkg/porter/build.go index 11778bc2d..37b1b8af6 100644 --- a/pkg/porter/build.go +++ b/pkg/porter/build.go @@ -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 { diff --git a/pkg/porter/build_test.go b/pkg/porter/build_test.go index 0aadfdcf6..af9bb46db 100644 --- a/pkg/porter/build_test.go +++ b/pkg/porter/build_test.go @@ -3,6 +3,7 @@ package porter import ( "testing" + "get.porter.sh/porter/pkg" "get.porter.sh/porter/pkg/manifest" "get.porter.sh/porter/pkg/mixin" "get.porter.sh/porter/pkg/pkgmgmt" @@ -36,3 +37,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, "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") +} + +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") +} From 353e2f98efe4bafbff118c039410ed4e26e4750d Mon Sep 17 00:00:00 2001 From: Kim Christensen Date: Fri, 9 Feb 2024 23:11:18 +0100 Subject: [PATCH 2/3] Add to CONTRIBUTORS Signed-off-by: Kim Christensen --- CONTRIBUTORS.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 598189841..eec120e36 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -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) \ No newline at end of file +- [Ray Terrill](https://github.com/rayterrill) +- [Kim Christensen](https://github.com/kichristensen) From c35d4b8d308252f9d9c772d7854d6da03e3f20ed Mon Sep 17 00:00:00 2001 From: Kim Christensen Date: Mon, 12 Feb 2024 23:58:55 +0100 Subject: [PATCH 3/3] Fix failing test Signed-off-by: Kim Christensen --- cmd/porter/bundle_test.go | 2 ++ pkg/porter/build_test.go | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/cmd/porter/bundle_test.go b/cmd/porter/bundle_test.go index 1879ef04b..dfb933fc2 100644 --- a/cmd/porter/bundle_test.go +++ b/cmd/porter/bundle_test.go @@ -6,6 +6,7 @@ import ( "strings" "testing" + "get.porter.sh/porter/pkg" "get.porter.sh/porter/tests" "get.porter.sh/porter/pkg/porter" @@ -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 == "" { diff --git a/pkg/porter/build_test.go b/pkg/porter/build_test.go index af9bb46db..723d5c0b9 100644 --- a/pkg/porter/build_test.go +++ b/pkg/porter/build_test.go @@ -1,6 +1,7 @@ package porter import ( + "fmt" "testing" "get.porter.sh/porter/pkg" @@ -47,7 +48,7 @@ func TestPorter_ErrorMessageOnMissingPorterYaml(t *testing.T) { } err := o.Validate(p.Porter) - require.ErrorContains(t, err, "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") + 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) {