Parse reference templates in predictable order (#7702)

* Parse reference templates in predictable order

Fix issue #7701

Signed-off-by: Andre Sencioles <asenci@gmail.com>

* Add test case for issue #7701 regression

Signed-off-by: Andre Sencioles <asenci@gmail.com>

* gofmt

Signed-off-by: Andre Sencioles <asenci@gmail.com>
pull/7963/head
Andre Sencioles 5 years ago committed by GitHub
parent b21b009785
commit d0726e07ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -213,6 +213,7 @@ func (e Engine) renderWithReferences(tpls, referenceTpls map[string]renderable)
// We want to parse the templates in a predictable order. The order favors
// higher-level (in file system) templates over deeply nested templates.
keys := sortTemplates(tpls)
referenceKeys := sortTemplates(referenceTpls)
for _, filename := range keys {
r := tpls[filename]
@ -223,8 +224,9 @@ func (e Engine) renderWithReferences(tpls, referenceTpls map[string]renderable)
// Adding the reference templates to the template context
// so they can be referenced in the tpl function
for filename, r := range referenceTpls {
for _, filename := range referenceKeys {
if t.Lookup(filename) == nil {
r := referenceTpls[filename]
if _, err := t.New(filename).Parse(r.tpl); err != nil {
return map[string]string{}, cleanupParseError(filename, err)
}

@ -126,6 +126,46 @@ func TestRender(t *testing.T) {
}
}
func TestRenderRefsOrdering(t *testing.T) {
parentChart := &chart.Chart{
Metadata: &chart.Metadata{
Name: "parent",
Version: "1.2.3",
},
Templates: []*chart.File{
{Name: "templates/_helpers.tpl", Data: []byte(`{{- define "test" -}}parent value{{- end -}}`)},
{Name: "templates/test.yaml", Data: []byte(`{{ tpl "{{ include \"test\" . }}" . }}`)},
},
}
childChart := &chart.Chart{
Metadata: &chart.Metadata{
Name: "child",
Version: "1.2.3",
},
Templates: []*chart.File{
{Name: "templates/_helpers.tpl", Data: []byte(`{{- define "test" -}}child value{{- end -}}`)},
},
}
parentChart.AddDependency(childChart)
expect := map[string]string{
"parent/templates/test.yaml": "parent value",
}
for i := 0; i < 100; i++ {
out, err := Render(parentChart, chartutil.Values{})
if err != nil {
t.Fatalf("Failed to render templates: %s", err)
}
for name, data := range expect {
if out[name] != data {
t.Fatalf("Expected %q, got %q (iteraction %d)", data, out[name], i+1)
}
}
}
}
func TestRenderInternals(t *testing.T) {
// Test the internals of the rendering tool.

Loading…
Cancel
Save