forked from cloudfoundry/cli
/
test_suite.go
130 lines (111 loc) · 2.73 KB
/
test_suite.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
127
128
129
130
package testsuite
import (
"crypto/md5"
"github.com/onsi/ginkgo/ginkgo/support/fsnotify"
"io/ioutil"
"log"
"os"
"path/filepath"
"regexp"
)
type TestSuite struct {
Path string
PackageName string
IsGinkgo bool
}
func SuitesInDir(dir string, recurse bool) []*TestSuite {
suites := []*TestSuite{}
files, _ := ioutil.ReadDir(dir)
re := regexp.MustCompile(`_test\.go$`)
for _, file := range files {
if !file.IsDir() && re.Match([]byte(file.Name())) {
suites = append(suites, New(dir, files))
break
}
}
if recurse {
re = regexp.MustCompile(`^[._]`)
for _, file := range files {
if file.IsDir() && !re.Match([]byte(file.Name())) {
suites = append(suites, SuitesInDir(dir+"/"+file.Name(), recurse)...)
}
}
}
return suites
}
func New(dir string, files []os.FileInfo) *TestSuite {
dir, _ = filepath.Abs(dir)
cwd, _ := os.Getwd()
dir, _ = filepath.Rel(cwd, filepath.Clean(dir))
dir = "." + string(filepath.Separator) + dir
return &TestSuite{
Path: dir,
PackageName: packageNameForSuite(dir),
IsGinkgo: filesHaveGinkgoSuite(dir, files),
}
}
func packageNameForSuite(dir string) string {
path, _ := filepath.Abs(dir)
return filepath.Base(path)
}
func filesHaveGinkgoSuite(dir string, files []os.FileInfo) bool {
reTestFile := regexp.MustCompile(`_test\.go$`)
reGinkgo := regexp.MustCompile(`package ginkgo|\/ginkgo"`)
for _, file := range files {
if !file.IsDir() && reTestFile.Match([]byte(file.Name())) {
contents, _ := ioutil.ReadFile(dir + "/" + file.Name())
if reGinkgo.Match(contents) {
return true
}
}
}
return false
}
func (suite *TestSuite) md5() [16]byte {
files, _ := ioutil.ReadDir(suite.Path)
re := regexp.MustCompile(`\.go$`)
data := []byte{}
for _, file := range files {
if !file.IsDir() && re.Match([]byte(file.Name())) {
data = append(data, []byte(file.Name())...)
fileData, err := ioutil.ReadFile(filepath.Join(suite.Path, file.Name()))
if err == nil {
data = append(data, fileData...)
}
}
}
return md5.Sum(data)
}
func (suite *TestSuite) Watch(notificationChannel chan *TestSuite) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
re := regexp.MustCompile(`\.go$`)
notified := false
notifiedChannel := make(chan bool)
hash := suite.md5()
go func() {
for {
select {
case ev := <-watcher.Event:
if re.Match([]byte(ev.Name)) && !notified && suite.md5() != hash {
notified = true
go func() {
notificationChannel <- suite
notifiedChannel <- true
}()
}
case err := <-watcher.Error:
log.Println("error:", err)
case <-notifiedChannel:
hash = suite.md5()
notified = false
}
}
}()
err = watcher.Watch(suite.Path)
if err != nil {
log.Fatal(err)
}
}