Skip to content

Commit 9162b86

Browse files
authoredMar 18, 2021
Add slim-sprig template functions to bootstrap/generate (#775)
* Add sprig template functions to bootstrap/generate * switch to slim-sprig
1 parent 9fb7fe4 commit 9162b86

File tree

5 files changed

+77
-4
lines changed

5 files changed

+77
-4
lines changed
 

‎ginkgo/bootstrap_command.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"go/build"
1414

15+
sprig "github.com/go-task/slim-sprig"
1516
"github.com/onsi/ginkgo/ginkgo/nodot"
1617
)
1718

@@ -176,7 +177,7 @@ func generateBootstrap(agouti, noDot, internal bool, customBootstrapFile string)
176177
templateText = bootstrapText
177178
}
178179

179-
bootstrapTemplate, err := template.New("bootstrap").Parse(templateText)
180+
bootstrapTemplate, err := template.New("bootstrap").Funcs(sprig.TxtFuncMap()).Parse(templateText)
180181
if err != nil {
181182
panic(err.Error())
182183
}

‎ginkgo/generate_command.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"strconv"
1111
"strings"
1212
"text/template"
13+
14+
sprig "github.com/go-task/slim-sprig"
1315
)
1416

1517
func BuildGenerateCommand() *Command {
@@ -157,7 +159,7 @@ func generateSpecForSubject(subject string, agouti, noDot, internal bool, custom
157159
templateText = specText
158160
}
159161

160-
specTemplate, err := template.New("spec").Parse(templateText)
162+
specTemplate, err := template.New("spec").Funcs(sprig.TxtFuncMap()).Parse(templateText)
161163
if err != nil {
162164
return err
163165
}

‎go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/onsi/ginkgo
33
go 1.15
44

55
require (
6+
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0
67
github.com/nxadm/tail v1.4.8
78
github.com/onsi/gomega v1.10.1
89
golang.org/x/sys v0.0.0-20210112080510-489259a85091

‎go.sum

+11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
14
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
25
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
36
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
7+
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 h1:p104kn46Q8WdvHunIJ9dAyjPVtrBPhSr3KT2yUst43I=
8+
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE=
49
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
510
github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
611
github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA=
@@ -22,6 +27,11 @@ github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108
2227
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
2328
github.com/onsi/gomega v1.10.1 h1:o0+MgICZLuZ7xjH7Vx6zS/zcu93/BEp1VwkIW1mEXCE=
2429
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
30+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
31+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
32+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
33+
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
34+
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
2535
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
2636
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
2737
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
@@ -70,6 +80,7 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
7080
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
7181
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
7282
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
83+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
7384
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
7485
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
7586
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

‎integration/subcommand_test.go

+60-2
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,36 @@ var _ = Describe("Subcommand", func() {
112112
Ω(content).Should(ContainSubstring(`"binary"`))
113113
Ω(content).Should(ContainSubstring("// This is a foo_test test"))
114114
})
115+
116+
It("should generate a bootstrap file using a template that contains functions when told to", func() {
117+
templateFile := filepath.Join(pkgPath, ".bootstrap")
118+
ioutil.WriteFile(templateFile, []byte(`package {{.Package}}
119+
120+
import (
121+
{{.GinkgoImport}}
122+
{{.GomegaImport}}
123+
124+
"testing"
125+
"binary"
126+
)
127+
128+
func Test{{.FormattedName}}(t *testing.T) {
129+
// This is a {{.Package | repeat 3}} test
130+
}`), 0666)
131+
session := startGinkgo(pkgPath, "bootstrap", "--template", ".bootstrap")
132+
Eventually(session).Should(gexec.Exit(0))
133+
output := session.Out.Contents()
134+
135+
Ω(output).Should(ContainSubstring("foo_suite_test.go"))
136+
137+
content, err := ioutil.ReadFile(filepath.Join(pkgPath, "foo_suite_test.go"))
138+
Ω(err).ShouldNot(HaveOccurred())
139+
Ω(content).Should(ContainSubstring("package foo_test"))
140+
Ω(content).Should(ContainSubstring(`. "github.com/onsi/ginkgo"`))
141+
Ω(content).Should(ContainSubstring(`. "github.com/onsi/gomega"`))
142+
Ω(content).Should(ContainSubstring(`"binary"`))
143+
Ω(content).Should(ContainSubstring("// This is a foo_testfoo_testfoo_test test"))
144+
})
115145
})
116146

117147
Describe("nodot", func() {
@@ -182,10 +212,10 @@ var _ = Describe("Subcommand", func() {
182212
import (
183213
{{if .IncludeImports}}. "github.com/onsi/ginkgo"{{end}}
184214
{{if .IncludeImports}}. "github.com/onsi/gomega"{{end}}
185-
215+
186216
{{if .ImportPackage}}"{{.PackageImportPath}}"{{end}}
187217
)
188-
218+
189219
var _ = Describe("{{.Subject}}", func() {
190220
// This is a {{.Package}} test
191221
})`), 0666)
@@ -203,6 +233,34 @@ var _ = Describe("Subcommand", func() {
203233
Ω(content).Should(ContainSubstring(`/foo_bar"`))
204234
Ω(content).Should(ContainSubstring("// This is a foo_bar_test test"))
205235
})
236+
237+
It("should generate a test file using a template that contains functions", func() {
238+
templateFile := filepath.Join(pkgPath, ".generate")
239+
ioutil.WriteFile(templateFile, []byte(`package {{.Package}}
240+
import (
241+
{{if .IncludeImports}}. "github.com/onsi/ginkgo"{{end}}
242+
{{if .IncludeImports}}. "github.com/onsi/gomega"{{end}}
243+
244+
{{if .ImportPackage}}"{{.PackageImportPath}}"{{end}}
245+
)
246+
247+
var _ = Describe("{{.Subject}}", func() {
248+
// This is a {{.Package | repeat 3 }} test
249+
})`), 0666)
250+
session := startGinkgo(pkgPath, "generate", "--template", ".generate")
251+
Eventually(session).Should(gexec.Exit(0))
252+
output := session.Out.Contents()
253+
254+
Ω(output).Should(ContainSubstring("foo_bar_test.go"))
255+
256+
content, err := ioutil.ReadFile(filepath.Join(pkgPath, "foo_bar_test.go"))
257+
Ω(err).ShouldNot(HaveOccurred())
258+
Ω(content).Should(ContainSubstring("package foo_bar_test"))
259+
Ω(content).Should(ContainSubstring(`. "github.com/onsi/ginkgo"`))
260+
Ω(content).Should(ContainSubstring(`. "github.com/onsi/gomega"`))
261+
Ω(content).Should(ContainSubstring(`/foo_bar"`))
262+
Ω(content).Should(ContainSubstring("// This is a foo_bar_testfoo_bar_testfoo_bar_test test"))
263+
})
206264
})
207265

208266
Context("with an argument of the form: foo", func() {

0 commit comments

Comments
 (0)
Please sign in to comment.