git & rspec magic - Article #49
git & rspec magic
finding the culprit with bisect automagically

I guess most of you know how to use rspec. And some might have used the power of git bisect. But how can you make them play nicely together?

(In the following example, I am assuming that you use rake, rspec 1.x and bundler, could be a rails project, could be anything)

Well, the easy way is to just run all the spec via the bisect command. Start with

% git bisect start <bad-revision> <good-revision>

and then use

% git bisect run rake spec

until it shows you the bad commit. Use the power of git diff(tool) to compare the last good and the first bad commit, and you'll find the problem in short order.

But what if your test suite is huge, or the startup time for rake spec is not worth it? Well, then you can combine two really interesting flags for rspec: --format e and -e FILE. Example:

Before I prepare bisecting with bisect start ... as above, I am running the specs with the 'failing' formatter via

% spec <directory_or_files_to_run> -f e 2>/dev/null 1>/tmp/failing.txt

which will run the specified files and output the failing specification's descriptions to the file /tmp/failing.txt'. In the next step, I use

% git bisect run spec -e /tmp/failing.txt

thus letting git do the hard work of running the failing specs over and over until the culprit is found. This should be way faster than running the whole test suite altogether, but you also might miss some other failing specs, so beware! Running all specs again will be necessary, and then you might have to start all over again.

Oh, and for those that are missing an example for rspec-2: I'm working on it. It's a bit more involved, because the formatter has to be customized, and the input has to come from the shell (unless I am willing to patch rspec-core), but I am confident that I can get that done in time, too.

Hope that helped some of you, and stay tuned for the rspec-2 method of getting the same stuff done!

by Anton Bangratz at Tue Sep 27 09:44:26 +0200 2011
Comments
Creative Commons License Get Firefox Valid XHTML 1.0 Transitional