From 69db81746fee54e14ee3e46f2fc31e93c096d529 Mon Sep 17 00:00:00 2001 From: Luke Kanies Date: Sat, 9 Apr 2011 15:04:23 -0700 Subject: Fixing the watchr script It had not been updated for quite a while. Signed-off-by: Luke Kanies Reviewed-by: Daniel Pittman --- autotest/watcher.rb | 138 ---------------------------------------------------- spec/spec.opts | 2 - spec/watchr.rb | 138 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 138 insertions(+), 140 deletions(-) delete mode 100644 autotest/watcher.rb create mode 100644 spec/watchr.rb diff --git a/autotest/watcher.rb b/autotest/watcher.rb deleted file mode 100644 index 9f89a448c..000000000 --- a/autotest/watcher.rb +++ /dev/null @@ -1,138 +0,0 @@ -ENV["WATCHR"] = "1" -ENV['AUTOTEST'] = 'true' - -def run_comp(cmd) - puts cmd - results = [] - old_sync = $stdout.sync - $stdout.sync = true - line = [] - begin - open("| #{cmd}", "r") do |f| - until f.eof? do - c = f.getc - putc c - line << c - if c == ?\n - results << if RUBY_VERSION >= "1.9" then - line.join - else - line.pack "c*" - end - line.clear - end - end - end - ensure - $stdout.sync = old_sync - end - results.join -end - -def clear - #system("clear") -end - -def growl(message, status) - # Strip the color codes - message.gsub!(/\[\d+m/, '') - - growlnotify = `which growlnotify`.chomp - return if growlnotify.empty? - title = "Watchr Test Results" - image = status == :pass ? "autotest/images/pass.png" : "autotest/images/fail.png" - options = "-w -n Watchr --image '#{File.expand_path(image)}' -m '#{message}' '#{title}'" - system %(#{growlnotify} #{options} &) -end - -def file2specs(file) - %w{spec/unit spec/integration}.collect { |d| - file.sub('lib/puppet', d) - }.find_all { |f| - File.exist?(f) - } -end - -def file2test(file) - result = file.sub('lib/puppet', 'test') - return nil unless File.exist?(result) - result -end - -def run_spec(command) - clear - result = run_comp(command).split("\n").last - status = result.include?('0 failures') ? :pass : :fail - growl result, status -end - -def run_test(command) - clear - result = run_comp(command).split("\n").last - status = result.include?('0 failures, 0 errors') ? :pass : :fail - growl result.split("\n").last rescue nil -end - -def run_test_file(file) - run_test(%Q(#{file})) -end - -def run_spec_files(files) - files = Array(files) - return if files.empty? - opts = File.readlines('spec/spec.opts').collect { |l| l.chomp }.join(" ") - run_spec("spec #{files.join(' ')}") -end - -def run_all_tests - run_test("rake unit") -end - -def run_all_specs - run_test("rake spec") -end - -def run_suite - run_all_tests - run_all_specs -end - -watch('spec/spec_helper.rb') { run_all_specs } -watch(%r{^spec/(unit|integration)/.*\.rb$}) { |md| run_spec_files(md[0]) } -watch(%r{^lib/puppet/(.*)\.rb$}) { |md| - run_spec_files(file2specs(md[0])) - if t = file2test(md[0]) - run_test_file(t) - end -} -watch(%r{^spec/lib/spec.*}) { |md| run_all_specs } -watch(%r{^spec/lib/monkey_patches/.*}) { |md| run_all_specs } -watch(%r{test/.+\.rb}) { |md| - if md[0] =~ /\/lib\// - run_all_tests - else - run_test_file(md[0]) - end -} - -# Ctrl-\ -Signal.trap 'QUIT' do - puts " --- Running all tests ---\n\n" - run_suite -end - -@interrupted = false - -# Ctrl-C -Signal.trap 'INT' do - if @interrupted - @wants_to_quit = true - abort("\n") - else - puts "Interrupt a second time to quit; wait for rerun of tests" - @interrupted = true - Kernel.sleep 1.5 - # raise Interrupt, nil # let the run loop catch it - run_suite - end -end diff --git a/spec/spec.opts b/spec/spec.opts index 91cd6427e..425f0edd3 100644 --- a/spec/spec.opts +++ b/spec/spec.opts @@ -1,6 +1,4 @@ --format s --colour ---loadby -mtime --backtrace diff --git a/spec/watchr.rb b/spec/watchr.rb new file mode 100644 index 000000000..bad89b088 --- /dev/null +++ b/spec/watchr.rb @@ -0,0 +1,138 @@ +ENV["WATCHR"] = "1" +ENV['AUTOTEST'] = 'true' + +def run_comp(cmd) + puts cmd + results = [] + old_sync = $stdout.sync + $stdout.sync = true + line = [] + begin + open("| #{cmd}", "r") do |f| + until f.eof? do + c = f.getc + putc c + line << c + if c == ?\n + results << if RUBY_VERSION >= "1.9" then + line.join + else + line.pack "c*" + end + line.clear + end + end + end + ensure + $stdout.sync = old_sync + end + results.join +end + +def clear + #system("clear") +end + +def growl(message, status) + # Strip the color codes + message.gsub!(/\[\d+m/, '') + + growlnotify = `which growlnotify`.chomp + return if growlnotify.empty? + title = "Watchr Test Results" + image = status == :pass ? "autotest/images/pass.png" : "autotest/images/fail.png" + options = "-w -n Watchr --image '#{File.expand_path(image)}' -m '#{message}' '#{title}'" + system %(#{growlnotify} #{options} &) +end + +def file2specs(file) + %w{spec/unit spec/integration}.collect { |d| + file.sub('lib/puppet', d).sub(".rb", "_spec.rb") + }.find_all { |f| + File.exist?(f) + } +end + +def file2test(file) + result = file.sub('lib/puppet', 'test') + return nil unless File.exist?(result) + result +end + +def run_spec(command) + clear + result = run_comp(command).split("\n").last + status = result.include?('0 failures') ? :pass : :fail + growl result, status +end + +def run_test(command) + clear + result = run_comp(command).split("\n").last + status = result.include?('0 failures, 0 errors') ? :pass : :fail + growl result.split("\n").last rescue nil +end + +def run_test_file(file) + run_test(%Q(#{file})) +end + +def run_spec_files(files) + files = Array(files) + return if files.empty? + opts = File.readlines('spec/spec.opts').collect { |l| l.chomp }.join(" ") + run_spec("rspec #{opts} --tty #{files.join(' ')}") +end + +def run_all_tests + run_test("rake unit") +end + +def run_all_specs + run_test("rake spec") +end + +def run_suite + run_all_tests + run_all_specs +end + +watch('spec/spec_helper.rb') { run_all_specs } +watch(%r{^spec/(unit|integration)/.*\.rb$}) { |md| run_spec_files(md[0]) } +watch(%r{^lib/puppet/(.*)\.rb$}) { |md| + run_spec_files(file2specs(md[0])) + if t = file2test(md[0]) + run_test_file(t) + end +} +watch(%r{^spec/lib/spec.*}) { |md| run_all_specs } +watch(%r{^spec/lib/monkey_patches/.*}) { |md| run_all_specs } +watch(%r{test/.+\.rb}) { |md| + if md[0] =~ /\/lib\// + run_all_tests + else + run_test_file(md[0]) + end +} + +# Ctrl-\ +Signal.trap 'QUIT' do + puts " --- Running all tests ---\n\n" + run_suite +end + +@interrupted = false + +# Ctrl-C +Signal.trap 'INT' do + if @interrupted + @wants_to_quit = true + abort("\n") + else + puts "Interrupt a second time to quit; wait for rerun of tests" + @interrupted = true + Kernel.sleep 1.5 + # raise Interrupt, nil # let the run loop catch it + run_suite + end +end -- cgit