Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add slim-sprig template functions to bootstrap/generate #775

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion ginkgo/bootstrap_command.go
Expand Up @@ -12,6 +12,7 @@ import (

"go/build"

sprig "github.com/go-task/slim-sprig"
"github.com/onsi/ginkgo/ginkgo/nodot"
)

Expand Down Expand Up @@ -176,7 +177,7 @@ func generateBootstrap(agouti, noDot, internal bool, customBootstrapFile string)
templateText = bootstrapText
}

bootstrapTemplate, err := template.New("bootstrap").Parse(templateText)
bootstrapTemplate, err := template.New("bootstrap").Funcs(sprig.TxtFuncMap()).Parse(templateText)
if err != nil {
panic(err.Error())
}
Expand Down
4 changes: 3 additions & 1 deletion ginkgo/generate_command.go
Expand Up @@ -10,6 +10,8 @@ import (
"strconv"
"strings"
"text/template"

sprig "github.com/go-task/slim-sprig"
)

func BuildGenerateCommand() *Command {
Expand Down Expand Up @@ -157,7 +159,7 @@ func generateSpecForSubject(subject string, agouti, noDot, internal bool, custom
templateText = specText
}

specTemplate, err := template.New("spec").Parse(templateText)
specTemplate, err := template.New("spec").Funcs(sprig.TxtFuncMap()).Parse(templateText)
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -3,6 +3,7 @@ module github.com/onsi/ginkgo
go 1.15

require (
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0
github.com/nxadm/tail v1.4.8
github.com/onsi/gomega v1.10.1
golang.org/x/sys v0.0.0-20210112080510-489259a85091
Expand Down
11 changes: 11 additions & 0 deletions go.sum
@@ -1,6 +1,11 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 h1:p104kn46Q8WdvHunIJ9dAyjPVtrBPhSr3KT2yUst43I=
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA=
Expand All @@ -22,6 +27,11 @@ github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
github.com/onsi/gomega v1.10.1 h1:o0+MgICZLuZ7xjH7Vx6zS/zcu93/BEp1VwkIW1mEXCE=
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
Expand Down Expand Up @@ -70,6 +80,7 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
62 changes: 60 additions & 2 deletions integration/subcommand_test.go
Expand Up @@ -112,6 +112,36 @@ var _ = Describe("Subcommand", func() {
Ω(content).Should(ContainSubstring(`"binary"`))
Ω(content).Should(ContainSubstring("// This is a foo_test test"))
})

It("should generate a bootstrap file using a template that contains functions when told to", func() {
templateFile := filepath.Join(pkgPath, ".bootstrap")
ioutil.WriteFile(templateFile, []byte(`package {{.Package}}

import (
{{.GinkgoImport}}
{{.GomegaImport}}

"testing"
"binary"
)

func Test{{.FormattedName}}(t *testing.T) {
// This is a {{.Package | repeat 3}} test
}`), 0666)
session := startGinkgo(pkgPath, "bootstrap", "--template", ".bootstrap")
Eventually(session).Should(gexec.Exit(0))
output := session.Out.Contents()

Ω(output).Should(ContainSubstring("foo_suite_test.go"))

content, err := ioutil.ReadFile(filepath.Join(pkgPath, "foo_suite_test.go"))
Ω(err).ShouldNot(HaveOccurred())
Ω(content).Should(ContainSubstring("package foo_test"))
Ω(content).Should(ContainSubstring(`. "github.com/onsi/ginkgo"`))
Ω(content).Should(ContainSubstring(`. "github.com/onsi/gomega"`))
Ω(content).Should(ContainSubstring(`"binary"`))
Ω(content).Should(ContainSubstring("// This is a foo_testfoo_testfoo_test test"))
})
})

Describe("nodot", func() {
Expand Down Expand Up @@ -182,10 +212,10 @@ var _ = Describe("Subcommand", func() {
import (
{{if .IncludeImports}}. "github.com/onsi/ginkgo"{{end}}
{{if .IncludeImports}}. "github.com/onsi/gomega"{{end}}

{{if .ImportPackage}}"{{.PackageImportPath}}"{{end}}
)

var _ = Describe("{{.Subject}}", func() {
// This is a {{.Package}} test
})`), 0666)
Expand All @@ -203,6 +233,34 @@ var _ = Describe("Subcommand", func() {
Ω(content).Should(ContainSubstring(`/foo_bar"`))
Ω(content).Should(ContainSubstring("// This is a foo_bar_test test"))
})

It("should generate a test file using a template that contains functions", func() {
templateFile := filepath.Join(pkgPath, ".generate")
ioutil.WriteFile(templateFile, []byte(`package {{.Package}}
import (
{{if .IncludeImports}}. "github.com/onsi/ginkgo"{{end}}
{{if .IncludeImports}}. "github.com/onsi/gomega"{{end}}

{{if .ImportPackage}}"{{.PackageImportPath}}"{{end}}
)

var _ = Describe("{{.Subject}}", func() {
// This is a {{.Package | repeat 3 }} test
})`), 0666)
session := startGinkgo(pkgPath, "generate", "--template", ".generate")
Eventually(session).Should(gexec.Exit(0))
output := session.Out.Contents()

Ω(output).Should(ContainSubstring("foo_bar_test.go"))

content, err := ioutil.ReadFile(filepath.Join(pkgPath, "foo_bar_test.go"))
Ω(err).ShouldNot(HaveOccurred())
Ω(content).Should(ContainSubstring("package foo_bar_test"))
Ω(content).Should(ContainSubstring(`. "github.com/onsi/ginkgo"`))
Ω(content).Should(ContainSubstring(`. "github.com/onsi/gomega"`))
Ω(content).Should(ContainSubstring(`/foo_bar"`))
Ω(content).Should(ContainSubstring("// This is a foo_bar_testfoo_bar_testfoo_bar_test test"))
})
})

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