From 829d4a12d9663adda06800570873d456c310a5d2 Mon Sep 17 00:00:00 2001 From: Ernst von Oelsen Date: Mon, 8 Apr 2024 17:16:31 +0200 Subject: [PATCH 1/2] Print full path rather than RelativePath only. Signed-off-by: Ernst von Oelsen --- pkg/cmd/template/data_values_flags.go | 2 +- pkg/files/sources.go | 2 +- pkg/workspace/template_loader.go | 2 +- test/e2e/e2e_test.go | 15 +++++++++++++++ 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/pkg/cmd/template/data_values_flags.go b/pkg/cmd/template/data_values_flags.go index 325457b2..4f144cfe 100644 --- a/pkg/cmd/template/data_values_flags.go +++ b/pkg/cmd/template/data_values_flags.go @@ -168,7 +168,7 @@ func (s *DataValuesFlags) file(fullPath string, strict bool) ([]*datavalues.Enve } docSet, err := yamlmeta.NewDocumentSetFromBytes(contents, docSetOpts) if err != nil { - return nil, fmt.Errorf("Unmarshaling YAML data values file '%s': %s", dvFile.RelativePath(), err) + return nil, fmt.Errorf("Unmarshaling YAML data values file '%s': %s", dvFile.Description(), err) } for _, doc := range docSet.Items { diff --git a/pkg/files/sources.go b/pkg/files/sources.go index 236d0f20..61ff85c9 100644 --- a/pkg/files/sources.go +++ b/pkg/files/sources.go @@ -55,7 +55,7 @@ type LocalSource struct { func NewLocalSource(path, dir string) LocalSource { return LocalSource{path, dir} } -func (s LocalSource) Description() string { return fmt.Sprintf("file '%s'", s.path) } +func (s LocalSource) Description() string { return fmt.Sprintf("file %s", s.path) } func (s LocalSource) RelativePath() (string, error) { if s.dir == "" { diff --git a/pkg/workspace/template_loader.go b/pkg/workspace/template_loader.go index 9073614c..ef6d7a26 100644 --- a/pkg/workspace/template_loader.go +++ b/pkg/workspace/template_loader.go @@ -149,7 +149,7 @@ func (l *TemplateLoader) EvalPlainYAML(file *files.File) (*yamlmeta.DocumentSet, docSet, err := yamlmeta.NewDocumentSetFromBytes(fileBs, docSetOpts) if err != nil { - return nil, fmt.Errorf("Unmarshaling YAML template '%s': %s", file.RelativePath(), err) + return nil, fmt.Errorf("Unmarshaling YAML template '%s': %s", file.Description(), err) } return docSet, nil diff --git a/test/e2e/e2e_test.go b/test/e2e/e2e_test.go index a950ab2e..a7a5b3a3 100644 --- a/test/e2e/e2e_test.go +++ b/test/e2e/e2e_test.go @@ -292,6 +292,21 @@ new_thing: new }) } +func TestProcessingFileWithError(t *testing.T) { + t.Run("invalidYamlFile", func(t *testing.T) { + tempInputDir, err := os.MkdirTemp(os.TempDir(), "ytt-check-dir") + require.NoError(t, err) + err = os.WriteFile(tempInputDir+"/config.yaml", []byte(`apiVersion: "v1" +key value`), 0666) + require.NoError(t, err) + defer os.Remove(tempInputDir) + + actualOutput := runYttExpectingError(t, testInputFiles{tempInputDir + "/config.yaml"}, nil, nil) + expectedOutput := "ytt: Error: Unmarshaling YAML template 'file " + tempInputDir + "/config.yaml': yaml: line 3: could not find expected ':'\n" + require.Equal(t, expectedOutput, actualOutput) + }) +} + func TestCheckDirectoryOutput(t *testing.T) { tempOutputDir, err := os.MkdirTemp(os.TempDir(), "ytt-check-dir") require.NoError(t, err) From c33ee2fb66a6cfb8c922e7db457b710dff17f9de Mon Sep 17 00:00:00 2001 From: Ernst von Oelsen Date: Mon, 8 Apr 2024 19:40:05 +0200 Subject: [PATCH 2/2] Add comment to exported method Description(). Signed-off-by: Ernst von Oelsen --- pkg/files/sources.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/files/sources.go b/pkg/files/sources.go index 61ff85c9..9c01f945 100644 --- a/pkg/files/sources.go +++ b/pkg/files/sources.go @@ -55,6 +55,7 @@ type LocalSource struct { func NewLocalSource(path, dir string) LocalSource { return LocalSource{path, dir} } +// Description return LocalSource's (i.e. file's) path func (s LocalSource) Description() string { return fmt.Sprintf("file %s", s.path) } func (s LocalSource) RelativePath() (string, error) {