Created
March 13, 2012 22:32
-
-
Save vladimir-vg/2032240 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "log" | |
import "os" | |
import "regexp" | |
import "os/exec" | |
import "exp/inotify" | |
func runTests() { | |
cmd := exec.Command("go", "test") | |
cmd.Stdout = os.Stdout | |
if err := cmd.Run(); err != nil { | |
log.Println(err) | |
notifySystem("go test failed") | |
} | |
} | |
// probably will work only for Ubuntu | |
func notifySystem(message string) { | |
cmd := exec.Command("notify-send", message) | |
if err := cmd.Run(); err != nil { | |
log.Fatal(err) | |
} | |
} | |
func setupWatcher() *inotify.Watcher { | |
watcher, err := inotify.NewWatcher() | |
if err != nil { | |
log.Fatal(err) | |
} | |
err = watcher.AddWatch(".", inotify.IN_MODIFY) | |
if err != nil { | |
log.Fatal(err) | |
} | |
return watcher | |
} | |
func main() { | |
watcher := setupWatcher() | |
runTests() | |
for { | |
select { | |
case ev := <-watcher.Event: | |
matched, err := regexp.MatchString(".*\\.go", ev.Name) | |
if err != nil { | |
log.Fatal(err) | |
} | |
if matched { | |
// On my machine it always raised two events | |
// ignore second, prevent twice tests run. | |
// Probably it's depends of test editor. | |
// If so, feel free to | |
// remove line above: | |
<-watcher.Event | |
runTests() | |
} | |
case err := <-watcher.Error: | |
log.Println("error:", err) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment