Skip to content

Instantly share code, notes, and snippets.

@vladimir-vg
Created March 13, 2012 22:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vladimir-vg/2032240 to your computer and use it in GitHub Desktop.
Save vladimir-vg/2032240 to your computer and use it in GitHub Desktop.
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