Guest User

Untitled

a guest
Jun 19th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. require 'rubygems'
  2. require 'growl'
  3.  
  4. # If your spec runner is at a different location: customize it here..
  5. RUNNER = "./bin/spec"
  6.  
  7.  
  8. system 'clear'
  9.  
  10. # ---------
  11. # Signals
  12. # ---------
  13.  
  14. Signal.trap('QUIT') { run_all_specs(File.join(Dir.pwd, 'spec')) } # Ctrl-\
  15. Signal.trap('INT' ) { abort("\nBye.\n") } # Ctrl-C
  16.  
  17. # ---------
  18. # Rules
  19. # ---------
  20.  
  21. watch( '^spec/(.*)_spec\.coffee' ) do |md|
  22. run "Runnning: #{md[0]}" do
  23. `#{RUNNER} #{md[0]}`
  24. end
  25. end
  26.  
  27. watch( '^lib/(.*)\.coffee' ) do |md|
  28. spec = File.join('spec', File.dirname(md[0]), "#{File.basename(md[0], '.coffee')}_spec.coffee")
  29. if File.exist?(spec)
  30. run "Running: #{spec}" do
  31. `#{RUNNER} #{spec}`
  32. end
  33. end
  34. end
  35.  
  36.  
  37. # ---------
  38. # Helpers
  39. # ---------
  40.  
  41. def run_all_specs(target)
  42. run "Running all in: #{target}" do
  43. `#{RUNNER} #{target}`
  44. end
  45. end
  46.  
  47.  
  48. def run(description, &block)
  49. puts "#{description}"
  50.  
  51. result = parse_result(block.call)
  52.  
  53. if result[:tests] =~ /\d/
  54. if $?.success? && result[:success]
  55. title = "Jasmine Specs Passed!"
  56. img = "~/.watchr/success.png"
  57. else
  58. title = "Jasmine Specs Failed!"
  59. img = "~/.watchr/failed.png"
  60. end
  61.  
  62. specs_count = pluralize(result[:assertions], "example", "examples")
  63. failed_count = pluralize(result[:failures], "failure", "failures")
  64.  
  65. growl(title, "#{specs_count}, #{failed_count}", img)
  66. else
  67. growl("Running Specs Failed!", "Runner returned an error..")
  68. end
  69. end
  70.  
  71. def pluralize(count, singular, plural)
  72. count == "1" ? "#{count} #{singular}" : "#{count} #{plural}"
  73. end
  74.  
  75. def growl(title, message, image_path = nil)
  76. image_path = File.expand_path(image_path) if image_path
  77.  
  78. roar = Growl.new
  79. roar.message = message
  80. roar.title = title
  81. roar.image = image_path if image_path && File.exist?(image_path)
  82. roar.run if Growl.installed?
  83. end
  84.  
  85. def parse_result(result)
  86. puts result
  87. duration = result.scan(/Finished in (\d.\d+) seconds/).flatten.first
  88. tests, assertions, failures = result.scan(/(\d+) tests?, (\d+) assertions?, (\d+) failures?/).flatten
  89. {
  90. :tests => tests,
  91. :assertions => assertions,
  92. :failures => failures,
  93. :success => failures == "0",
  94. :duration => duration
  95. }
  96. end
Add Comment
Please, Sign In to add comment