From 5e3c7d7eb86a9fe6aad9174434415b9c42e78478 Mon Sep 17 00:00:00 2001 From: Anshul Verma Date: Thu, 23 Jan 2020 17:16:18 +0530 Subject: [PATCH] Friendly error message for non-existent Chart while packaging (#7127) * Fixes #6713 Friedly error message Signed-off-by: Anshul Verma * Fixes #6713 Friedly error message Signed-off-by: Anshul Verma * chaging error to no such file or directory Signed-off-by: Anshul Verma * changed it for the wider stat error messages which comes directly from go standard library. Signed-off-by: Anshul Verma * changed it for the wider stat error messages which comes directly from go standard library. Signed-off-by: Anshul Verma --- cmd/helm/package.go | 4 ++++ cmd/helm/package_test.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/cmd/helm/package.go b/cmd/helm/package.go index 9f7961f95..20cde6e4c 100644 --- a/cmd/helm/package.go +++ b/cmd/helm/package.go @@ -20,6 +20,7 @@ import ( "fmt" "io" "io/ioutil" + "os" "path/filepath" "github.com/pkg/errors" @@ -80,6 +81,9 @@ func newPackageCmd(out io.Writer) *cobra.Command { if err != nil { return err } + if _, err := os.Stat(args[i]); err != nil { + return err + } if client.DependencyUpdate { downloadManager := &downloader.Manager{ diff --git a/cmd/helm/package_test.go b/cmd/helm/package_test.go index 739857fa9..38938b4af 100644 --- a/cmd/helm/package_test.go +++ b/cmd/helm/package_test.go @@ -296,6 +296,41 @@ func TestPackageValues(t *testing.T) { } } +func TestNonExistentDirAndBadPermission(t *testing.T) { + nonExistentDir := "testdata/testcharts/non-existent-directory" + + tests := []struct { + name string + flags map[string]string + args []string + expect string + hasfile string + err bool + }{ + { + name: "package testdata/testcharts/non-existent-directory", + args: []string{"testdata/testcharts/non-existent-directory"}, + expect: fmt.Sprintf("stat %s: no such file or directory", nonExistentDir), + err: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + var buf bytes.Buffer + c := newPackageCmd(&buf) + re := regexp.MustCompile(tt.expect) + err := c.RunE(c, tt.args) + if err != nil { + if tt.err && re.MatchString(err.Error()) { + return + } + t.Fatalf("%q: expected error %q, got %q", tt.name, tt.expect, err) + } + }) + } +} + func createValuesFile(t *testing.T, data string) string { outputDir := ensure.TempDir(t)