MCPcopy
hub / github.com/caddyserver/caddy / TestNestedInclude

Function TestNestedInclude

modules/caddyhttp/templates/tplcontext_test.go:190–276  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

188}
189
190func TestNestedInclude(t *testing.T) {
191 for i, test := range []struct {
192 child string
193 childFile string
194 parent string
195 parentFile string
196 shouldErr bool
197 expect string
198 child2 string
199 child2File string
200 }{
201 {
202 // include in parent
203 child: `{{ include "file1" }}`,
204 childFile: "file0",
205 parent: `{{ $content := "file2" }}{{ $p := include $content}}`,
206 parentFile: "file1",
207 shouldErr: false,
208 expect: ``,
209 child2: `This shouldn't show`,
210 child2File: "file2",
211 },
212 } {
213 context := getContextOrFail(t)
214 var absFilePath string
215 var absFilePath0 string
216 var absFilePath1 string
217 var buf *bytes.Buffer
218 var err error
219
220 // create files and for test case
221 if test.parentFile != "" {
222 absFilePath = filepath.Join(fmt.Sprintf("%s", context.Root), test.parentFile)
223 if err := os.WriteFile(absFilePath, []byte(test.parent), os.ModePerm); err != nil {
224 os.Remove(absFilePath)
225 t.Fatalf("Test %d: Expected no error creating file, got: '%s'", i, err.Error())
226 }
227 }
228 if test.childFile != "" {
229 absFilePath0 = filepath.Join(fmt.Sprintf("%s", context.Root), test.childFile)
230 if err := os.WriteFile(absFilePath0, []byte(test.child), os.ModePerm); err != nil {
231 os.Remove(absFilePath0)
232 t.Fatalf("Test %d: Expected no error creating file, got: '%s'", i, err.Error())
233 }
234 }
235 if test.child2File != "" {
236 absFilePath1 = filepath.Join(fmt.Sprintf("%s", context.Root), test.child2File)
237 if err := os.WriteFile(absFilePath1, []byte(test.child2), os.ModePerm); err != nil {
238 os.Remove(absFilePath0)
239 t.Fatalf("Test %d: Expected no error creating file, got: '%s'", i, err.Error())
240 }
241 }
242
243 buf = bufPool.Get().(*bytes.Buffer)
244 buf.Reset()
245 defer bufPool.Put(buf)
246 buf.WriteString(test.child)
247 err = context.executeTemplateInBuffer(test.childFile, buf)

Callers

nothing calls this directly

Calls 6

getContextOrFailFunction · 0.85
GetMethod · 0.65
ResetMethod · 0.65
ErrorMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected