Last active
March 14, 2024 07:13
-
-
Save eungju/7126a3f7ad84295928987d68da5d5d65 to your computer and use it in GitHub Desktop.
Go Continuous Testing
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 ( | |
"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