Skip to content
This repository
Browse code

add watchr

  • Loading branch information...
commit ea95408a0b0caf8e1279f8a00a01ff31249f4bc3 1 parent 68c23b2
Arthur Chiu authored

Showing 1 changed file with 67 additions and 0 deletions. Show diff stats Hide diff stats

  1. +67 0 spec.watchr
67 spec.watchr
... ... @@ -0,0 +1,67 @@
  1 +ENV["WATCHR"] = "1"
  2 +system 'clear'
  3 +
  4 +def growl(message)
  5 + growlnotify = `which growlnotify`.chomp
  6 + if not growlnotify.empty?
  7 + title = "Watchr Test Results"
  8 + image = message.include?('0 failures, 0 errors') ? "~/.watchr_images/passed.png" : "~/.watchr_images/failed.png"
  9 + options = "-w -n Watchr --image '#{File.expand_path(image)}' -m '#{message}' '#{title}'"
  10 + system %(#{growlnotify} #{options} &)
  11 + else
  12 + puts message
  13 + end
  14 +end
  15 +
  16 +def run(cmd)
  17 + puts(cmd)
  18 + `#{cmd}`
  19 +end
  20 +
  21 +def run_test_file(file)
  22 + system('clear')
  23 + result = run(%Q(ruby -I"lib:test" -rubygems #{file}))
  24 + growl result.split("\n").last rescue nil
  25 + puts result
  26 +end
  27 +
  28 +def run_all_tests
  29 + system('clear')
  30 + result = run "rake spec"
  31 + growl result.split("\n").last rescue nil
  32 + puts result
  33 +end
  34 +
  35 +def related_test_files(path)
  36 + Dir['spec/**/*.rb'].select { |file| file =~ /#{File.basename(path).split(".").first}_spec.rb/ }
  37 +end
  38 +
  39 +def run_suite
  40 + run_all_tests
  41 +end
  42 +
  43 +watch('spec/spec_helper\.rb') { run_all_tests }
  44 +watch('spec/(.*).*_spec\.rb') { |m| run_test_file(m[0]) }
  45 +watch('lib/.*/.*\.rb') { |m| related_test_files(m[0]).map {|tf| run_test_file(tf) } }
  46 +
  47 +# Ctrl-\
  48 +Signal.trap 'QUIT' do
  49 + puts " --- Running all tests ---\n\n"
  50 + run_all_tests
  51 +end
  52 +
  53 +@interrupted = false
  54 +
  55 +# Ctrl-C
  56 +Signal.trap 'INT' do
  57 + if @interrupted then
  58 + @wants_to_quit = true
  59 + abort("\n")
  60 + else
  61 + puts "Interrupt a second time to quit"
  62 + @interrupted = true
  63 + Kernel.sleep 1.5
  64 + # raise Interrupt, nil # let the run loop catch it
  65 + run_suite
  66 + end
  67 +end

0 comments on commit ea95408

Please sign in to comment.
Something went wrong with that request. Please try again.