Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow template tests to have trailing empty lines #745

Merged
merged 4 commits into from Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 29 additions & 10 deletions 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 (
Expand Down Expand Up @@ -40,6 +44,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
Expand Down Expand Up @@ -93,12 +108,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:"):
Expand Down Expand Up @@ -134,14 +149,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)}
Expand Down Expand Up @@ -250,3 +257,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")
}
59 changes: 59 additions & 0 deletions test/filetests/filetests_test.go
@@ -0,0 +1,59 @@
// Copyright 2022 VMware, Inc.
// SPDX-License-Identifier: Apache-2.0

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