Skip to content

Commit

Permalink
Fix execution with partial templates (#388)
Browse files Browse the repository at this point in the history
Fix execution with partial templates
  • Loading branch information
twpayne committed Jul 7, 2019
2 parents 4142242 + 4953b05 commit 8e29647
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
43 changes: 43 additions & 0 deletions cmd/apply_test.go
Expand Up @@ -88,9 +88,47 @@ func TestApplyCommand(t *testing.T) {
},
},
},
{
name: "define_template",
root: map[string]interface{}{
"/home/user/.local/share/chezmoi": map[string]interface{}{
"dir/file.tmpl": `{{ define "foo" }}cont{{end}}{{ template "foo" }}ents`,
},
},
},
{
name: "partial_template",
root: map[string]interface{}{
"/home/user/.local/share/chezmoi": map[string]interface{}{
"dir/file.tmpl": `{{ template "foo" }}ents`,
".chezmoitemplates/foo": "{{ if true }}cont{{ end }}",
},
},
},
{
name: "multiple_templates",
root: map[string]interface{}{
"/home/user/.local/share/chezmoi": map[string]interface{}{
"dir/file.tmpl": `{{ template "foo" }}`,
"dir/other.tmpl": `{{ if true }}other stuff{{ end }}`,
".chezmoitemplates/foo": "{{ if true }}contents{{ end }}",
},
},
},
{
name: "multiple_associated",
root: map[string]interface{}{
"/home/user/.local/share/chezmoi": map[string]interface{}{
"dir/file.tmpl": `{{ template "foo" }}{{ template "bar" }}`,
".chezmoitemplates/foo": "{{ if true }}cont{{ end }}",
".chezmoitemplates/bar": "{{ if true }}ents{{ end }}",
},
},
},
} {
t.Run(tc.name, func(t *testing.T) {
tc.root["/home/user/.local/share/chezmoi/dir/file"] = "contents"
tc.root["/home/user/.local/share/chezmoi/dir/other"] = "other stuff"
tc.root["/home/user/.local/share/chezmoi/symlink_symlink"] = "target"
fs, cleanup, err := vfst.NewTestFS(tc.root)
require.NoError(t, err)
Expand All @@ -111,6 +149,11 @@ func TestApplyCommand(t *testing.T) {
vfst.TestModePerm(0644),
vfst.TestContentsString("contents"),
),
vfst.TestPath("/home/user/dir/other",
vfst.TestModeIsRegular,
vfst.TestModePerm(0644),
vfst.TestContentsString("other stuff"),
),
vfst.TestPath("/home/user/symlink",
vfst.TestModeType(os.ModeSymlink),
vfst.TestSymlinkTarget("target"),
Expand Down
2 changes: 1 addition & 1 deletion lib/chezmoi/targetstate.go
Expand Up @@ -660,7 +660,7 @@ func (ts *TargetState) executeTemplateData(name string, data []byte) ([]byte, er
}
}
output := &bytes.Buffer{}
if err = tmpl.Execute(output, ts.Data); err != nil {
if err = tmpl.ExecuteTemplate(output, name, ts.Data); err != nil {
return nil, err
}
return output.Bytes(), nil
Expand Down

0 comments on commit 8e29647

Please sign in to comment.