/
autotest.go
387 lines (344 loc) · 8.72 KB
/
autotest.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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
// Autotest watches source code directories for changes and automatically runs
// ‘go test’; useful for continuous integration, test driven and behavior
// driven development workflows (CI, BDD, TDD)
package main
// autotest github.com/roastery/[paths...] [packages...] [testflags]
// - new module for log colorization
// - use StringArray (TestFlags, paths)
import (
"fmt"
"go/build"
"log"
"os"
"os/exec"
"os/signal"
"path/filepath"
"regexp"
"strings"
"syscall"
"time"
"github.com/go-fsnotify/fsnotify"
)
type autotest struct {
// Finished is signaled when cleanup is complete.
Finished chan bool
// SettleTime indicates how long to wait after the last file system change before launching.
SettleTime time.Duration
// IgnoreDirs lists the names of directories that should not be watched for changes.
IgnoreDirs map[string]bool
// IgnoreFiles is a list of regular expression patterns for files that should be ignored.
IgnoreFiles []*regexp.Regexp
// TestFlags contains optional arguments for 'go test'.
TestFlags []string
debug bool
fs *fsnotify.Watcher
done chan bool
gosrc string
paths []string
timeSuccess time.Time
timeFailure time.Time
lastState int
}
// Values for lastState
const (
starting = iota
working
failing
)
// Go's time package does not provide a method such as:
// func (d *Duration) Round(Duration)
// So we implement it here.
func round(duration, interval time.Duration) time.Duration {
t := int64(duration) + int64(interval)/2
return time.Duration(t - t%int64(interval))
}
func newWatcher() (*autotest, error) {
fs, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}
a := &autotest{
Finished: make(chan bool),
SettleTime: 2 * time.Second,
IgnoreDirs: map[string]bool{".git": true},
IgnoreFiles: []*regexp.Regexp{
regexp.MustCompile(`\..*\.swp$`),
},
TestFlags: make([]string, 0),
debug: false,
fs: fs,
done: make(chan bool),
gosrc: filepath.Join(os.Getenv("GOPATH"), "src"),
paths: make([]string, 0),
lastState: starting,
}
return a, nil
}
func (a *autotest) Close() error {
return a.fs.Close()
}
func (a *autotest) Start() {
go a.monitorChanges()
}
func (a *autotest) Stop() {
a.done <- true
}
func (a *autotest) Add(path string) error {
// watch the file system path
err := a.fs.Add(path)
if err != nil {
log.Fatal(err)
}
a.paths = append(a.paths, path)
// is it a package dir (under $GOPATH/src?)
if pkg := a.getPackageName(path); pkg != "" && a.debug {
log.Println("package:", pkg, "in path:", path)
}
log.Println("watching for changes:", path)
return err
}
func (a *autotest) Remove(path string) error {
// find path in a.paths, remove the entry
for i, val := range a.paths {
if val == path {
// delete entry at position i
copy(a.paths[i:], a.paths[i+1:])
a.paths = a.paths[0 : len(a.paths)-1]
break
}
}
return a.fs.Remove(path)
}
// AddRecursive walks a directory recursively, and watches all subdirectories.
func (a *autotest) AddRecursive(path string) error {
return filepath.Walk(path, func(subpath string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if _, ignore := a.IgnoreDirs[info.Name()]; ignore {
return filepath.SkipDir
}
if info.IsDir() {
return a.Add(subpath)
}
return nil
})
}
// RunTests invokes the 'go test' tool for all monitored packages.
func (a *autotest) RunTests() {
if err := a.handleModifications(); err != nil {
msg := "error: " + err.Error()
if a.lastState != failing {
a.timeFailure = time.Now()
}
if a.lastState == working {
msg += fmt.Sprintf(" (%s success)", round(time.Since(a.timeSuccess), time.Second))
}
a.lastState = failing
log.Println("\u001b[31m" + msg + "\u001b[0m")
} else {
msg := ""
if a.lastState != working {
a.timeSuccess = time.Now()
}
if a.lastState == failing {
msg = fmt.Sprintf("success after %s failures", round(time.Since(a.timeFailure), time.Second))
}
a.lastState = working
if len(msg) != 0 {
log.Println("\u001b[32m" + msg + "\u001b[0m")
}
}
}
// monitorChanges is the main processing loop for file system notifications.
func (a *autotest) monitorChanges() {
modified := false
for {
select {
case <-a.done:
a.Finished <- true
return
case err := <-a.fs.Errors:
log.Println("error:", err)
case event := <-a.fs.Events:
mod, err := a.handleEvent(event)
if err != nil {
log.Println("error:", err)
} else if mod {
modified = true
}
case <-time.After(a.SettleTime):
if modified {
a.RunTests()
modified = false
}
}
}
}
// handleEvent handles a file system change notification.
func (a *autotest) handleEvent(event fsnotify.Event) (bool, error) {
filename := event.Name
modified := false
if event.Op&fsnotify.Create != 0 {
info, err := os.Stat(filename)
if err != nil {
// ENOENT can occur here for temp files (already deleted)
if strings.HasSuffix(err.Error(), "no such file or directory") {
return false, nil
}
return false, err
}
if info.IsDir() {
if err := a.Add(filename); err != nil {
return false, err
}
} else {
if a.debug {
log.Println("created:", filename)
}
modified = true
}
}
if event.Op&fsnotify.Remove != 0 {
if err := a.Remove(filename); err != nil {
// "can't remove non-existent inotify watch" is OK
if !strings.HasPrefix(err.Error(), "can't remove non-existent inotify watch") {
return false, err
}
}
if a.debug {
log.Println("removed:", filename)
}
modified = true
}
if event.Op&fsnotify.Write != 0 {
// skip file if it matches any regexp in IgnoreFiles
skip := false
base := filepath.Base(filename)
for _, re := range a.IgnoreFiles {
if re.MatchString(base) {
skip = true
break
}
}
if skip {
if a.debug {
log.Println("skipping:", filename)
}
} else {
if a.debug {
log.Println("modified:", filename)
}
modified = true
}
}
return modified, nil
}
// handleModifications launches 'go test'.
func (a *autotest) handleModifications() error {
args := make([]string, 1+len(a.TestFlags))
args[0] = "test"
copy(args[1:], a.TestFlags)
npkg := 0
for _, path := range a.paths {
if pkg := a.getPackageName(path); pkg != "" {
args = append(args, pkg)
npkg++
}
}
cmd := exec.Command("go", args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
log.Printf("running go test with %d packages\n", npkg)
return cmd.Run()
}
// getPackageName returns the go package name for a path, or "" if not a package dir.
func (a *autotest) getPackageName(path string) string {
if pkg, err := filepath.Rel(a.gosrc, path); err == nil {
return pkg
}
return ""
}
// --------------------------------------------------------------------------
func getCwd() string {
cwd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
return cwd
}
// findPackage looks for path in the current directory, and any go source dirs,
// and returns the resolved path or an empty string if not found.
func findPackage(path string) string {
// check relative to current directory first
if stat, err := os.Stat(path); err == nil && stat.IsDir() {
if !filepath.IsAbs(path) {
path = filepath.Join(getCwd(), path)
}
return path
}
// check GOROOT / GOPATH
for _, srcDir := range build.Default.SrcDirs() {
pkg, err := build.Default.Import(path, srcDir, build.FindOnly)
if err == nil {
return pkg.Dir
}
}
log.Println("package not found:", path)
return ""
}
func main() {
for _, arg := range os.Args[1:] {
if arg == "-h" || arg == "--help" {
fmt.Printf(`Monitors the file system and automatically runs 'go test' on changes.
usage: %s [-h | --help] [testflags] [path...] [package...]
options:
-h, --help print this message
testflags flags supported by 'go test'; see 'go help testflag'
path... filesystem path, monitored recursively
package... go package name for which 'go test' will be issued
`, os.Args[0])
os.Exit(0)
}
}
if os.Getenv("GOPATH") == "" {
log.Fatalln("GOPATH is not set")
}
w, err := newWatcher()
if err != nil {
log.Fatal(err)
}
w.SettleTime = 500 * time.Millisecond
// signals used to stop
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt, os.Kill, syscall.SIGTERM)
go func() {
//signal := <-signals
//log.Println("got signal:", signal)
<-signals
w.Stop()
}()
// monitor paths
gotOne := false
for _, arg := range os.Args[1:] {
if arg[0] == '-' {
w.TestFlags = append(w.TestFlags, arg)
} else if path := findPackage(arg); path != "" {
if err := w.AddRecursive(path); err != nil {
log.Fatal(err)
} else {
gotOne = true
}
}
}
if !gotOne {
log.Fatalln("no paths to watch")
}
w.Start()
w.RunTests()
<-w.Finished
if err := w.Close(); err != nil {
log.Fatal(err)
}
log.Println("exiting")
}