-
Notifications
You must be signed in to change notification settings - Fork 3
/
goautotest.go
71 lines (56 loc) · 981 Bytes
/
goautotest.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
package main
import (
"fmt"
"os"
"os/exec"
"strings"
"github.com/howeyc/fsnotify"
)
var args = append([]string{"test"}, os.Args[1:]...)
func main() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
fail(err)
}
wd, err := os.Getwd()
if err != nil {
fail(err)
}
if err := watcher.Watch(wd); err != nil {
fail(err)
}
defer watcher.Close()
running := false
doneChan := make(chan bool)
for {
select {
case ev := <-watcher.Event:
if running {
continue
}
if strings.HasSuffix(ev.Name, ".go") {
running = true
go test(doneChan)
}
case err := <-watcher.Error:
fmt.Println(err)
case <-doneChan:
running = false
}
}
}
func test(doneChan chan bool) {
fmt.Println("Running tests...")
cmd := exec.Command("go", args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
fail(err)
}
fmt.Println()
doneChan <- true
}
func fail(err error) {
fmt.Println(err)
os.Exit(1)
}