Permalink
Browse files

Don't start 'go test' on each file event, skip events burst.

  • Loading branch information...
1 parent 325ee96 commit 70e25bd29debc8cbcc9d85ab4c0452664e52bf1f @antage antage committed Sep 22, 2012
Showing with 33 additions and 24 deletions.
  1. +33 −24 goautotest.go
View
@@ -7,8 +7,36 @@ import (
"os"
"os/exec"
"strings"
+ "time"
)
+func startGoTest() {
+ fmt.Println("Running tests...")
+
+ args := append([]string{"test"}, os.Args[1:]...)
+ cmd := exec.Command("go", args...)
+
+ stdout, err := cmd.StdoutPipe()
+ if err != nil {
+ fmt.Println(err)
+ os.Exit(1)
+ }
+
+ err = cmd.Start()
+ if err != nil {
+ fmt.Println(err)
+ os.Exit(1)
+ }
+
+ go io.Copy(os.Stdout, stdout)
+ err = cmd.Wait()
+ if err != nil {
+ fmt.Println(err)
+ }
+
+ fmt.Println()
+}
+
func main() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
@@ -30,38 +58,19 @@ func main() {
defer watcher.Close()
+ burstGuard := make(<-chan time.Time)
for {
select {
case ev := <-watcher.Event:
if strings.HasSuffix(ev.Name, ".go") {
- fmt.Println("Running tests...")
-
- args := append([]string{"test"}, os.Args[1:]...)
- cmd := exec.Command("go", args...)
-
- stdout, err := cmd.StdoutPipe()
- if err != nil {
- fmt.Println(err)
- break
- }
-
- err = cmd.Start()
- if err != nil {
- fmt.Println(err)
- break
- }
-
- go io.Copy(os.Stdout, stdout)
- err = cmd.Wait()
- if err != nil {
- fmt.Println(err)
- }
-
- fmt.Println()
+ burstGuard = time.After(500 * time.Millisecond)
}
case err := <-watcher.Error:
fmt.Println(err)
+
+ case <-burstGuard:
+ startGoTest()
}
}

0 comments on commit 70e25bd

Please sign in to comment.