1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
package main
import (
"bytes"
"flag"
"fmt"
"os"
"path/filepath"
"strings"
"text/template"
"go/build"
"github.com/onsi/ginkgo/ginkgo/nodot"
)
func BuildBootstrapCommand() *Command {
var agouti, noDot, internal bool
flagSet := flag.NewFlagSet("bootstrap", flag.ExitOnError)
flagSet.BoolVar(&agouti, "agouti", false, "If set, bootstrap will generate a bootstrap file for writing Agouti tests")
flagSet.BoolVar(&noDot, "nodot", false, "If set, bootstrap will generate a bootstrap 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")
return &Command{
Name: "bootstrap",
FlagSet: flagSet,
UsageCommand: "ginkgo bootstrap <FLAGS>",
Usage: []string{
"Bootstrap a test suite for the current package",
"Accepts the following flags:",
},
Command: func(args []string, additionalArgs []string) {
generateBootstrap(agouti, noDot, internal)
},
}
}
var bootstrapText = `package {{.Package}}
import (
{{.GinkgoImport}}
{{.GomegaImport}}
"testing"
)
func Test{{.FormattedName}}(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "{{.FormattedName}} Suite")
}
`
var agoutiBootstrapText = `package {{.Package}}
import (
{{.GinkgoImport}}
{{.GomegaImport}}
"github.com/sclevine/agouti"
"testing"
)
func Test{{.FormattedName}}(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "{{.FormattedName}} Suite")
}
var agoutiDriver *agouti.WebDriver
var _ = BeforeSuite(func() {
// Choose a WebDriver:
agoutiDriver = agouti.PhantomJS()
// agoutiDriver = agouti.Selenium()
// agoutiDriver = agouti.ChromeDriver()
Expect(agoutiDriver.Start()).To(Succeed())
})
var _ = AfterSuite(func() {
Expect(agoutiDriver.Stop()).To(Succeed())
})
`
type bootstrapData struct {
Package string
FormattedName string
GinkgoImport string
GomegaImport string
}
func getPackageAndFormattedName() (string, string, string) {
path, err := os.Getwd()
if err != nil {
complainAndQuit("Could not get current working directory: \n" + err.Error())
}
dirName := strings.Replace(filepath.Base(path), "-", "_", -1)
dirName = strings.Replace(dirName, " ", "_", -1)
pkg, err := build.ImportDir(path, 0)
packageName := pkg.Name
if err != nil {
packageName = dirName
}
formattedName := prettifyPackageName(filepath.Base(path))
return packageName, dirName, formattedName
}
func prettifyPackageName(name string) string {
name = strings.Replace(name, "-", " ", -1)
name = strings.Replace(name, "_", " ", -1)
name = strings.Title(name)
name = strings.Replace(name, " ", "", -1)
return name
}
func determinePackageName(name string, internal bool) string {
if internal {
return name
}
return name + "_test"
}
func fileExists(path string) bool {
_, err := os.Stat(path)
if err == nil {
return true
}
return false
}
func generateBootstrap(agouti, noDot, internal bool) {
packageName, bootstrapFilePrefix, formattedName := getPackageAndFormattedName()
data := bootstrapData{
Package: determinePackageName(packageName, internal),
FormattedName: formattedName,
GinkgoImport: `. "github.com/onsi/ginkgo"`,
GomegaImport: `. "github.com/onsi/gomega"`,
}
if noDot {
data.GinkgoImport = `"github.com/onsi/ginkgo"`
data.GomegaImport = `"github.com/onsi/gomega"`
}
targetFile := fmt.Sprintf("%s_suite_test.go", bootstrapFilePrefix)
if fileExists(targetFile) {
fmt.Printf("%s already exists.\n\n", targetFile)
os.Exit(1)
} else {
fmt.Printf("Generating ginkgo test suite bootstrap for %s in:\n\t%s\n", packageName, targetFile)
}
f, err := os.Create(targetFile)
if err != nil {
complainAndQuit("Could not create file: " + err.Error())
panic(err.Error())
}
defer f.Close()
var templateText string
if agouti {
templateText = agoutiBootstrapText
} else {
templateText = bootstrapText
}
bootstrapTemplate, err := template.New("bootstrap").Parse(templateText)
if err != nil {
panic(err.Error())
}
buf := &bytes.Buffer{}
bootstrapTemplate.Execute(buf, data)
if noDot {
contents, err := nodot.ApplyNoDot(buf.Bytes())
if err != nil {
complainAndQuit("Failed to import nodot declarations: " + err.Error())
}
fmt.Println("To update the nodot declarations in the future, switch to this directory and run:\n\tginkgo nodot")
buf = bytes.NewBuffer(contents)
}
buf.WriteTo(f)
goFmt(targetFile)
}
|