Skip to content

Commit

Permalink
properly handle plain-text file mark for text templates
Browse files Browse the repository at this point in the history
  • Loading branch information
cppforlife committed Jul 2, 2019
1 parent 26156e1 commit 1b1b96c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
3 changes: 1 addition & 2 deletions pkg/cmd/template/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

cmdcore "github.com/k14s/ytt/pkg/cmd/core"
"github.com/k14s/ytt/pkg/files"
"github.com/k14s/ytt/pkg/texttemplate"
"github.com/k14s/ytt/pkg/workspace"
"github.com/k14s/ytt/pkg/yamlmeta"
"github.com/k14s/ytt/pkg/yamltemplate"
Expand Down Expand Up @@ -139,7 +138,7 @@ func (o *TemplateOptions) RunWithFiles(in TemplateInput, ui cmdcore.PlainUI) Tem
return TemplateOutput{Err: err}
}

resultStr := resultVal.(*texttemplate.NodeRoot).AsString()
resultStr := resultVal.AsString()

ui.Debugf("### %s result\n%s", fileInLib.File.RelativePath(), resultStr)
outputFiles = append(outputFiles, files.NewOutputFile(fileInLib.File.RelativePath(), []byte(resultStr)))
Expand Down
33 changes: 33 additions & 0 deletions pkg/cmd/template/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,3 +431,36 @@ text_template: (@= "string" @)
t.Fatalf("Expected output file to have specific data, but was: >>>%s<<<", file.Bytes())
}
}

func TestPlainTextNoTemplateProcessing(t *testing.T) {
txtTplData := []byte(`text (@= "string" @)`)
expectedTxtTplData := `text (@= "string" @)`

filesToProcess := []*files.File{
files.MustNewFileFromSource(files.NewBytesSource("tpl.txt", txtTplData)),
}

filesToProcess[0].MarkTemplate(false)

ui := cmdcore.NewPlainUI(false)
opts := cmdtpl.NewOptions()

out := opts.RunWithFiles(cmdtpl.TemplateInput{Files: filesToProcess}, ui)
if out.Err != nil {
t.Fatalf("Expected RunWithFiles to succeed, but was error: %s", out.Err)
}

if len(out.Files) != 1 {
t.Fatalf("Expected number of output files to be 1, but was %d", len(out.Files))
}

file := out.Files[0]

if file.RelativePath() != "tpl.txt" {
t.Fatalf("Expected output file to be tpl.txt, but was %#v", file.RelativePath())
}

if string(file.Bytes()) != expectedTxtTplData {
t.Fatalf("Expected output file to have specific data, but was: >>>%s<<<", file.Bytes())
}
}
9 changes: 6 additions & 3 deletions pkg/workspace/template_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func (l *TemplateLoader) EvalYAML(library *Library, file *files.File) (starlark.
return globals, resultVal, nil
}

func (l *TemplateLoader) EvalText(library *Library, file *files.File) (starlark.StringDict, interface{}, error) {
func (l *TemplateLoader) EvalText(library *Library, file *files.File) (starlark.StringDict, *texttemplate.NodeRoot, error) {
fileBs, err := file.Bytes()
if err != nil {
return nil, nil, err
Expand All @@ -186,7 +186,10 @@ func (l *TemplateLoader) EvalText(library *Library, file *files.File) (starlark.
l.ui.Debugf("## file %s\n", file.RelativePath())

if !file.IsTemplate() && !file.IsLibrary() {
return nil, string(fileBs), nil
plainRootNode := &texttemplate.NodeRoot{
Items: []interface{}{&texttemplate.NodeText{Content: string(fileBs)}},
}
return nil, plainRootNode, nil
}

textRoot, err := texttemplate.NewParser().Parse(fileBs, file.RelativePath())
Expand All @@ -210,7 +213,7 @@ func (l *TemplateLoader) EvalText(library *Library, file *files.File) (starlark.
return nil, nil, fmt.Errorf("Evaluating text template: %s", err)
}

return globals, resultVal, nil
return globals, resultVal.(*texttemplate.NodeRoot), nil
}

func (l *TemplateLoader) EvalStarlark(library *Library, file *files.File) (starlark.StringDict, error) {
Expand Down

0 comments on commit 1b1b96c

Please sign in to comment.