From d267887346095b2e6f2fc4c16ffbe31b38ad0e99 Mon Sep 17 00:00:00 2001 From: Max Brauer Date: Sat, 17 Sep 2022 12:59:45 +0200 Subject: [PATCH 1/4] Allow FileTests to have trailing empty lines This makes it easier to author template tests, because editors tend to append a newline when saving files. --- test/filetests/filetests.go | 24 ++++++++------ test/filetests/filetests_test.go | 56 ++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 10 deletions(-) create mode 100644 test/filetests/filetests_test.go diff --git a/test/filetests/filetests.go b/test/filetests/filetests.go index 4535e066..b31965ec 100644 --- a/test/filetests/filetests.go +++ b/test/filetests/filetests.go @@ -93,12 +93,12 @@ func (f FileTests) Run(t *testing.T) { } else { resultStr := testErr.UserErr().Error() resultStr = regexp.MustCompile("__ytt_tpl\\d+_").ReplaceAllString(resultStr, "__ytt_tplXXX_") - resultStr = f.trimTrailingWhitespace(resultStr) + resultStr = TrimTrailingMultilineWhitespace(resultStr) expectedStr = strings.TrimPrefix(expectedStr, "ERR:") expectedStr = strings.TrimPrefix(expectedStr, " ") expectedStr = strings.ReplaceAll(expectedStr, "__YTT_VERSION__", version.Version) - expectedStr = f.trimTrailingWhitespace(expectedStr) + expectedStr = TrimTrailingMultilineWhitespace(expectedStr) err = f.expectEquals(resultStr, expectedStr) } case strings.HasPrefix(expectedStr, "OUTPUT POSITION:"): @@ -134,14 +134,6 @@ func (f FileTests) Run(t *testing.T) { } } -func (f FileTests) trimTrailingWhitespace(multiLineString string) string { - var newLines []string - for _, line := range strings.Split(multiLineString, "\n") { - newLines = append(newLines, strings.TrimRight(line, "\t ")) - } - return strings.Join(newLines, "\n") -} - func (f FileTests) asFilePositionsStr(result MarshalableResult) (string, error) { printerFunc := func(w io.Writer) yamlmeta.DocumentPrinter { return yamlmeta.WrappedFilePositionPrinter{yamlmeta.NewFilePositionPrinter(w)} @@ -250,3 +242,15 @@ func (l DefaultTemplateLoader) Load(thread *starlark.Thread, module string) (sta yttlibrary.NewDataModule(&l.DataValues, nil), nil, nil) return api.FindModule(strings.TrimPrefix(module, "@ytt:")) } + +// TrimTrailingMultilineWhitespace returns a string with trailing whitespace trimmed from every line as well +// as trimmed trailing empty lines +func TrimTrailingMultilineWhitespace(s string) string { + var trimmedLines []string + for _, line := range strings.Split(s, "\n") { + trimmedLine := strings.TrimRight(line, "\t ") + trimmedLines = append(trimmedLines, trimmedLine) + } + multiline := strings.Join(trimmedLines, "\n") + return strings.TrimRight(multiline, "\n") +} diff --git a/test/filetests/filetests_test.go b/test/filetests/filetests_test.go new file mode 100644 index 00000000..9a949c21 --- /dev/null +++ b/test/filetests/filetests_test.go @@ -0,0 +1,56 @@ +package filetests + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestTrimTrailingMultilineWhitespace(t *testing.T) { + for _, testcase := range []struct { + give, want string + }{ + { + give: `we want yaml`, + want: `we want yaml`, + }, + { + give: `we want yaml `, + want: `we want yaml`, + }, + { + give: `we want yaml `, + want: `we want yaml`, + }, + { + give: `we want yaml +`, + want: `we want yaml`, + }, + { + give: ` +we +want +yaml `, + want: ` +we +want +yaml`, + }, + { + give: ` +we + + want + yaml + +`, + want: ` +we + + want + yaml`, + }, + } { + assert.Equal(t, testcase.want, TrimTrailingMultilineWhitespace(testcase.give)) + } +} From cecc394bd8a1d6f17064031d51f1bccd172e2631 Mon Sep 17 00:00:00 2001 From: Max Brauer Date: Sat, 17 Sep 2022 17:17:34 +0200 Subject: [PATCH 2/4] Add example to FileTests doc --- test/filetests/filetests.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/filetests/filetests.go b/test/filetests/filetests.go index b31965ec..e1b9ad29 100644 --- a/test/filetests/filetests.go +++ b/test/filetests/filetests.go @@ -40,6 +40,17 @@ type EvaluateTemplate func(src string) (MarshalableResult, *TestErr) // - expected output starting with `ERR:` indicate that expected output is an error message // - expected output starting with `OUTPUT POSITION:` indicate that expected output is "pos" format // - otherwise expected output is the literal output from template +// +// For example: +// +// #! my-test.tpltest +// --- +// #@ msg = "hello" +// msg: #@ msg +// +++ +// +// msg: hello +// type FileTests struct { PathToTests string EvalFunc EvaluateTemplate From 46dfd50a042ed02f5ba1f17c85be44da7f92547d Mon Sep 17 00:00:00 2001 From: "John S. Ryan" Date: Thu, 22 Sep 2022 16:18:03 -0700 Subject: [PATCH 3/4] Add required license header --- test/filetests/filetests_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/filetests/filetests_test.go b/test/filetests/filetests_test.go index 9a949c21..0ab946d7 100644 --- a/test/filetests/filetests_test.go +++ b/test/filetests/filetests_test.go @@ -1,3 +1,6 @@ +// Copyright 2022 VMware, Inc. +// SPDX-License-Identifier: Apache-2.0 + package filetests import ( From 13821564ffb3139e6a05ad5529683b222e325e5f Mon Sep 17 00:00:00 2001 From: "John S. Ryan" Date: Thu, 22 Sep 2022 16:19:05 -0700 Subject: [PATCH 4/4] Add linter-required package documentation --- test/filetests/filetests.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/filetests/filetests.go b/test/filetests/filetests.go index e1b9ad29..0e151f8f 100644 --- a/test/filetests/filetests.go +++ b/test/filetests/filetests.go @@ -1,6 +1,10 @@ // Copyright 2022 VMware, Inc. // SPDX-License-Identifier: Apache-2.0 +/* +Package filetests houses a test harness for evaluating templates and asserting +the expected output. +*/ package filetests import (