Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 16 additions & 50 deletions .mise.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions docs/funcs/file.SetMode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
order: 1010
---

<!-- Generated by tools/docgen. DO NOT EDIT. -->

# file.SetMode

SetMode sets the provided mode on the current output file.

```go
{{- file.SetMode 0o755 }}
```
2 changes: 1 addition & 1 deletion docs/funcs/file.SetPath.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
order: 1010
order: 1011
---

<!-- Generated by tools/docgen. DO NOT EDIT. -->
Expand Down
2 changes: 1 addition & 1 deletion docs/funcs/file.Skip.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
order: 1011
order: 1012
---

<!-- Generated by tools/docgen. DO NOT EDIT. -->
Expand Down
2 changes: 1 addition & 1 deletion docs/funcs/file.Static.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
order: 1012
order: 1013
---

<!-- Generated by tools/docgen. DO NOT EDIT. -->
Expand Down
2 changes: 1 addition & 1 deletion docs/funcs/module.Call.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
order: 1013
order: 1014
---

<!-- Generated by tools/docgen. DO NOT EDIT. -->
Expand Down
2 changes: 1 addition & 1 deletion docs/funcs/module.Export.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
order: 1014
order: 1015
---

<!-- Generated by tools/docgen. DO NOT EDIT. -->
Expand Down
2 changes: 1 addition & 1 deletion docs/funcs/stencil.AddToModuleHook.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
order: 1015
order: 1016
---

<!-- Generated by tools/docgen. DO NOT EDIT. -->
Expand Down
2 changes: 1 addition & 1 deletion docs/funcs/stencil.ApplyTemplate.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
order: 1016
order: 1017
---

<!-- Generated by tools/docgen. DO NOT EDIT. -->
Expand Down
2 changes: 1 addition & 1 deletion docs/funcs/stencil.Arg.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
order: 1017
order: 1018
---

<!-- Generated by tools/docgen. DO NOT EDIT. -->
Expand Down
2 changes: 1 addition & 1 deletion docs/funcs/stencil.Debug.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
order: 1018
order: 1019
---

<!-- Generated by tools/docgen. DO NOT EDIT. -->
Expand Down
2 changes: 1 addition & 1 deletion docs/funcs/stencil.Exists.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
order: 1019
order: 1020
---

<!-- Generated by tools/docgen. DO NOT EDIT. -->
Expand Down
2 changes: 1 addition & 1 deletion docs/funcs/stencil.GetGlobal.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
order: 1020
order: 1021
---

<!-- Generated by tools/docgen. DO NOT EDIT. -->
Expand Down
2 changes: 1 addition & 1 deletion docs/funcs/stencil.GetModuleHook.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
order: 1021
order: 1022
---

<!-- Generated by tools/docgen. DO NOT EDIT. -->
Expand Down
2 changes: 1 addition & 1 deletion docs/funcs/stencil.Include.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
order: 1022
order: 1023
---

<!-- Generated by tools/docgen. DO NOT EDIT. -->
Expand Down
2 changes: 1 addition & 1 deletion docs/funcs/stencil.ReadBlocks.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
order: 1023
order: 1024
---

<!-- Generated by tools/docgen. DO NOT EDIT. -->
Expand Down
2 changes: 1 addition & 1 deletion docs/funcs/stencil.ReadDir.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
order: 1024
order: 1025
---

<!-- Generated by tools/docgen. DO NOT EDIT. -->
Expand Down
2 changes: 1 addition & 1 deletion docs/funcs/stencil.ReadFile.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
order: 1025
order: 1026
---

<!-- Generated by tools/docgen. DO NOT EDIT. -->
Expand Down
2 changes: 1 addition & 1 deletion docs/funcs/stencil.SetGlobal.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
order: 1026
order: 1027
---

<!-- Generated by tools/docgen. DO NOT EDIT. -->
Expand Down
110 changes: 42 additions & 68 deletions internal/codegen/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,94 +38,68 @@ var generatedBlockIndentTemplate string
var fakeGeneratedBlockIndentFile string

func TestSingleFileRender(t *testing.T) {
log := slogext.NewTestLogger(t)
fs, err := testmemfs.WithManifest("name: testing\n")
assert.NilError(t, err, "failed to testmemfs.WithManifest")
m, err := modulestest.NewWithFS(t.Context(), "testing", fs)
assert.NilError(t, err, "failed to NewWithFS")

tpl, err := NewTemplate(m, "virtual-file.tpl", 0o644, time.Now(), []byte("hello world!"), log, nil)
assert.NilError(t, err, "failed to create basic template")

sm := &configuration.Manifest{Name: "testing"}

st := NewStencil(sm, nil, []*modules.Module{m}, log, false)
err = tpl.Render(st, NewValues(t.Context(), sm, nil))
assert.NilError(t, err, "expected Render() to not fail")
tpl := RenderTemplate(t, nil, nil, "hello world!")
assert.Equal(t, len(tpl.Files), 1, "expected exactly one file to be rendered")
assert.Equal(t, tpl.Files[0].String(), "hello world!", "expected Render() to modify first created file")
}

