feat: add more clear error message and Environment info for template (#413)

* feat: add more clear error message and Environment info for template

Signed-off-by: yxxhero <aiopsclub@163.com>

* Update pkg/state/state.go

Co-authored-by: Yusuke Kuoka <ykuoka@gmail.com>
Signed-off-by: yxxhero <aiopsclub@163.com>

Signed-off-by: yxxhero <aiopsclub@163.com>
Co-authored-by: Yusuke Kuoka <ykuoka@gmail.com>
This commit is contained in:
yxxhero 2022-10-07 09:56:32 +08:00 committed by GitHub
parent a8101e44c7
commit e06abe4620
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 59 additions and 4 deletions

View File

@ -3200,7 +3200,7 @@ func (st *HelmState) GenerateOutputFilePath(release *ReleaseSpec, outputFileTemp
t, err := template.New("output-file").Parse(outputFileTemplate)
if err != nil {
return "", fmt.Errorf("parsing output-file template")
return "", fmt.Errorf("parsing output-file template %q: %w", outputFileTemplate, err)
}
buf := &bytes.Buffer{}
@ -3213,8 +3213,9 @@ func (st *HelmState) GenerateOutputFilePath(release *ReleaseSpec, outputFileTemp
}
data := struct {
State state
Release *ReleaseSpec
State state
Release *ReleaseSpec
Environment *environment.Environment
}{
State: state{
BaseName: stateFileName,
@ -3222,7 +3223,8 @@ func (st *HelmState) GenerateOutputFilePath(release *ReleaseSpec, outputFileTemp
AbsPath: stateAbsPath,
AbsPathSHA1: sha1sum,
},
Release: release,
Release: release,
Environment: &st.Env,
}
if err := t.Execute(buf, data); err != nil {

View File

@ -8,8 +8,10 @@ import (
"testing"
"github.com/Masterminds/semver/v3"
"github.com/stretchr/testify/require"
"github.com/variantdev/vals"
"github.com/helmfile/helmfile/pkg/environment"
"github.com/helmfile/helmfile/pkg/exectest"
"github.com/helmfile/helmfile/pkg/filesystem"
"github.com/helmfile/helmfile/pkg/helmexec"
@ -2542,3 +2544,54 @@ func Test_gatherUsernamePassword(t *testing.T) {
})
}
}
func TestGenerateOutputFilePath(t *testing.T) {
tests := []struct {
envName string
filePath string
releaseName string
outputFileTemplate string
wantErr bool
expected string
}{
{
envName: "dev",
releaseName: "release1",
filePath: "/path/to/helmfile.yaml",
outputFileTemplate: "helmfile-{{ .Environment.Name }}.yaml",
expected: "helmfile-dev.yaml",
},
{
envName: "error",
releaseName: "release2",
filePath: "helmfile.yaml",
outputFileTemplate: "helmfile-{{ .Environment.Name",
wantErr: true,
expected: "",
},
}
for _, tt := range tests {
t.Run(tt.envName, func(t *testing.T) {
st := &HelmState{
FilePath: tt.envName,
ReleaseSetSpec: ReleaseSetSpec{
Env: environment.Environment{
Name: tt.envName,
},
},
}
ra := &ReleaseSpec{
Name: tt.releaseName,
}
got, err := st.GenerateOutputFilePath(ra, tt.outputFileTemplate)
if tt.wantErr {
require.Errorf(t, err, "GenerateOutputFilePath() error = %v, want error", err)
} else {
require.NoError(t, err, "GenerateOutputFilePath() error = %v, want nil", err)
}
require.Equalf(t, got, tt.expected, "GenerateOutputFilePath() got = %v, want %v", got, tt.expected)
})
}
}