Skip to content

Instantly share code, notes, and snippets.

@eungju
Last active March 14, 2024 07:13
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 eungju/7126a3f7ad84295928987d68da5d5d65 to your computer and use it in GitHub Desktop.
Save eungju/7126a3f7ad84295928987d68da5d5d65 to your computer and use it in GitHub Desktop.
Go Continuous Testing
package main
import (
"fmt"
"log"
"os"
"os/exec"
"time"
"github.com/fsnotify/fsnotify"
)
func main() {
runTest()
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
done := make(chan bool)
go func() {
lastT := time.Unix(0, 0)
for {
select {
case event := <-watcher.Events:
log.Println("event:", event)
d := time.Now().Sub(lastT).Seconds()
if d >= 3.0 {
runTest()
}
lastT = time.Now()
case err := <-watcher.Errors:
log.Println("error:", err)
}
}
}()
err = watcher.Add(os.Args[1])
if err != nil {
log.Fatal(err)
}
<-done
}
func runTest() {
cmd := exec.Command("go", "test", "./...")
outerr, err := cmd.CombinedOutput()
if err != nil {
fmt.Printf("%s", outerr)
log.Println(err)
} else {
log.Println("ok.")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment