Friday, November 27, 2009

Grails autotest

A year ago I stumbled upon ZenTest / autotest, a simple, but great tool for test-driven development with Ruby or Rails. It runs a subset of your tests whenever you change some source, and keeps you notified whenever something fails.

There was only one problem. It works on ruby and rails only, not on groovy or grails. I assume you could set up Hudson or whatever to do something similar, but for small projects I need something much simpler than that.

So I wrote a tiny bash script that does kind of the same as autotest, but on a grails project. It runs all tests whenever some source change, and keeps nagging you with this message if it fails:



This notification will stick as long as the tests fails, and change into this when tests succeededs:



This notifiction will fade away.

Growl is used for notification. Here´s the script:

#!/bin/bash

#
# Run grails tests continously.
#
# Author trygve.amundsen@gmail.com
#
logfile=.grailsAutoTest.log
last_filestatus=""
imagesDir=`dirname $0`/images
curdir=`pwd`
project=`basename $curdir`
echo "Starting autotest for $project"

while true; do
    current_filestatus=`ls -lR grails-app lib scripts src test web-app/js web-app/css \
                       *GrailsPlugin.groovy | md5`

    if [ "$current_filestatus" != "$last_filestatus" ]
    then
        last_filestatus=$current_filestatus
        echo -n "Running tests..."
        grails test-app > $logfile 
        tail -3 $logfile | grep -q 'Tests PASSED'
        if [ $? -eq 0 ]
        then
            echo "Tests PASSED - waiting for changes..."
            growlnotify -m "Grails AutoTests succeeded" -t "Test $project" \ 
                        -d "$project" --image $imagesDir/succeed.jpg
        else
            last_filestatus="failed"
            tail -3 $logfile | head -1 
            growlnotify -s -m "Grails AutoTests failed" -t "Test $project" \
                        -d "$project" --image $imagesDir/fail-icon.png
        fi
    fi
    sleep 2
done


Maybe I'll come back with at groovy version of the script later on.

No comments:

Post a Comment