discourse/lib/autospec/runner.rb

309 lines
6.7 KiB
Ruby
Raw Normal View History

require "drb/drb"
require "thread"
2013-04-25 21:46:48 -04:00
require "fileutils"
require "autospec/reload_css"
2013-10-23 19:06:05 -04:00
require "autospec/base_runner"
require "autospec/simple_runner"
require "autospec/spork_runner"
module Autospec; end
class Autospec::Runner
MATCHERS = {}
def self.watch(pattern, &blk)
MATCHERS[pattern] = blk
end
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/components/#{m[1]}_spec.rb" }
# Rails example
watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb" }
watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
2013-08-05 16:34:42 -04:00
watch("app/controllers/application_controller.rb") { "spec/controllers" }
# Capybara request specs
watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/requests/#{m[1]}_spec.rb" }
# Fabrication
watch(%r{^spec/fabricators/(.+)_fabricator\.rb$}) { "spec" }
RELOAD_MATCHERS = Set.new
def self.watch_reload(pattern)
RELOAD_MATCHERS << pattern
end
watch_reload('spec/spec_helper.rb')
watch_reload('config/(.*).rb')
watch_reload(%r{app/helpers/(.*).rb})
def self.run(opts={})
self.new.run(opts)
end
def initialize
@queue = []
@mutex = Mutex.new
@signal = ConditionVariable.new
start_service_queue
end
def run(opts = {})
puts "Forced polling (slower) - inotify does not work on network filesystems, use local filesystem to avoid" if opts[:force_polling]
2013-04-29 23:25:55 -04:00
if ENV["SPORK"] == "0"
puts "Using Simple Runner"
2013-10-23 19:06:05 -04:00
@runner = Autospec::SimpleRunner.new
else
puts "Using Spork Runner"
2013-10-23 19:06:05 -04:00
@runner = Autospec::SporkRunner.new
end
@runner.start
Signal.trap("HUP") {@runner.stop; exit }
Signal.trap("SIGINT") {@runner.stop; exit }
2013-04-30 20:04:47 -04:00
options = {filter: /^app|^spec|^lib/, relative_paths: true}
if opts[:force_polling]
options[:force_polling] = true
options[:latency] = opts[:latency] || 3
2013-04-30 20:04:47 -04:00
end
Thread.start do
2013-04-30 20:04:47 -04:00
Listen.to('.', options ) do |modified, added, removed|
process_change([modified, added].flatten.compact)
end
end
@mutex.synchronize do
@queue << ['spec', 'spec']
@signal.signal
end
2013-10-23 19:06:05 -04:00
while @runner.running?
2013-08-05 16:34:42 -04:00
process_queue
end
rescue => e
puts e
puts e.backtrace
2013-10-23 19:06:05 -04:00
@runner.stop
end
2013-08-05 16:34:42 -04:00
def process_queue
STDIN.gets
if @queue.length == 0
@queue << ['spec', 'spec']
@signal.signal
else
specs = failed_specs(:delete => false)
puts
puts
if specs.length == 0
puts "No specs have failed yet!"
puts
else
puts "The following specs have failed: "
specs.each do |s|
puts s
end
puts
queue_specs(specs.zip specs)
2013-08-05 16:34:42 -04:00
end
end
end
2013-04-25 21:46:48 -04:00
def wait_for(timeout_milliseconds)
timeout = (timeout_milliseconds + 0.0) / 1000
finish = Time.now + timeout
t = Thread.new do
while Time.now < finish && !yield
sleep(0.001)
end
end
t.join rescue nil
2013-04-25 21:46:48 -04:00
end
def force_polling?
works = false
begin
require 'rb-inotify'
2013-04-30 20:04:47 -04:00
require 'fileutils'
2013-04-25 21:46:48 -04:00
n = INotify::Notifier.new
FileUtils.touch('./tmp/test_polling')
2013-04-25 21:46:48 -04:00
2013-04-30 20:04:47 -04:00
n.watch("./tmp", :modify, :attrib){ works = true }
2013-04-25 21:46:48 -04:00
quit = false
Thread.new do
while !works && !quit
if IO.select([n.to_io], [], [], 0.1)
n.process
end
end
end
2013-04-25 23:58:11 -04:00
sleep 0.01
2013-04-25 21:46:48 -04:00
2013-04-30 20:04:47 -04:00
FileUtils.touch('./tmp/test_polling')
2013-04-25 21:46:48 -04:00
wait_for(100) { works }
2013-04-30 20:04:47 -04:00
File.unlink('./tmp/test_polling')
2013-04-25 21:46:48 -04:00
n.stop
quit = true
rescue LoadError
#assume it works (mac)
works = true
end
2013-04-25 23:58:11 -04:00
!works
2013-04-25 21:46:48 -04:00
end
def process_change(files)
return unless files.length > 0
2013-10-23 19:06:05 -04:00
specs = []
2013-04-25 23:58:11 -04:00
hit = false
files.each do |file|
RELOAD_MATCHERS.each do |k|
if k.match(file)
2013-10-23 19:06:05 -04:00
@runner.reload
return
end
end
MATCHERS.each do |k,v|
if m = k.match(file)
2013-04-25 23:58:11 -04:00
hit = true
spec = v ? ( v.arity == 1 ? v.call(m) : v.call ) : file
if File.exists?(spec) || Dir.exists?(spec)
specs << [file, spec]
end
end
end
Autospec::ReloadCss::MATCHERS.each do |k,v|
matches = []
if k.match(file)
matches << file
end
Autospec::ReloadCss.run_on_change(matches) if matches.present?
end
end
2013-04-25 23:58:11 -04:00
queue_specs(specs) if hit
rescue => e
p "failed in watcher"
p e
p e.backtrace
end
def queue_specs(specs)
if specs.length == 0
locked = @mutex.try_lock
if locked
@signal.signal
@mutex.unlock
end
return
else
2013-10-23 19:06:05 -04:00
@runner.abort
end
@mutex.synchronize do
specs.each do |c,spec|
@queue.delete([c,spec])
if @queue.last && @queue.last[0] == "focus"
focus = @queue.pop
@queue << [c,spec]
2013-04-25 23:58:11 -04:00
if focus[1].include?(spec) || c != spec
@queue << focus
end
else
@queue << [c,spec]
end
end
@signal.signal
end
end
2013-04-25 23:58:11 -04:00
def thread_loop
@mutex.synchronize do
last_failed = false
current = @queue.last
if current
2013-08-14 21:11:14 -04:00
last_failed = process_spec(current[1])
2013-04-25 23:58:11 -04:00
end
wait = @queue.length == 0 || last_failed
@signal.wait(@mutex) if wait
end
rescue => e
p "DISASTA PASTA"
puts e
puts e.backtrace
end
2013-08-14 21:11:14 -04:00
def process_spec(spec)
last_failed = false
result = run_spec(spec)
if result == 0
2013-08-14 21:11:14 -04:00
@queue.pop
else
last_failed = true
if result.to_i > 0
focus_on_failed_tests
ensure_all_specs_will_run
end
end
last_failed
end
def start_service_queue
@worker ||= Thread.new do
while true
2013-04-25 23:58:11 -04:00
thread_loop
end
end
end
2013-04-25 21:46:48 -04:00
def focus_on_failed_tests
2013-04-25 23:58:11 -04:00
current = @queue.last
2013-04-25 21:46:48 -04:00
specs = failed_specs[0..10]
if current[0] == "focus"
@queue.pop
end
@queue << ["focus", specs.join(" ")]
end
def ensure_all_specs_will_run
unless @queue.any?{|s,t| t == 'spec'}
@queue.unshift(['spec','spec'])
end
end
def failed_specs(opts={:delete => true})
specs = []
path = './tmp/rspec_result'
if File.exist?(path)
specs = File.open(path) { |file| file.read.split("\n") }
File.delete(path) if opts[:delete]
end
specs
end
def run_spec(specs)
File.delete("tmp/rspec_result") if File.exists?("tmp/rspec_result")
args = ["-f", "progress", specs.split(" "),
"-r", "#{File.dirname(__FILE__)}/formatter.rb",
"-f", "Autospec::Formatter"].flatten
2013-10-23 19:06:05 -04:00
@runner.run(args, specs)
end
end