Saturday, April 28, 2012

Running Tests, Boodhoo Style

Watch this and weep at the beauty of Jean-Paul Boodhoo’s development environment.  In particular marvel at the whole “saving a file causes compilation, runs tests and lists failures in a toast pop-up” thing.  WANT.
JP publishes everything you see in the dnrTV episodes on github, so we can all learn.  He recorded a webinar about his setup, but hasn’t published the video yet.  I couldn’t wait so I went digging.
At the core is a Ruby script that checks every second for updated files and runs msbuild  to compile and then the MSpec test runner.  The script parses the MSpec runner’s output for failures and sends the text to Growl for Windows, which produced the toast notification.  It even customizes the icon that Growl to use depending on whether there are failures or not – a fiery (red) Mario for failures and a happy Mario for success.
It’s a somewhat complex system that does a few other things, like dissolving each subfolder in the main project into separate assemblies and batches some git commands.  I was particularly interested in implementing the Growl notification with the failed tests.
My setup:
  1. Growl for Windows installed in c:\utils\growl for windows
  2. JP’s Mario pictures (red.jpg, green.jpg) from github copied to c:\utils\growl for windows.
  3. MSpec extracted to C:\utils\Machine.Specifications-Release.
  4. runspecs.bat (content below) added to the tests project in my solution and set to always be copied to the output folder.
  5. A post build command on the tests project:
    $(TargetDir)runspecs.bat $(TargetFileName)
Now, every time you build your solution tests will run and failures, or a success message, are sent to Growl.
The batch:
@echo on
setlocal EnableDelayedExpansion
C:\utils\Machine.Specifications-Release\mspec-clr4.exe --html .\specs.html %1|find "FAIL">fail.txt
set fail=
for /f "tokens=*" %%a in (fail.txt) do set fail=!fail!\n%%a
if not "!fail!"=="" "c:\utils\growl for windows\growlnotify.exe" /t:Build /i:.\red.jpg "!fail!"
if "!fail!"=="" "c:\utils\growl for windows\growlnotify.exe" /t:Build /i:.\green.jpg "All passed"

Visual studio passes the name of the DLL with the tests in the variable %1.  The batch asks mspec-cl4.exe to create an html report of the test results into specs.html (for later examination if needed), and also picks out the lines in mspec-cl4.exe’s console output containing “FAIL”.  Those lines are piped to fail.txt.
Next, it for loops through the lines in fail.txt and builds a batch variable called fail.  Each line in fail.txt is separated by a “\n” so that Growl puts each one on a separate line.  The !fail! syntax, alone with the setlocal EnableDelayedExpansion command, causes the batch processor to update the contents of the fail variable each time through the loop.  The usual %fail% syntax won’t do this.
Finally the batch decides if there are any failures or not and sends the appropriate message to growlnotify.exe.
Now to learn VIM and be a keyboard junkie like JP!

No comments:

Post a Comment