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 support for using template to generate tests #752

Merged
merged 4 commits into from Dec 15, 2020
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
25 changes: 18 additions & 7 deletions ginkgo/generate_command.go
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
Expand All @@ -12,11 +13,15 @@ import (
)

func BuildGenerateCommand() *Command {
var agouti, noDot, internal bool
var (
agouti, noDot, internal bool
customTestFile string
)
flagSet := flag.NewFlagSet("generate", flag.ExitOnError)
flagSet.BoolVar(&agouti, "agouti", false, "If set, generate will generate a test file for writing Agouti tests")
flagSet.BoolVar(&noDot, "nodot", false, "If set, generate will generate a test file that does not . import ginkgo and gomega")
flagSet.BoolVar(&internal, "internal", false, "If set, generate will generate a test file that uses the regular package name")
flagSet.StringVar(&customTestFile, "template", "", "If specified, generate will use the contents of the file passed as the test file template")

return &Command{
Name: "generate",
Expand All @@ -28,7 +33,7 @@ func BuildGenerateCommand() *Command {
"Accepts the following flags:",
},
Command: func(args []string, additionalArgs []string) {
generateSpec(args, agouti, noDot, internal)
generateSpec(args, agouti, noDot, internal, customTestFile)
},
}
}
Expand Down Expand Up @@ -81,9 +86,9 @@ type specData struct {
ImportPackage bool
}

func generateSpec(args []string, agouti, noDot, internal bool) {
func generateSpec(args []string, agouti, noDot, internal bool, customTestFile string) {
if len(args) == 0 {
err := generateSpecForSubject("", agouti, noDot, internal)
err := generateSpecForSubject("", agouti, noDot, internal, customTestFile)
if err != nil {
fmt.Println(err.Error())
fmt.Println("")
Expand All @@ -95,7 +100,7 @@ func generateSpec(args []string, agouti, noDot, internal bool) {

var failed bool
for _, arg := range args {
err := generateSpecForSubject(arg, agouti, noDot, internal)
err := generateSpecForSubject(arg, agouti, noDot, internal, customTestFile)
if err != nil {
failed = true
fmt.Println(err.Error())
Expand All @@ -107,7 +112,7 @@ func generateSpec(args []string, agouti, noDot, internal bool) {
}
}

func generateSpecForSubject(subject string, agouti, noDot, internal bool) error {
func generateSpecForSubject(subject string, agouti, noDot, internal bool, customTestFile string) error {
packageName, specFilePrefix, formattedName := getPackageAndFormattedName()
if subject != "" {
specFilePrefix = formatSubject(subject)
Expand Down Expand Up @@ -136,7 +141,13 @@ func generateSpecForSubject(subject string, agouti, noDot, internal bool) error
defer f.Close()

var templateText string
if agouti {
if customTestFile != "" {
tpl, err := ioutil.ReadFile(customTestFile)
if err != nil {
panic(err.Error())
}
templateText = string(tpl)
} else if agouti {
templateText = agoutiSpecText
} else {
templateText = specText
Expand Down
30 changes: 30 additions & 0 deletions integration/subcommand_test.go
Expand Up @@ -175,6 +175,36 @@ var _ = Describe("Subcommand", func() {
})
})

Context("with template argument", func() {
It("should generate a test file using a template", 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}} 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_test test"))
})
})

Context("with an argument of the form: foo", func() {
It("should generate a test file named after the argument", func() {
session := startGinkgo(pkgPath, "generate", "baz_buzz")
Expand Down