|
| 1 | +require 'rubygems' |
| 2 | +require 'rack/request' |
| 3 | +require 'rack/response' |
| 4 | + |
| 5 | +module Rack |
| 6 | + class RSpecHTML |
| 7 | + def call(env) |
| 8 | + @req = Request.new(env) |
| 9 | + root = Dir.getwd |
| 10 | + path = @req.env['PATH_INFO'] |
| 11 | + if path =~ /_spec.rb$/ |
| 12 | + # clicking on an .rb runs it through spec |
| 13 | + result = `spec #{clean_path(path)} -f h` |
| 14 | + #TODO show console stderr |
| 15 | + result = 'sorry, there was a problem!' if result.empty? |
| 16 | + elsif path =~ /.feature$/ |
| 17 | + # clicking on an .feature runs it through cucumber |
| 18 | + result = `cucumber #{clean_path(path)} -f html` |
| 19 | + #TODO show console stderr |
| 20 | + result = 'sorry, there was a problem!' if result.empty? |
| 21 | + else |
| 22 | + # or we show the contents |
| 23 | + result = "contents of directory<br/>" |
| 24 | + directory_path = "#{root}/#{clean_path(path)}" |
| 25 | + Dir.entries(directory_path).sort.each do |file| |
| 26 | + result << "<a href='#{path_in_dir(file)}'>#{file}</a><br/>" |
| 27 | + end |
| 28 | + end |
| 29 | + # and rendering |
| 30 | + res = Response.new |
| 31 | + res.write HELP |
| 32 | + res.write "path = #{path}<br/>" |
| 33 | + res.write result |
| 34 | + res.finish |
| 35 | + end |
| 36 | + |
| 37 | + HELP = <<-HELP |
| 38 | + <title>specs on #{Dir.getwd}</title> |
| 39 | + <ul>clicking on<li>directory: browses in</li> |
| 40 | + <li>spec file: runs `spec SPEC_FILE -f h`</li> |
| 41 | + <li>feature file: runs `cucumber FEATURE_FILE -f html`</li> |
| 42 | + </ul><br/> |
| 43 | + HELP |
| 44 | + |
| 45 | +private |
| 46 | + # we need to remove starting slash |
| 47 | + def clean_path(path) |
| 48 | + path.sub(/^\//,'') |
| 49 | + end |
| 50 | + #relative path inside a directory |
| 51 | + def path_in_dir(file) |
| 52 | + "#{@req.env['PATH_INFO']}/#{file}".gsub('//', '/') |
| 53 | + end |
| 54 | + end |
| 55 | +end |
| 56 | + |
| 57 | +if $0 == __FILE__ |
| 58 | + require 'rack' |
| 59 | + require 'rack/showexceptions' |
| 60 | + # Rack::Handler::Mongrel but then I loose console stdout |
| 61 | + Rack::Handler::WEBrick.run \ |
| 62 | + Rack::ShowExceptions.new(Rack::Lint.new(Rack::RSpecHTML.new)), |
| 63 | + :Port => 9292 |
| 64 | +end |
0 commit comments