Hi there! You've reached the homepage of Ben Lavender! Ben is a computery person who partakes in many kinds of nerd tomfoolery, but mostly programming! You've found his home on the web, with the caveat that a lot his day-to-day stuff ends up on social networks, so this site makes him seem pretty stodgy.
Enjoy your stay.


Running RSpec on Heroku

03 Apr 2010

Recently, while working on my rdf-do gem, a simple SQL backend for RDF.rb, I wanted to make sure it worked on Heroku. Easier said than done–Heroku demands a webapp, and RSpec hates that. But I fought it and fixed it.

First, in the root of your gem repository, add a Gemfile and a config.ru (examples are at the end). Then commit them, heroku create, and push. You’ll need to push from master initially; heroku won’t let you push to master from another branch until it already exists:

$ git commit -m "heroku stuff" Gemfile config.ru
$ heroku create mygemname
$ git push heroku master

Now, we don’t want this Heroku stuff cluttering up our repository in general. So make a heroku branch and clean up your master. You’ll never need to push the Heroku branch to github or anything.

$ git branch heroku
$ git reset --hard HEAD^1

You can update your heroku branch, and your app, whenever it is you want to test, by merging master and pushing your heroku branch to Heroku’s master branch:

$ git checkout heroku
$ git merge master
$ git push heroku heroku:master

Poof! Now you’re running your config.ru application. In my case, I wanted to run RSpec and get the html output. The config.ru I am using for my RDF::DataObjects gem is:

# sample heroku rackup


require 'spec'

app = proc do |env|

    io = StringIO.new
    basename = File.basename(env['REQUEST_PATH'])
    if basename.empty?
      files = Dir.glob(File.dirname(__FILE__) + '/spec/*.spec')
      files = ['spec/' + basename + ".spec"] unless basename =~ /spec$/

    options = ['--format','html',files].flatten
    puts "I couldn't find #{options.last}" unless File.exists?(options.last)

    parser = ::Spec::Runner::OptionParser.new(io, io)
    opts = parser.options
    ::Spec::Runner.use opts

  [ 200, {'Content-Type' => 'text/html'}, io.string ]

run app

There’s nothing special about the gemfile:

# Gemfile
source 'http://rubygems.org'
gem 'rdf-spec'
gem 'pg'
gem 'sqlite3'
gem 'data_objects'
gem 'do_sqlite3'
gem 'do_postgres'
gem 'rdf'
gem 'rspec'

This will run any test at appname.heroku.com/spec-file-name. For example, my rdf-do gem has a spec/sqlite3.spec file. You can see the output at http://rdfdo.heroku.com/sqlite3, or below, where a screenshot is perhaps embedded; I’m not a specialist here.

RSpec results
Uploaded with plasq's Skitch!