Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- def usage(msg = nil)
- puts "Error: #{msg}\n" if msg
- puts "Usage: #{File.basename(__FILE__)} test files"
- puts
- puts "Will convert a existing test files with names like "
- puts
- puts " def test_should_do_stuff"
- puts " ..."
- puts " end"
- puts
- puts "to one using the new syntax: "
- puts
- puts " should \"be super cool\" do"
- puts " ..."
- puts " end"
- puts
- puts "Will run the tests before and after, ensuring that they don't break, and that the test counts"
- puts "are consistent"
- puts
- puts "A copy of the old file will be left under /tmp/ in case this script just seriously screws up"
- puts
- exit (msg ? 2 : 0)
- end
- require 'fileutils'
- usave if ARGV.empty?
- usage("This system doesn't have a /tmp directory. wtf?") unless File.directory?('/tmp')
- def run_tests(file)
- puts " running..."
- test_output = `ruby #{file}`
- unless $?.success?
- puts "Tests failed for #{file}"
- exit 2
- end
- count = test_output.grep(/ assertions, /).first.split[0]
- #puts "#{count} tests"
- return count
- end
- def make_tempfile(file)
- tmpfile = "/tmp/#{File.basename(file)}"
- puts " copying to #{tmpfile}"
- FileUtils.cp(file, tmpfile)
- return tmpfile
- end
- def convert_contents(tmpfile)
- contents = File.read(tmpfile)
- contents.gsub!(/def test_(\S*)/) do |match|
- parts = $1.split('_')
- if parts.first == 'should'
- parts.shift
- else
- puts " Needs renaming: should #{parts.join(' ')}"
- parts.unshift("RENAME ME:")
- end
- "should \"#{parts.join(' ')}\" do"
- end
- return contents
- end
- def write_file(file, contents)
- puts " writing new version"
- File.open(file, 'w') { |f| f.write(contents) }
- end
- ARGV.each do |file|
- unless File.exists?(file)
- puts "Can't find #{file}"
- next
- end
- puts "Converting #{file}"
- old_count = run_tests(file)
- tf = make_tempfile(file)
- write_file(file, convert_contents(tf))
- new_count = run_tests(file)
- if new_count != old_count
- puts "ERROR: #{old_count} tests before conversion, #{new_count} after. Something's wrong."
- exit 2
- end
- end
Add Comment
Please, Sign In to add comment