Jan 05 Simple Rack app to browse/run Specs/Features on browser

tags: rack rspec | comments

Rack: a Ruby Webserver Interface

Rack provides a minimal, modular and adaptable interface for developing web applications in Ruby. By wrapping HTTP requests and responses in the simplest way possible, it unifies and distills the API for web servers, web frameworks, and software in between (the so-called middleware) into a single method call.

A Rack application is an Ruby object (not a class) that responds to call. It takes exactly one argument, the environment and returns an Array of exactly three values: The status, the headers, and the body.

Rack comes with several Supported Handlers among them: Mongrel, Thin and Phusion Passenger. Supported Adapters include: Merb, Sinatra and Rails should ship with it in Rails 3. Read more at Rack page

With that said, Rack qualifies as an option for super light web apps.

Minimal app to run Specs directly in your browser

I find usefull to view Specs in their HTML format to:

  • give developers a fast overall idea of the logics a Spec guarantees.

if this gets ported over to run Cucumber features in browser you add:

  • the value of been able to have your Client browse the features list of the Product.

So the app is simply a CGI like wrapping of scripts to browse your spec directory and run spec.rb files living there. The app will look like this (pay attention to the call method):

require 'rubygems'
require 'rack/request'
require 'rack/response'

module Rack
  class RSpecHTML

    def call(env)
      req = Request.new(env)
      root = Dir.getwd
      path = req.env['PATH_INFO']
      if path =~ /.rb$/
        # clicking on an .rb runs it through spec
        spec_file = "spec/#{path}".gsub('//', '/')
        result = `spec #{spec_file} -f h`
        #TODO show console stderr
        result = 'sorry, there was a problem!' if result.empty?
      else
        # or we show the contents
        result = "contents of directory<br/>"
        Dir.entries("spec/#{path}").sort.each do |file|
          file_path = "#{path}/#{file}".gsub('//', '/')
          result << "<a href='#{file_path}'>#{file}</a><br/>"
        end
      end

      res = Response.new
      res.write "<title>specs on #{root}</title>"
      res.write "<ul>clicking on<li>directory: browses in</li>
                 <li>spec file: runs `spec SPEC_FILE -f h`</li></ul><br/>"
      res.write "path = #{path}<br/>"
      res.write result
      res.finish
    end

  end
end

if $0 == __FILE__
  require 'rack'
  require 'rack/showexceptions'
  Rack::Handler::WEBrick.run 
    Rack::ShowExceptions.new(Rack::Lint.new(Rack::RSpecHTML.new)),
    :Port => 9292
end

What we are simply doing is parse the url and if it ends in .rb we run it through spec if not is a directory and we go in.

Copy the script to your RAILS_ROOT/spec directory, start it with:

$ ruby -Ilib spec/rack_rspec_html.rb

and now visit http://localhost:9292 to see your Specs on your browser!

The code is available as a gist on github.

Update

I’ve added support to browse/run cucumber features to the app, check it out in gist

Right now you can:

  • browse your spec/ and features/ directory
  • run specs and feature files living there.

I’ve also moved the gists to a normal github repo rack rspec html

blog comments powered by Disqus