diff --git a/pkg/app/app.go b/pkg/app/app.go index 3edf12c0..040b11f3 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -543,7 +543,12 @@ func (a *App) PrintState(c StateConfigProvider) error { return } - fmt.Printf("---\n# Source: %s\n\n%+v", run.state.FullFilePath(), stateYaml) + sourceFile, err := run.state.FullFilePath() + if err != nil { + errs = []error{err} + return + } + fmt.Printf("---\n# Source: %s\n\n%+v", sourceFile, stateYaml) errs = []error{} }) diff --git a/pkg/state/state.go b/pkg/state/state.go index 9d9da74c..474918c7 100644 --- a/pkg/state/state.go +++ b/pkg/state/state.go @@ -3357,6 +3357,11 @@ func (st *HelmState) getOCIChart(release *ReleaseSpec, tempDir string, helm helm return &chartPath, nil } -func (st *HelmState) FullFilePath() string { - return filepath.Join(st.basePath, st.FilePath) +func (st *HelmState) FullFilePath() (string, error) { + var wd string + var err error + if st.fs != nil { + wd, err = st.fs.Getwd() + } + return filepath.Join(wd, st.basePath, st.FilePath), err } diff --git a/pkg/state/state_test.go b/pkg/state/state_test.go index 375f4ff0..31e8717d 100644 --- a/pkg/state/state_test.go +++ b/pkg/state/state_test.go @@ -2694,9 +2694,11 @@ func TestGenerateOutputFilePath(t *testing.T) { } func TestFullFilePath(t *testing.T) { + fs := testhelper.NewTestFs(map[string]string{}) tests := []struct { basePath string filePath string + fs *filesystem.FileSystem expected string }{ { @@ -2714,6 +2716,18 @@ func TestFullFilePath(t *testing.T) { filePath: "helmfile.yaml", expected: "/test-2/helmfile.yaml", }, + { + basePath: "./test-3/", + filePath: "helmfile.yaml", + fs: fs.ToFileSystem(), + expected: "/path/to/test-3/helmfile.yaml", + }, + { + basePath: "/test-4/", + filePath: "helmfile.yaml", + fs: fs.ToFileSystem(), + expected: "/path/to/test-4/helmfile.yaml", + }, } for _, tt := range tests { @@ -2721,9 +2735,11 @@ func TestFullFilePath(t *testing.T) { st := &HelmState{ basePath: tt.basePath, FilePath: tt.filePath, + fs: tt.fs, } - actual := st.FullFilePath() + actual, err := st.FullFilePath() require.Equalf(t, actual, tt.expected, "FullFilePath() got = %v, want %v", actual, tt.expected) + require.Equalf(t, err, nil, "error %v", err) }) } } diff --git a/test/e2e/template/helmfile/testdata/snapshot/issue_2098_release_template_needs/output.yaml b/test/e2e/template/helmfile/testdata/snapshot/issue_2098_release_template_needs/output.yaml index 1a00807b..32490372 100644 --- a/test/e2e/template/helmfile/testdata/snapshot/issue_2098_release_template_needs/output.yaml +++ b/test/e2e/template/helmfile/testdata/snapshot/issue_2098_release_template_needs/output.yaml @@ -1,5 +1,5 @@ --- -# Source: input.yaml +# Source: /home/runner/work/helmfile/helmfile/test/e2e/template/helmfile/testdata/snapshot/issue_2098_release_template_needs/input.yaml filepath: input.yaml helmBinary: helm