Saturday, October 02, 2010

Watchr is teh awesome

Autotest spoiled me for immediate feedback during the development cycle. So as I was going through the Relevance functional koans for Clojure, I had a little Watchr script continually re-running the tests from the exercise at every file save. This is what that script looked like:

def run_it
system('sh run.sh')
end
run_it
watch( 'src/koans/.*\.clj' ) { run_it }
view raw snippet.rb hosted with ❤ by GitHub

There's barely anything there. All it does is run the "run.sh" script that comes with the koans whenever a ".clj" file in anywhere under the "src/koans" directory is saved. It's just a simple little thing, but so very useful.

I later discovered a fork of those koans (from David Laing) that adds JavaScript lessons. So I adapted my watchr script to start the KoansRunner file in Safari, reload the test page, and scroll to the bottom on every save of the JavaScript files.

def js_command(command)
"-e 'do JavaScript \"#{command}\" in first document'"
end
def reload
js_command("window.location.reload();")
end
def scroll
js_command("window.scrollTo(0, document.body.scrollHeight);")
end
def safari_cmd(*cmds)
"osascript -e 'tell app \"Safari\"' #{cmds.join(' ')} -e 'end tell'"
end
def reload_and_scroll
safari_cmd reload, scroll
end
def scroll_to_bottom
safari_cmd scroll
end
def run_it(command = scroll_to_bottom)
system("ps -xc|grep -sq Safari && #{command}")
end
system("open /Applications/Safari.app KoansRunner.html")
run_it
watch( 'koans/.*\.js' ) { run_it(reload_and_scroll) }
view raw snippet.rb hosted with ❤ by GitHub

I can't say enough about the value of rapid feedback from automated tests during the learning/development cycle. The absence of context switching to manually rerun the tests is great WIN.

1 comment:

Colin Jones said...

That's a great idea. Lazytest, a new testing framework for Clojure, has a watchr/autotest-like runner built in. I might convert the koans to use that rather than clojure.test, because it really is a bit of a hassle to run tests as things are - people are always disappointed they can't easily run them from their IDE without some setup.

Thanks for the tip!

p.s. I'd love to get some patches to add more koans now that you're done going through them! :)