fix(action): strip file extensions from name

Signed-off-by: Matthew Fisher <matt.fisher@microsoft.com>
pull/6837/head
Matthew Fisher 5 years ago
parent 9ba574f130
commit bf012282c8
No known key found for this signature in database
GPG Key ID: 92AA783CBAAE8E3B

@ -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
}

@ -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)
})
}
}

Loading…
Cancel
Save