Failing fast in rails with a smoke test initializer

The Rails app that I’m working on has a dependency on ImageMagick. In fact, if ImageMagick is either missing or broken in the app’s environment, the app is beyond repair and Rails shouldn’t even start.

To achieve this, I have added an initializer under config/initializers/dependency_tests.rb:

require 'minitest/unit'
require 'stringio'

class DependencyTests < MiniTest::Unit::TestCase
  def test_imagemagick_6_is_present
    assert_match /Version: ImageMagick 6/, `convert`.split("\n")[0], "ImageMagick 6 not present!"
  end
end

MiniTest::Unit.output = StringIO.new
MiniTest::Unit.new.run
unless ENV["SKIP_TESTS"]
  raise MiniTest::Unit.output.string if (MiniTest::Unit.runner.failures + MiniTest::Unit.runner.errors > 0)
end

Note: this particular example is specific to ruby 1.9.3 (in the way that minitest is used) but can be easily adapted to other versions of ruby.

If the test fails, then its error messages are rethrown on the console. This approach has already saved me quite a few minutes of troubleshooting (most recently yesterday when I was bringing everything up to scratch post the Mountain Lion upgrade).

%d bloggers like this: