[Rspec-devel] TextMate command for RSpec on Rails

Jonathan del Strother maillist at steelskies.com
Sun Jul 2 08:16:14 EDT 2006


Feel free to include it, though I suspect it could do with some  
cleaning up - this is my first TextMate plugin and I'm sure I'm  
ignoring all sorts of conventions and useful utility functions...

I've just come up with an updated version:


===

#!/usr/bin/env ruby
require 'cgi'

#try and remove unnecessary '..'s from paths.
#Eg collapse 'config/../app/controllers/../../public' to 'public'.   
Hopefully.
def rationalize_path(path)
   components = path.split("/")
   real_components = []
   components.each do |c|
     if (c == "..") && (real_components.size > 0) &&  
(real_components.last != "..")
       real_components.pop
     else
       real_components.push c
     end
   end

   File.join(real_components)
end



Dir.chdir(ENV['TM_PROJECT_DIRECTORY'])

output = `rake spec 2>&1`
output = CGI.escapeHTML(output)

#rationalize paths (strip out unnecessary '..'s etc)
output.gsub! /(\.\/|\/)[^:\n]+\.[^:\n]+/ do |m|
    rationalize_path(m)
end

#Find local file names and make them into proper links
#It ignores any paths that don't start with './'.
# (This is a feature, IMO - too many links if you find every single  
path in the output.)
output.gsub! /\.(\/[^:]+):([\d]+)/ do |m|
   path = Dir.pwd + $1
   "<a href=\"txmt://open?url=file://#{path}&line=#{$2}\">#{m}</a>"
end


#Find the result lines (x specification(s), y failure(s)) and style them
output.gsub! /^[\d]+ specifications?, [\d]+ failures?/ do |m|
   "<span class='result'>#{m}</span>"
end

#Remove unnecessary repetition of the project path:
output.gsub! /#{Dir.pwd}\//, './'


output.gsub!("\n", "<br/>")

puts <<END
<html>
<head>
   <title>rspec results</title>
   <style type='text/css'>
	body {font-size:0.8em}
	.result {font-weight:bold;}
   </style>
</head>
<body>
<h1>rspec</h1>
#{output}
</body>
</html>
END


==


which properly feeds stderr into the output, and makes a few attempts  
to cut down on the amount of unnecessary text - eg collapsing paths  
with '..' in them.    Oh, and the rspec results get their own style.


It's a shame we don't have a proper runner for this yet, to avoid  
setting up the rails environment every time you launch 'rake spec'.   
What happened to rspecRunner?  And is it suitable for running with  
rspec on rails?





On 2 Jul 2006, at 02:32, aslak hellesoy wrote:

> Hi Jonathan,
>
> This is great! I haven't had the chance to try it yet, but I will  
> next week.
>
> Would you be ok with us adding it to RSpce's svn? (vendor/textmate -
> to be distributed as a separate package from the Rspec gem). We'll
> credit you duly in the docs.
>
> Cheers,
> Aslak
>
> On 6/30/06, Jonathan del Strother <maillist at steelskies.com> wrote:
>> Don't know how many TextMate users we've got here, but:
>>
>> I was getting a little tired of tweaking a spec file, swapping to
>> Terminal, running specs, and swapping back again, so I came up with a
>> TextMate command to do this for me.  It runs 'rake spec' and display
>> the output, complete with clickable links to any files.
>>
>>
>>
>> ===
>>
>> #!/usr/bin/env ruby
>>
>> def process_for_html(string)
>>    string.gsub!("\n", "<br/>")
>>
>>    #try to find file names and make them into proper links
>>    string.gsub! /\.(\/[^:]*):([\d]*)/ do |m|
>>      path = Dir.pwd + $1
>>      "<a href=\"txmt://open?url=file://#{path}&line=#{$2}\">#{m}</a>"
>>    end
>>    string
>> end
>>
>> Dir.chdir(ENV['TM_PROJECT_DIRECTORY'])
>>
>> output = `rake spec`
>> output.gsub!("\n", "<br/>")
>>
>> #try to find file names and make them into proper links
>> output.gsub! /\.(\/[^:]*):([\d]*)/ do |m|
>>    path = Dir.pwd + $1
>>    "<a href=\"txmt://open?url=file://#{path}&line=#{$2}\">#{m}</a>"
>> end
>>
>> puts "<html><body style='font-size:0.8em'>#{"<h1>rspec</ 
>> h1>"+output}</
>> body></html>"
>>
>>
>> ===
>>
>>
>> Just create a new bundle command, and set its output to 'Show as
>> HTML', and you should be good to go.
>>
>>
>> There's a few limitations though - mainly that stderr isn't captured,
>> so any stderr gets dumped at the top of the file , without
>> formatting.  This includes the ruby invocation commands (ruby - 
>> Ilib "/
>> usr/local/lib/ruby/gems/1.8/gems/rspec-0.5.13/bin/spec" "spec/models/
>> active_user_spec.rb"), and any syntax errors in your specs.  I've
>> been trying for the past hour to properly capture stderr and
>> interpolate it into the normal output, without success.  popen3 just
>> gives you stdout and stderr as separate objects, with no way of re-
>> joining them up in the order they occurred.
>> So, if anyone has a fix to the stderr problem, or any other additions
>> & improvements, I'd love to see them.
>>
>>
>> _______________________________________________
>> Rspec-devel mailing list
>> Rspec-devel at rubyforge.org
>> http://rubyforge.org/mailman/listinfo/rspec-devel
>>
> _______________________________________________
> Rspec-devel mailing list
> Rspec-devel at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-devel

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2114 bytes
Desc: not available
Url : http://rubyforge.org/pipermail/rspec-devel/attachments/20060702/6cb4e49a/attachment-0001.bin 


More information about the Rspec-devel mailing list