Skip to content

Commit

Permalink
Add ability to assert positions in template_test
Browse files Browse the repository at this point in the history
Co-authored-by: Garrett Cheadle <[email protected]>
  • Loading branch information
cari-lynn and gcheadle-vmware committed Apr 6, 2021
1 parent 701a3b3 commit 7035b29
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,12 @@ test1
---
+++

ASSERT LINE NUM:

stdin:20 | [doc]
OUTPUT POSITION: stdin:20 | [doc]
| test1
stdin:5 | [doc]
stdin:6 | apiVersion: extensions/v1beta1
stdin:7 | kind: Ingress
stdin:16 | metadata:
stdin:8 | metadata:
stdin:9 | name: example-ingress1
stdin:17 | annotations:
stdin:10 | annotations:
stdin:18 | key: 2
30 changes: 27 additions & 3 deletions pkg/yamltemplate/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package yamltemplate_test

import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
Expand Down Expand Up @@ -67,10 +68,15 @@ func TestYAMLTemplate(t *testing.T) {
if len(pieces) != 2 {
t.Fatalf("expected file %s to include +++ separator", filePath)
}

resultStr, testErr := evalTemplate(t, pieces[0])
expectedStr := pieces[1]

includePositions := false
if strings.HasPrefix(expectedStr, "OUTPUT POSITION:") {
includePositions = true
}

resultStr, testErr := evalTemplate(t, pieces[0], includePositions)

if strings.HasPrefix(expectedStr, "ERR: ") {
if testErr == nil {
err = fmt.Errorf("expected eval error, but did not receive it")
Expand All @@ -79,6 +85,12 @@ func TestYAMLTemplate(t *testing.T) {
resultStr = regexp.MustCompile("__ytt_tpl\\d+_").ReplaceAllString(resultStr, "__ytt_tplXXX_")
err = expectEquals(t, resultStr, strings.ReplaceAll(strings.TrimPrefix(expectedStr, "ERR: "), "__YTT_VERSION__", version.Version))
}
} else if includePositions {
if testErr == nil {
err = expectEquals(t, resultStr, strings.ReplaceAll(strings.TrimPrefix(expectedStr, "OUTPUT POSITION:"), "__YTT_VERSION__", version.Version))
} else {
err = testErr.TestErr()
}
} else {
if testErr == nil {
err = expectEquals(t, resultStr, expectedStr)
Expand Down Expand Up @@ -116,7 +128,7 @@ type testErr struct {
func (e testErr) UserErr() error { return e.realErr }
func (e testErr) TestErr() error { return e.testErr }

func evalTemplate(t *testing.T, data string) (string, *testErr) {
func evalTemplate(t *testing.T, data string, includePositions bool) (string, *testErr) {
docSet, err := yamlmeta.NewDocumentSetFromBytes([]byte(data), yamlmeta.DocSetOpts{AssociatedName: "stdin"})
if err != nil {
return "", &testErr{err, fmt.Errorf("unmarshal error: %v", err)}
Expand Down Expand Up @@ -152,6 +164,18 @@ func evalTemplate(t *testing.T, data string) (string, *testErr) {
typedNewVal.(*yamlmeta.DocumentSet).Print(os.Stdout)
}

if includePositions {
printerFunc := func(w io.Writer) yamlmeta.DocumentPrinter {
return yamlmeta.WrappedFilePositionPrinter{yamlmeta.NewFilePositionPrinter(w)}
}
documentSet := typedNewVal.(*yamlmeta.DocumentSet)
combinedDocBytes, err := documentSet.AsBytesWithPrinter(printerFunc)
if err != nil {
return "", &testErr{err, fmt.Errorf("expected result docSet to be printable: %v", err)}
}
return string(combinedDocBytes), nil
}

resultBytes, err := typedNewVal.AsBytes()
if err != nil {
return "", &testErr{err, fmt.Errorf("marshal error: %v", err)}
Expand Down

0 comments on commit 7035b29

Please sign in to comment.