forked from ryanslade/goautotest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goautotest.go
97 lines (79 loc) · 1.72 KB
/
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
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
package main
import (
"bytes"
"fmt"
"github.com/howeyc/fsnotify"
"os"
"os/exec"
"strings"
"time"
)
func startGoTest(doneChan chan bool) {
var output bytes.Buffer
var errorOutput bytes.Buffer
fmt.Println("Running tests...")
args := append([]string{"test"}, os.Args[1:]...)
cmd := exec.Command("go", args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
//snagging the cmd output.
cmd.Stdout = &output
cmd.Stderr = &errorOutput
errorOutput.Reset()
err := cmd.Run()
if err != nil {
fmt.Println(err)
}
//checking to see if any unit tests failed, if so do the windows cmd beep
if strings.Contains(output.String(), "--- FAIL") ||
strings.Contains(errorOutput.String(), ".go:") { //make this into a better regex later
fmt.Print("\x07") //the lovely console beep sound :D
}
//display any build errors
fmt.Println(errorOutput.String())
//dislay out the unit test results
fmt.Println(output.String())
fmt.Println()
fmt.Println("waiting...")
doneChan <- true
}
func main() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
wd, err := os.Getwd()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
err = watcher.Watch(wd)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer watcher.Close()
ignore := false
doneChan := make(chan bool)
readyChan := make(chan bool)
//initial waiting message
fmt.Println("waiting...")
for {
select {
case ev := <-watcher.Event:
if strings.HasSuffix(ev.Name, ".go") && !ignore {
ignore = true
go startGoTest(doneChan)
}
case err := <-watcher.Error:
fmt.Println(err)
case <-doneChan:
time.AfterFunc(1500*time.Millisecond, func() {
readyChan <- true
})
case <-readyChan:
ignore = false
}
}
}