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

Function TestInclude

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

Source from the content-addressed store, hash-verified

276}
277
278func TestInclude(t *testing.T) {
279 for i, test := range []struct {
280 fileContent string
281 fileName string
282 shouldErr bool
283 expect string
284 args string
285 }{
286 {
287 // file exists, content is text only
288 fileContent: "text",
289 fileName: "file1",
290 shouldErr: false,
291 expect: "text",
292 },
293 {
294 // file exists, content is template
295 fileContent: "{{ if . }}text{{ end }}",
296 fileName: "file1",
297 shouldErr: false,
298 expect: "text",
299 },
300 {
301 // file does not exit
302 fileContent: "",
303 fileName: "",
304 shouldErr: true,
305 },
306 {
307 // args
308 fileContent: "{{ index .Args 0 }}",
309 fileName: "file1",
310 shouldErr: false,
311 args: "text",
312 expect: "text",
313 },
314 {
315 // args, reference arg out of range
316 fileContent: "{{ index .Args 1 }}",
317 fileName: "file1",
318 shouldErr: true,
319 args: "text",
320 },
321 } {
322 tplContext := getContextOrFail(t)
323 var absFilePath string
324
325 // create files for test case
326 if test.fileName != "" {
327 absFilePath := filepath.Join(fmt.Sprintf("%s", tplContext.Root), test.fileName)
328 if err := os.WriteFile(absFilePath, []byte(test.fileContent), os.ModePerm); err != nil {
329 os.Remove(absFilePath)
330 t.Fatalf("Test %d: Expected no error creating file, got: '%s'", i, err.Error())
331 }
332 }
333
334 // perform test
335 actual, err := tplContext.funcInclude(test.fileName, test.args)

Callers

nothing calls this directly

Calls 3

getContextOrFailFunction · 0.85
funcIncludeMethod · 0.80
ErrorMethod · 0.45

Tested by

no test coverage detected