Faster TDD With Zeus & TextMate


My Beloved TextMate

TextMate is my favorite tool as a programmer. I have been using it since 2007 and it was the reason I purchased my first Mac.

For the uninitiated, TextMate is a blazingly fast text editor and makes no claims of being an IDE . But don’t let the term text editor fool you, it is also wildly extensible by way of “bundles”. All of TextMate’s language grammars, framework intelligence (Rails, JQuery, etc.), source control, and UI components can be enhanced with bundles.

The power of the platform is limited only by your own imagination.

Did I mention that’s it’s now completely Open Source ? It’s beautiful, people, really.

But let’s get down to business…

This is TDD – We Need A Test!

Let’s begin by creating a simple test case for a fictional password_policy class.

Now we’ll execute the test, prefixing it with time to get a benchmark:

time ruby test test/unit/my_password_test.rb

First Pass Results:

We’ve got 7 tests (40 assertions) and it took nearly 8 seconds to execute. The time it took to run the actual tests was 0.023 seconds, the remainder was just boot time. Also, we had to run this from a console instead of staying in our code. Bummer.

Ruby → Run

One of the many bundles included with TextMate is Ruby. It contains numerous commands and snippets but the one we’re going to focus on is simply called “ Run ”. Run does just what’d you think it does — it executes the current document using your installed Ruby runtime.

Side note: TextMate shows no favoritism toward Ruby. It plays well with everything from Python to CoffeeScript. The Run ( CMD + R) command is available in nearly every language bundle.

If I tell TextMate to execute this code with Run ( CMD + R) the following window appears:

Can you see the 11 seconds of hell boredom I have to endure to get the result of this single unit test?

The “Finished in 0.024933 seconds.” line is the amount of time it took to actually run the tests themselves. The remaining 10.97 seconds was the time it took for TextMate to boot up rails runner behind the scenes.

We were able to execute the test from within TextMate but let’s be honest, this still sucks.

There has to be a better way, right?

BOOM ! ZEUS !


Zeus is a rails pre-loader. It boots multiple instances of your app and gives you wrapper commands (rake, generate, etc.) that make your development environment very fast .

Browse on over to GitHub and read up on Burke’s instructions on how to get started.

Pay special attention to the advice about removing any references to autorun in your test_helper.rb/spec_helper.rb file otherwise you’ll end up running all your code twice.

Bogus.

ZEUS IT!

To begin, start up Zeus from the console with:

zeus start

You should see something like this:

Let’s try it out:

zeus test test/unit/my_password_test.rb

Do you see how much faster this is?

We’re gone from just shy of 8 seconds to 1.5 seconds. That’s a nice improvement but what else can we do?

Make TextMate Use Zeus!

This process takes a bit of effort but it’s well worth it. Follow with me:

  • Open a console and type which zeus (then hit enter)
  • The result should be something like this:

/Users/sethmacpherson/.rvm/gems/ruby-1.9.3-p385/bin/zeus

  • Copy the file path
  • Open TextMate preferences
  • Click Variables
  • Add a new variable called “ ZEUS ” with the value of the zeus path above
    At this point you now have an ENV variable that can be referenced within scripts to your Zeus command. Nice.
  • Save the contents of this gist to:

~/Application Support/Avian/Bundles/Ruby.tmbundle/Support/RubyMate/run_script_with_zeus.rb

  • Next up click Bundle Edit Bundles ( CMD + OPTION + SHIFT + B)
  • Locate Ruby Bundle
  • Click Menu Actions
  • Locate Run
  • Remove the existing command text and replace with this gist

  • Type CMD + S to save or you’ll lose your changes

The Result

If we go back to our trivial test case from earlier and hit CMD + R we see the following:

YAY ! Zeus + TextMate == ‘Happiness’

And finally, this is my rendition of Zeus holding TextMate instead of a lightning bolt.

Extra Credit

TextMate has a sibling command to Run called Run Focused Unit Test . This means you can have a dozen test methods within a given file and ask TextMate to execute just the one you have focused.

The shortcut is CMD + SHIFT + R.

The Ruby bundle command can also be edited to take advantage of Zeus(y) goodness with the following gist.

Conclusion

Even if you don’t fancy TextMate as much as I do go check out Zeus and send Burke a big thanks.

HiringThing

Author: HiringThing

HiringThing is easy to use, intuitive online recruiting software that makes it easy to post jobs online, manage applicants and hire great employees.