/
package_rewriter.go
126 lines (104 loc) · 3.07 KB
/
package_rewriter.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
124
125
126
package convert
import (
"fmt"
"go/build"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"regexp"
)
/*
* RewritePackage takes a name (eg: my-package/tools), finds its test files using
* Go's build package, and then rewrites them. A ginkgo test suite file will
* also be added for this package, and all of its child packages.
*/
func RewritePackage(packageName string) {
pkg, err := packageWithName(packageName)
if err != nil {
panic(fmt.Sprintf("unexpected error reading package: '%s'\n%s\n", packageName, err.Error()))
}
for _, filename := range findTestsInPackage(pkg) {
rewriteTestsInFile(filename)
}
}
/*
* Given a package, findTestsInPackage reads the test files in the directory,
* and then recurses on each child package, returning a slice of all test files
* found in this process.
*/
func findTestsInPackage(pkg *build.Package) (testfiles []string) {
for _, file := range append(pkg.TestGoFiles, pkg.XTestGoFiles...) {
testfiles = append(testfiles, filepath.Join(pkg.Dir, file))
}
dirFiles, err := ioutil.ReadDir(pkg.Dir)
if err != nil {
panic(fmt.Sprintf("unexpected error reading dir: '%s'\n%s\n", pkg.Dir, err.Error()))
}
re := regexp.MustCompile(`^[._]`)
for _, file := range dirFiles {
if !file.IsDir() {
continue
}
if re.Match([]byte(file.Name())) {
continue
}
packageName := filepath.Join(pkg.ImportPath, file.Name())
subPackage, err := packageWithName(packageName)
if err != nil {
panic(fmt.Sprintf("unexpected error reading package: '%s'\n%s\n", packageName, err.Error()))
}
testfiles = append(testfiles, findTestsInPackage(subPackage)...)
}
addGinkgoSuiteForPackage(pkg)
goFmtPackage(pkg)
return
}
/*
* Shells out to `ginkgo bootstrap` to create a test suite file
*/
func addGinkgoSuiteForPackage(pkg *build.Package) {
originalDir, err := os.Getwd()
if err != nil {
panic(err)
}
suite_test_file := filepath.Join(pkg.Dir, pkg.Name+"_suite_test.go")
_, err = os.Stat(suite_test_file)
if err == nil {
return // test file already exists, this should be a no-op
}
err = os.Chdir(pkg.Dir)
if err != nil {
panic(err)
}
output, err := exec.Command("ginkgo", "bootstrap").Output()
if err != nil {
panic(fmt.Sprintf("error running 'ginkgo bootstrap'.\nstdout: %s\n%s\n", output, err.Error()))
}
err = os.Chdir(originalDir)
if err != nil {
panic(err)
}
}
/*
* Shells out to `go fmt` to format the package
*/
func goFmtPackage(pkg *build.Package) {
output, err := exec.Command("go", "fmt", pkg.ImportPath).Output()
if err != nil {
fmt.Printf("Warning: Error running 'go fmt %s'.\nstdout: %s\n%s\n", pkg.ImportPath, output, err.Error())
}
}
/*
* Attempts to return a package with its test files already read.
* The ImportMode arg to build.Import lets you specify if you want go to read the
* buildable go files inside the package, but it fails if the package has no go files
*/
func packageWithName(name string) (pkg *build.Package, err error) {
pkg, err = build.Default.Import(name, ".", build.ImportMode(0))
if err == nil {
return
}
pkg, err = build.Default.Import(name, ".", build.ImportMode(1))
return
}