Skip to content

Commit 8e29647

Browse files
authored
Fix execution with partial templates (#388)
Fix execution with partial templates
2 parents 4142242 + 4953b05 commit 8e29647

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

cmd/apply_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,47 @@ func TestApplyCommand(t *testing.T) {
8888
},
8989
},
9090
},
91+
{
92+
name: "define_template",
93+
root: map[string]interface{}{
94+
"/home/user/.local/share/chezmoi": map[string]interface{}{
95+
"dir/file.tmpl": `{{ define "foo" }}cont{{end}}{{ template "foo" }}ents`,
96+
},
97+
},
98+
},
99+
{
100+
name: "partial_template",
101+
root: map[string]interface{}{
102+
"/home/user/.local/share/chezmoi": map[string]interface{}{
103+
"dir/file.tmpl": `{{ template "foo" }}ents`,
104+
".chezmoitemplates/foo": "{{ if true }}cont{{ end }}",
105+
},
106+
},
107+
},
108+
{
109+
name: "multiple_templates",
110+
root: map[string]interface{}{
111+
"/home/user/.local/share/chezmoi": map[string]interface{}{
112+
"dir/file.tmpl": `{{ template "foo" }}`,
113+
"dir/other.tmpl": `{{ if true }}other stuff{{ end }}`,
114+
".chezmoitemplates/foo": "{{ if true }}contents{{ end }}",
115+
},
116+
},
117+
},
118+
{
119+
name: "multiple_associated",
120+
root: map[string]interface{}{
121+
"/home/user/.local/share/chezmoi": map[string]interface{}{
122+
"dir/file.tmpl": `{{ template "foo" }}{{ template "bar" }}`,
123+
".chezmoitemplates/foo": "{{ if true }}cont{{ end }}",
124+
".chezmoitemplates/bar": "{{ if true }}ents{{ end }}",
125+
},
126+
},
127+
},
91128
} {
92129
t.Run(tc.name, func(t *testing.T) {
93130
tc.root["/home/user/.local/share/chezmoi/dir/file"] = "contents"
131+
tc.root["/home/user/.local/share/chezmoi/dir/other"] = "other stuff"
94132
tc.root["/home/user/.local/share/chezmoi/symlink_symlink"] = "target"
95133
fs, cleanup, err := vfst.NewTestFS(tc.root)
96134
require.NoError(t, err)
@@ -111,6 +149,11 @@ func TestApplyCommand(t *testing.T) {
111149
vfst.TestModePerm(0644),
112150
vfst.TestContentsString("contents"),
113151
),
152+
vfst.TestPath("/home/user/dir/other",
153+
vfst.TestModeIsRegular,
154+
vfst.TestModePerm(0644),
155+
vfst.TestContentsString("other stuff"),
156+
),
114157
vfst.TestPath("/home/user/symlink",
115158
vfst.TestModeType(os.ModeSymlink),
116159
vfst.TestSymlinkTarget("target"),

lib/chezmoi/targetstate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ func (ts *TargetState) executeTemplateData(name string, data []byte) ([]byte, er
660660
}
661661
}
662662
output := &bytes.Buffer{}
663-
if err = tmpl.Execute(output, ts.Data); err != nil {
663+
if err = tmpl.ExecuteTemplate(output, name, ts.Data); err != nil {
664664
return nil, err
665665
}
666666
return output.Bytes(), nil

0 commit comments

Comments
 (0)