func TestMultiFileRender(t *testing.T) {
log := slogext.NewTestLogger(t)
fs, err := testmemfs.WithManifest("name: testing\narguments:\n commands:\n type: list")
assert.NilError(t, err, "failed to testmemfs.WithManifest")
m, err := modulestest.NewWithFS(t.Context(), "testing", fs)
assert.NilError(t, err, "failed to NewWithFS")

tpl, err := NewTemplate(m, "multi-file.tpl", 0o644,
time.Now(), []byte(multiFileTemplate), log, nil)
assert.NilError(t, err, "failed to create template")

sm := &configuration.Manifest{Name: "testing", Arguments: map[string]any{
"commands": []string{"hello", "world", "command"},
}}

st := NewStencil(sm, nil, []*modules.Module{m}, log, false)
err = tpl.Render(st, NewValues(t.Context(), sm, nil))
assert.NilError(t, err, "expected Render() to not fail")
tpl := RenderTemplate(t,
&configuration.Manifest{
Arguments: map[string]any{
"commands": []string{"hello", "world", "command"},
},
},
&configuration.TemplateRepositoryManifest{
Arguments: map[string]configuration.Argument{
"commands": {},
},
}, multiFileTemplate,
)
assert.Equal(t, len(tpl.Files), 3, "expected Render() to create 3 files")

for i, f := range tpl.Files {
assert.Equal(t, f.String(), "command", "rendered template %d contents differred", i)
assert.Equal(t, f.String(), "command", "rendered template %d contents differed", i)
}
}

func TestMultiFileWithInputRender(t *testing.T) {
log := slogext.NewTestLogger(t)
fs, err := testmemfs.WithManifest("name: testing\narguments:\n commands:\n type: list")
assert.NilError(t, err, "failed to testmemfs.WithManifest")
m, err := modulestest.NewWithFS(t.Context(), "testing", fs)
assert.NilError(t, err, "failed to NewWithFS")

tpl, err := NewTemplate(m, "multi-file-input.tpl", 0o644,
time.Now(), []byte(multiFileInputTemplate), log, nil)
assert.NilError(t, err, "failed to create template")

sm := &configuration.Manifest{Name: "testing", Arguments: map[string]any{
"commands": []string{"hello", "world", "command"},
}}
mf := &configuration.Manifest{
Arguments: map[string]any{
"commands": []string{"hello", "world", "command"},
},
}

st := NewStencil(sm, nil, []*modules.Module{m}, log, false)
err = tpl.Render(st, NewValues(t.Context(), sm, nil))
assert.NilError(t, err, "expected Render() to not fail")
tpl := RenderTemplate(t,
mf,
&configuration.TemplateRepositoryManifest{
Arguments: map[string]configuration.Argument{
"commands": {},
},
}, multiFileInputTemplate,
)
assert.Equal(t, len(tpl.Files), 3, "expected Render() to create 3 files")

for i, f := range tpl.Files {
assert.Equal(t, (sm.Arguments["commands"].([]string))[i], f.String(), "rendered template %d contents differred", i)
assert.Equal(t, f.String(), mf.Arguments["commands"].([]string)[i], "rendered template %d contents differed", i)
}
}

func TestIncludeArgumentPassthrough(t *testing.T) {
log := slogext.NewTestLogger(t)
fs, err := testmemfs.WithManifest("name: testing\narguments:\n commands:\n type: list")
assert.NilError(t, err, "failed to testmemfs.WithManifest")
m, err := modulestest.NewWithFS(t.Context(), "testing", fs)
assert.NilError(t, err, "failed to NewWithFS")

tpl, err := NewTemplate(m, "apply-template-passthrough.tpl", 0o644,
time.Now(), []byte(includePassthroughTemplate), log, nil)
assert.NilError(t, err, "failed to create template")

sm := &configuration.Manifest{Name: "testing", Arguments: map[string]any{
"commands": []string{"hello", "world", "command"},
}}

st := NewStencil(sm, nil, []*modules.Module{m}, log, false)
err = tpl.Render(st, NewValues(t.Context(), sm, nil))
assert.NilError(t, err, "expected Render() to not fail")
tpl := RenderTemplate(t,
&configuration.Manifest{
Arguments: map[string]any{
"commands": []string{"hello", "world", "command"},
},
},
&configuration.TemplateRepositoryManifest{
Arguments: map[string]configuration.Argument{
"commands": {},
},
}, includePassthroughTemplate,
)
assert.Equal(t, len(tpl.Files), 1, "expected Render() to create 1 files")

assert.Equal(t, "testing", tpl.Files[0].String(), "rendered template contents differed")
assert.Equal(t, t.Name(), tpl.Files[0].String(), "rendered template contents differed")
}

func TestGeneratedBlock(t *testing.T) {
Expand Down
Loading
Loading