This repository has been archived by the owner on Nov 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
bootstrap_command.go
123 lines (103 loc) · 2.79 KB
/
bootstrap_command.go
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
package main
import (
"bytes"
"flag"
"fmt"
"os"
"path/filepath"
"strings"
"text/template"
"github.com/onsi/ginkgo/ginkgo/nodot"
)
func BuildBootstrapCommand() *Command {
var noDot bool
flagSet := flag.NewFlagSet("bootstrap", flag.ExitOnError)
flagSet.BoolVar(&noDot, "nodot", false, "If set, bootstrap will generate a bootstrap file that does not . import ginkgo and gomega")
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(noDot)
},
}
}
var bootstrapText = `package {{.Package}}_test
import (
{{.GinkgoImport}}
{{.GomegaImport}}
"testing"
)
func Test{{.FormattedPackage}}(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "{{.FormattedPackage}} Suite")
}
`
type bootstrapData struct {
Package string
FormattedPackage string
GinkgoImport string
GomegaImport string
}
func getPackage() string {
workingDir, err := os.Getwd()
if err != nil {
complainAndQuit("Could not find package: " + err.Error())
}
packageName := filepath.Base(workingDir)
return strings.Replace(packageName, "-", "_", -1)
}
func fileExists(path string) bool {
_, err := os.Stat(path)
if err == nil {
return true
}
return false
}
func generateBootstrap(noDot bool) {
packageName := getPackage()
formattedPackage := strings.Replace(strings.Title(strings.Replace(packageName, "_", " ", -1)), " ", "", -1)
data := bootstrapData{
Package: packageName,
FormattedPackage: formattedPackage,
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", packageName)
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()
bootstrapTemplate, err := template.New("bootstrap").Parse(bootstrapText)
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)
}