quick script to move your test::units to specs
Posted by jacqui maher on December 25, 2007 at 01:25 AM
this isn’t drop dead gorgeous or anything, but it worked ok for what i needed to do in the amount of time i felt like spending on it. i’d love to see how someone would make it better though, as usual.
and argh, i really need to spend some time on the css here. it’s hard to read code i post, and i apologize about that. lame!
download it: teststospecs.rb
view it
1 <pre>
2 #!/usr/bin/env ruby
3
4 # This script moves the files from test/unit and test/functional to spec/models and spec/controllers
5
6 require File.expand_path(File.dirname(__FILE__) + '/../../config/environment')
7
8 class TestsToSpecs
9
10 def initialize
11 @tests_dir = File.join(RAILS_ROOT, 'test')
12 @specs_dir = File.join(RAILS_ROOT, 'spec')
13
14 @units_dir = Dir.new(File.join(@tests_dir, 'unit'))
15 @models_dir = Dir.new(File.join(@specs_dir, 'models'))
16 @funcs_dir = Dir.new(File.join(@tests_dir, 'functional'))
17 @ctrl_dir = Dir.new(File.join(@specs_dir, 'controllers'))
18 end
19
20 def convert
21 [{:s => @models_dir, :tu => @units_dir}, {:s => @ctrl_dir, :tu => @funcs_dir}].each do |dir_set|
22 make_tests_specs(dir_set)
23 end
24 end
25
26 def spec_file_name(test_file)
27 spec_file = test_file.gsub(/test/, "spec")
28 spec_file.gsub!(/unit/, "models")
29 spec_file.gsub!(/functional/, "controllers")
30 spec_file.gsub!(/_controller/, "")
31 spec_file
32 end
33
34 def make_tests_specs(dir_set)
35 dir = dir_set[:tu]
36 dir.each do |file|
37 unless file == "." or file == ".."
38
39 full_file = File.join(dir_set[:tu].path, file)
40 if File.directory? full_file
41 spec_file = spec_file_name(full_file)
42 FileUtils.mkdir_p(spec_file) unless File.exists? spec_file
43 make_tests_specs({:tu => Dir.new(full_file), :s => Dir.new(spec_file)})
44 else
45 new_spec = File.join(dir_set[:s].path, file.gsub(/_test/, "_spec"))
46 new_spec.gsub!(/_controller/, "")
47 puts "converting TestCase #{full_file} to ExampleGroup #{new_spec}\n"
48
49 old_file = File.new(full_file, "r+") # open for reading & writing
50
51 if File.exists?(new_spec)
52 puts "! a spec already exists for this test at #{new_spec}. Perhaps you should merge these by hand?\n"
53 else
54 new_file = File.new(new_spec, "w+") # create file for writing
55 old_file.each do |line|
56 if line =~ %r@require.*?test_helper@
57 new_file.puts "require '#{RAILS_ROOT}/test/test_helper'"
58 new_file.puts "require '#{RAILS_ROOT}/spec/spec_helper'"
59 elsif line =~ %r@class.*?TestCase@ and line !~ %r@Controller@
60 new_file.puts "describe 'transitioning from TestCase to ExampleGroup' do"
61 elsif line =~ %r@class\s*(.*?)Test@
62 new_file.puts "describe #{$1}, 'transitioning from TestCase to ExampleGroup' do"
63 new_file.puts "\tintegrate_views"
64 elsif line =~ %r@def setup@
65 new_file.puts "\tbefore do"
66 elsif line =~ %r@def test_(.*?)\n@
67 new_file.puts "\tit \"should #{$1.humanize.downcase}\" do"
68 elsif line =~ %r@assert_response :success@
69 new_file.puts "\t\tresponse.should be_success"
70 elsif line =~ %r@assert_response :redirect@
71 new_file.puts "\t\tresponse.should be_redirect"
72 elsif line =~ %r@assert_redirected_to (.*?)\n@
73 new_file.puts "\t\tresponse.should redirect_to(#{$1})"
74 elsif line =~ %r@assert_equal\s*(\[.*?\]), (.*?)[,\n]@
75 new_file.puts "\t\t#{$1}.should == #{$2}"
76 elsif line =~ %r@assert_equal\s*(.*?),\s*(.*?)[,\n]@mi
77 m1 = $1
78 m2 = $2
79 unless m1.nil? or m2.nil?
80 unless m1.match(/nil/) or m2.match(/nil/)
81 new_file.puts "#{m2}.should == #{m1}"
82 end
83 end
84 elsif line =~ %r@assert assigns\(:(.*?)\)$@
85 new_file.puts "assigns[:#{$1}].should be_true"
86 else
87 unless line =~ /^class.*?Controller.*?rescue_action.*?end$/i or line =~ /require '.*?_controller'/i or line =~ /^#/
88 new_file.puts line
89 end
90 end
91 end
92 new_file.close
93 end
94 old_file.close
95 # uncomment this if you want it to delete the old test files once it's finished.
96 #File.delete(full_file)
97 end
98 end
99 end
100
101 end
102 end
103
104 tts = TestsToSpecs.new
105 tts.convert
106
107 </pre>
Comments
There are 0 comments on this post. Post yours →
Post a comment
Required fields in bold.