Automatically Run Go Tests When Files Change (OS X)

Want to run go test automatically on a mac when your code changes?

  1. Download and compile fswatch
  2. In the directory you want to test, run:

    go test && fswatch . “go test”

Any time you save a file in that directory, your tests will run.

If you’re a fan of notification center, you can install terminal-notifier and do something like:

go test && fswatch . "terminal-notifier -message `go test`"

This isn’t perfect as it barfs on the multiline result of running go test, but it will show PASS/FAIL in the notification center. It wouldn’t be hard to write a little script to split up the output and make this pretty.

Ok fine, here it is:

#!/bin/bash

IFS='
'
lines=( `go test` )
title=${lines[0]}
if [ $title != "PASS" ]
then
        title="FAIL"
        echo "${lines[*]}"
fi
unset lines[0]
terminal-notifier -title "go test" -subtitle "$title" -message "${lines[*]}" -group "go test `pwd`"

Call that script whatever you want…I named it gtnotify. Then you can do:

gtnotify && fswatch . gtnotify

Still too verbose? Here’s another script called gtwatch:

#!/bin/sh

gtnotify && fswatch . gtnotify

And now you can do:

gtwatch

After all that, I think I prefer the terminal output…


Comments? Send us a tweet.

Permalink:

Previous:
Use JSON to Update Many Stats
Next:
Nonblocking StatHat Ruby Gem Released