github.com/hairyhenderson/gomplate/v4@v4.0.0-pre-2.0.20240520121557-362f058f0c93/internal/tests/integration/nested_templates_test.go (about)

     1  package integration
     2  
     3  import (
     4  	"testing"
     5  
     6  	"gotest.tools/v3/fs"
     7  )
     8  
     9  func setupNestedTemplatesTest(t *testing.T) *fs.Dir {
    10  	tmpDir := fs.NewDir(t, "gomplate-inttests",
    11  		fs.WithFile("hello.t", `Hello {{ . }}!`),
    12  		fs.WithDir("templates",
    13  			fs.WithFile("one.t", `{{ . }}`),
    14  			fs.WithFile("two.t", `{{ range $n := (seq 2) }}{{ $n }}: {{ $ }} {{ end }}`),
    15  		),
    16  	)
    17  	t.Cleanup(tmpDir.Remove)
    18  
    19  	return tmpDir
    20  }
    21  
    22  func TestNestedTemplates(t *testing.T) {
    23  	tmpDir := setupNestedTemplatesTest(t)
    24  
    25  	o, e, err := cmd(t,
    26  		"-t", "hello="+tmpDir.Join("hello.t"),
    27  		"-i", `{{ template "hello" "World"}}`,
    28  	).run()
    29  	assertSuccess(t, o, e, err, "Hello World!")
    30  
    31  	o, e, err = cmd(t, "-t", "hello.t",
    32  		"-i", `{{ template "hello.t" "World"}}`).
    33  		withDir(tmpDir.Path()).run()
    34  	assertSuccess(t, o, e, err, "Hello World!")
    35  
    36  	o, e, err = cmd(t, "-t", "templates/",
    37  		"-i", `{{ template "templates/one.t" "one"}}
    38  {{ template "templates/two.t" "two"}}`).
    39  		withDir(tmpDir.Path()).run()
    40  	assertSuccess(t, o, e, err, "one\n1: two 2: two ")
    41  
    42  	// referencing a dir without a trailing / is undocumented, but works
    43  	// currently - I don't want to break it...
    44  	o, e, err = cmd(t, "-t", "templates",
    45  		"-i", `{{ template "templates/one.t" "one"}}
    46  {{ template "templates/two.t" "two"}}`).
    47  		withDir(tmpDir.Path()).run()
    48  	assertSuccess(t, o, e, err, "one\n1: two 2: two ")
    49  }