Skip to content

Commit

Permalink
properly support plain-yaml file mark -- bypass templating facility e…
Browse files Browse the repository at this point in the history
…ntirely
  • Loading branch information
cppforlife committed Jul 2, 2019
1 parent c6e9032 commit 26156e1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
39 changes: 39 additions & 0 deletions pkg/cmd/template/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,3 +392,42 @@ yamlfunc yamlfunc
t.Fatalf("Expected RunWithFiles to fail with error, but was '%s'", out.Err.Error())
}
}

func TestPlainYAMLNoTemplateProcessing(t *testing.T) {
yamlTplData := []byte(`
#@ load("funcs/funcs.lib.yml", "yamlfunc")
annotation: 5 #@ 1 + 2
text_template: (@= "string" @)`)

expectedYAMLTplData := `annotation: 5
text_template: (@= "string" @)
`

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

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.yml" {
t.Fatalf("Expected output file to be tpl.yml, but was %#v", file.RelativePath())
}

if string(file.Bytes()) != expectedYAMLTplData {
t.Fatalf("Expected output file to have specific data, but was: >>>%s<<<", file.Bytes())
}
}
4 changes: 4 additions & 0 deletions pkg/workspace/template_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ func (l *TemplateLoader) EvalYAML(library *Library, file *files.File) (starlark.
l.ui.Debugf("### ast\n")
docSet.Print(l.ui.DebugWriter())

if !file.IsTemplate() && !file.IsLibrary() {
return nil, docSet, nil
}

tplOpts := yamltemplate.TemplateOpts{IgnoreUnknownComments: l.opts.IgnoreUnknownComments}

compiledTemplate, err := yamltemplate.NewTemplate(file.RelativePath(), tplOpts).Compile(docSet)
Expand Down

0 comments on commit 26156e1

Please sign in to comment.