Guest User

Untitled

a guest
Apr 20th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. def usage(msg = nil)
  4. puts "Error: #{msg}\n" if msg
  5. puts "Usage: #{File.basename(__FILE__)} test files"
  6. puts
  7. puts "Will convert a existing test files with names like "
  8. puts
  9. puts " def test_should_do_stuff"
  10. puts " ..."
  11. puts " end"
  12. puts
  13. puts "to one using the new syntax: "
  14. puts
  15. puts " should \"be super cool\" do"
  16. puts " ..."
  17. puts " end"
  18. puts
  19. puts "Will run the tests before and after, ensuring that they don't break, and that the test counts"
  20. puts "are consistent"
  21. puts
  22. puts "A copy of the old file will be left under /tmp/ in case this script just seriously screws up"
  23. puts
  24. exit (msg ? 2 : 0)
  25. end
  26.  
  27. require 'fileutils'
  28.  
  29. usave if ARGV.empty?
  30. usage("This system doesn't have a /tmp directory. wtf?") unless File.directory?('/tmp')
  31.  
  32. def run_tests(file)
  33. puts " running..."
  34. test_output = `ruby #{file}`
  35. unless $?.success?
  36. puts "Tests failed for #{file}"
  37. exit 2
  38. end
  39. count = test_output.grep(/ assertions, /).first.split[0]
  40. #puts "#{count} tests"
  41. return count
  42. end
  43.  
  44. def make_tempfile(file)
  45. tmpfile = "/tmp/#{File.basename(file)}"
  46. puts " copying to #{tmpfile}"
  47. FileUtils.cp(file, tmpfile)
  48. return tmpfile
  49. end
  50.  
  51. def convert_contents(tmpfile)
  52. contents = File.read(tmpfile)
  53. contents.gsub!(/def test_(\S*)/) do |match|
  54. parts = $1.split('_')
  55. if parts.first == 'should'
  56. parts.shift
  57. else
  58. puts " Needs renaming: should #{parts.join(' ')}"
  59. parts.unshift("RENAME ME:")
  60. end
  61. "should \"#{parts.join(' ')}\" do"
  62. end
  63. return contents
  64. end
  65.  
  66. def write_file(file, contents)
  67. puts " writing new version"
  68. File.open(file, 'w') { |f| f.write(contents) }
  69. end
  70.  
  71. ARGV.each do |file|
  72. unless File.exists?(file)
  73. puts "Can't find #{file}"
  74. next
  75. end
  76.  
  77. puts "Converting #{file}"
  78.  
  79. old_count = run_tests(file)
  80.  
  81. tf = make_tempfile(file)
  82. write_file(file, convert_contents(tf))
  83.  
  84. new_count = run_tests(file)
  85.  
  86. if new_count != old_count
  87. puts "ERROR: #{old_count} tests before conversion, #{new_count} after. Something's wrong."
  88. exit 2
  89. end
  90. end
Add Comment
Please, Sign In to add comment