Example #1
0
func (l *watcherLoop) Run() {
	// Run the tests for the first time.
	execGoTest(l.paths)

	watcher, err := fsnotify.NewWatcher()

	for _, path := range l.paths {
		err = watcher.Watch(path)
		if err != nil {
			application.Fatal(err.Error())
		}
		application.Printf("Start watching path %s", path)
	}

	for {
		select {
		case <-l.pause:
			l.pause <- 0
		case <-l.terminate:
			watcher.Close()
			l.terminate <- 0
			return
		case ev := <-watcher.Event:
			if ev.IsModify() {
				if matches(ev.Name, ".*\\.go$") {
					if application.Verbose {
						application.Logf("Event %s occured for file %s", ev, ev.Name)
					}
					// check if the same event was
					// registered for the same
					// file in the acceptable
					// TIME_DISCARD time window
					event := getEvent(ev.Name)
					if event == nil {
						event = addEvent(&eventOnFile{ev, time.Now()})
						application.Logf("Run the tests")
						execGoTest(l.paths)
					} else if time.Now().Sub(event.time) > DISCARD_TIME {
						event.time = time.Now()
						application.Logf("Run the tests")
						execGoTest(l.paths)
					} else {
						if application.Verbose {
							application.Logf("Event %s was discarded for file %s", ev, ev.Name)
						}
					}
				}
			}
		case err := <-watcher.Error:
			application.Fatal(err.Error())
		}
	}
}
Example #2
0
func main() {
	verbose := flag.Bool("verbose", false, "Verbose mode")
	help := flag.Bool("help", false, "Show usage")
	flag.Usage = usage
	flag.Parse()

	initialPath := flag.Arg(0)
	if initialPath == "" {
		initialPath = "./"
	}

	if _, err := os.Stat(initialPath); err != nil {
		application.Fatal(err.Error())
	}

	application.Verbose = *verbose
	if *help {
		usage()
		return
	}
	watcherLoop := newWatcherLoop(initialPath)
	application.Register("Watcher Loop", watcherLoop)
	application.InstallSignalHandler(&sigterm{paths: watcherLoop.paths})
	exitCh := make(chan bool)
	application.Run()
	<-exitCh
}