diff --git a/pkg/action/install.go b/pkg/action/install.go index 606226500..8f1b5528b 100644 --- a/pkg/action/install.go +++ b/pkg/action/install.go @@ -564,6 +564,10 @@ func (i *Install) NameAndChart(args []string) (string, string, error) { if base == "." || base == "" { base = "chart" } + // if present, strip out the file extension from the name + if idx := strings.Index(base, "."); idx != -1 { + base = base[0:idx] + } return fmt.Sprintf("%s-%d", base, time.Now().Unix()), args[0], nil } diff --git a/pkg/action/install_test.go b/pkg/action/install_test.go index 406574995..bb36b843d 100644 --- a/pkg/action/install_test.go +++ b/pkg/action/install_test.go @@ -33,6 +33,7 @@ import ( kubefake "helm.sh/helm/v3/pkg/kube/fake" "helm.sh/helm/v3/pkg/release" "helm.sh/helm/v3/pkg/storage/driver" + "helm.sh/helm/v3/pkg/time" ) type nameTemplateTestCase struct { @@ -469,3 +470,99 @@ func TestInstallReleaseOutputDir(t *testing.T) { _, err = os.Stat(filepath.Join(dir, "hello/templates/empty")) is.True(os.IsNotExist(err)) } + +func TestNameAndChart(t *testing.T) { + is := assert.New(t) + instAction := installAction(t) + chartName := "./foo" + + name, chrt, err := instAction.NameAndChart([]string{chartName}) + if err != nil { + t.Fatal(err) + } + is.Equal(instAction.ReleaseName, name) + is.Equal(chartName, chrt) + + instAction.GenerateName = true + _, _, err = instAction.NameAndChart([]string{"foo", chartName}) + if err == nil { + t.Fatal("expected an error") + } + is.Equal("cannot set --generate-name and also specify a name", err.Error()) + + instAction.GenerateName = false + instAction.NameTemplate = "{{ . }}" + _, _, err = instAction.NameAndChart([]string{"foo", chartName}) + if err == nil { + t.Fatal("expected an error") + } + is.Equal("cannot set --name-template and also specify a name", err.Error()) + + instAction.NameTemplate = "" + instAction.ReleaseName = "" + _, _, err = instAction.NameAndChart([]string{chartName}) + if err == nil { + t.Fatal("expected an error") + } + is.Equal("must either provide a name or specify --generate-name", err.Error()) +} + +func TestNameAndChartGenerateName(t *testing.T) { + is := assert.New(t) + instAction := installAction(t) + + instAction.ReleaseName = "" + instAction.GenerateName = true + + tests := []struct { + Name string + Chart string + ExpectedName string + }{ + { + "local filepath", + "./chart", + fmt.Sprintf("chart-%d", time.Now().Unix()), + }, + { + "dot filepath", + ".", + fmt.Sprintf("chart-%d", time.Now().Unix()), + }, + { + "empty filepath", + "", + fmt.Sprintf("chart-%d", time.Now().Unix()), + }, + { + "packaged chart", + "chart.tgz", + fmt.Sprintf("chart-%d", time.Now().Unix()), + }, + { + "packaged chart with .tar.gz extension", + "chart.tar.gz", + fmt.Sprintf("chart-%d", time.Now().Unix()), + }, + { + "packaged chart with local extension", + "./chart.tgz", + fmt.Sprintf("chart-%d", time.Now().Unix()), + }, + } + + for _, tc := range tests { + tc := tc + t.Run(tc.Name, func(t *testing.T) { + t.Parallel() + + name, chrt, err := instAction.NameAndChart([]string{tc.Chart}) + if err != nil { + t.Fatal(err) + } + + is.Equal(tc.ExpectedName, name) + is.Equal(tc.Chart, chrt) + }) + } +}