watchr.rb 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. ENV['FOG_MOCK'] ||= 'true'
  2. ENV['AUTOTEST'] = 'true'
  3. ENV['WATCHR'] = '1'
  4. system 'clear'
  5. def growl(message)
  6. growlnotify = `which growlnotify`.chomp
  7. title = "Watchr Test Results"
  8. image = case message
  9. when /(\d+)\s+?(failure|error)/i
  10. ($1.to_i == 0) ? "~/.watchr_images/passed.png" : "~/.watchr_images/failed.png"
  11. else
  12. '~/.watchr_images/unknown.png'
  13. end
  14. options = "-w -n Watchr --image '#{File.expand_path(image)}' -m '#{message}' '#{title}'"
  15. system %(#{growlnotify} #{options} &)
  16. end
  17. def run(cmd)
  18. puts(cmd)
  19. `#{cmd}`
  20. end
  21. def run_spec_test(file)
  22. if File.exist? file
  23. result = run "rspec --format p --color #{file}"
  24. growl result.split("\n").last
  25. puts result
  26. else
  27. puts "FIXME: No test #{file} [#{Time.now}]"
  28. end
  29. end
  30. def filter_rspec(data)
  31. data.split("\n").find_all do |l|
  32. l =~ /^(\d+)\s+exampl\w+.*?(\d+).*?failur\w+.*?(\d+).*?pending/
  33. end.join("\n")
  34. end
  35. def run_all_tests
  36. system('clear')
  37. files = Dir.glob("spec/**/*_spec.rb").join(" ")
  38. result = run "rspec #{files}"
  39. growl_results = filter_rspec result
  40. growl growl_results
  41. puts result
  42. puts "GROWL: #{growl_results}"
  43. end
  44. # Ctrl-\
  45. Signal.trap 'QUIT' do
  46. puts " --- Running all tests ---\n\n"
  47. run_all_tests
  48. end
  49. @interrupted = false
  50. # Ctrl-C
  51. Signal.trap 'INT' do
  52. if @interrupted then
  53. @wants_to_quit = true
  54. abort("\n")
  55. else
  56. puts "Interrupt a second time to quit"
  57. @interrupted = true
  58. Kernel.sleep 1.5
  59. # raise Interrupt, nil # let the run loop catch it
  60. run_suite
  61. end
  62. end
  63. def file2spec(file)
  64. result = file.sub('lib/puppet/', 'spec/unit/puppet/').gsub(/\.rb$/, '_spec.rb')
  65. result = file.sub('lib/facter/', 'spec/unit/facter/').gsub(/\.rb$/, '_spec.rb')
  66. end
  67. watch( 'spec/.*_spec\.rb' ) do |md|
  68. #run_spec_test(md[0])
  69. run_all_tests
  70. end
  71. watch( 'lib/.*\.rb' ) do |md|
  72. # run_spec_test(file2spec(md[0]))
  73. run_all_tests
  74. end