2013-04-25 15:17:43 +08:00
|
|
|
require "drb/drb"
|
|
|
|
require "thread"
|
2013-04-26 09:46:48 +08:00
|
|
|
require "fileutils"
|
2013-04-25 15:17:43 +08:00
|
|
|
|
|
|
|
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" }
|
|
|
|
watch('spec/spec_helper.rb') { "spec" }
|
|
|
|
|
|
|
|
# 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" }
|
|
|
|
watch('app/controllers/application_controller.rb') { "spec/controllers" }
|
|
|
|
|
|
|
|
# Capybara request specs
|
|
|
|
watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/requests/#{m[1]}_spec.rb" }
|
|
|
|
|
|
|
|
|
2013-05-01 08:29:39 +08:00
|
|
|
def self.run(opts={})
|
|
|
|
self.new.run(opts)
|
2013-04-25 15:17:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
@queue = []
|
|
|
|
@mutex = Mutex.new
|
|
|
|
@signal = ConditionVariable.new
|
|
|
|
start_service_queue
|
|
|
|
end
|
|
|
|
|
2013-05-01 08:29:39 +08:00
|
|
|
def run(opts = {})
|
2013-04-25 15:17:43 +08:00
|
|
|
if already_running?(pid_file)
|
|
|
|
puts "autospec appears to be running, it is possible the pid file is old"
|
|
|
|
puts "if you are sure it is not running, delete #{pid_file}"
|
|
|
|
return
|
|
|
|
end
|
|
|
|
write_pid_file(pid_file, Process.pid)
|
|
|
|
|
|
|
|
start_spork
|
|
|
|
Signal.trap("HUP") {stop_spork; exit }
|
|
|
|
Signal.trap("SIGINT") {stop_spork; exit }
|
|
|
|
|
2013-05-01 08:29:39 +08:00
|
|
|
puts "Forced polling (slower) - inotify does not work on network filesystems, use local filesystem to avoid" if opts[:force_polling]
|
2013-04-30 11:25:55 +08:00
|
|
|
|
2013-05-01 08:04:47 +08:00
|
|
|
options = {filter: /^app|^spec|^lib/, relative_paths: true}
|
|
|
|
|
2013-05-01 08:29:39 +08:00
|
|
|
if opts[:force_polling]
|
|
|
|
options[:force_polling] = true
|
|
|
|
options[:latency] = opts[:latency] || 3
|
2013-05-01 08:04:47 +08:00
|
|
|
end
|
|
|
|
|
2013-04-25 15:17:43 +08:00
|
|
|
Thread.start do
|
2013-05-01 08:04:47 +08:00
|
|
|
Listen.to('.', options ) do |modified, added, removed|
|
2013-04-25 15:17:43 +08:00
|
|
|
process_change([modified, added].flatten.compact)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@mutex.synchronize do
|
|
|
|
@queue << ['spec', 'spec']
|
|
|
|
@signal.signal
|
|
|
|
end
|
|
|
|
|
2013-04-30 10:43:21 +08:00
|
|
|
Process.wait(@spork_pid)
|
|
|
|
puts "Spork has been terminated, exiting"
|
2013-04-25 15:17:43 +08:00
|
|
|
|
|
|
|
rescue => e
|
|
|
|
puts e
|
|
|
|
puts e.backtrace
|
|
|
|
stop_spork
|
|
|
|
end
|
|
|
|
|
2013-04-26 09:46:48 +08: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
|
2013-04-30 10:43:21 +08:00
|
|
|
t.join rescue nil
|
2013-04-26 09:46:48 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def force_polling?
|
|
|
|
works = false
|
|
|
|
|
|
|
|
begin
|
|
|
|
require 'rb-inotify'
|
2013-05-01 08:04:47 +08:00
|
|
|
require 'fileutils'
|
2013-04-26 09:46:48 +08:00
|
|
|
n = INotify::Notifier.new
|
2013-04-30 20:54:30 +08:00
|
|
|
FileUtils.touch('./tmp/test_polling')
|
2013-04-26 09:46:48 +08:00
|
|
|
|
2013-05-01 08:04:47 +08:00
|
|
|
n.watch("./tmp", :modify, :attrib){ works = true }
|
2013-04-26 09:46:48 +08:00
|
|
|
quit = false
|
|
|
|
Thread.new do
|
|
|
|
while !works && !quit
|
|
|
|
if IO.select([n.to_io], [], [], 0.1)
|
|
|
|
n.process
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-04-26 11:58:11 +08:00
|
|
|
sleep 0.01
|
2013-04-26 09:46:48 +08:00
|
|
|
|
2013-05-01 08:04:47 +08:00
|
|
|
FileUtils.touch('./tmp/test_polling')
|
2013-04-26 09:46:48 +08:00
|
|
|
wait_for(100) { works }
|
2013-05-01 08:04:47 +08:00
|
|
|
File.unlink('./tmp/test_polling')
|
2013-04-26 09:46:48 +08:00
|
|
|
n.stop
|
|
|
|
quit = true
|
|
|
|
rescue LoadError
|
|
|
|
#assume it works (mac)
|
|
|
|
works = true
|
|
|
|
end
|
|
|
|
|
2013-04-26 11:58:11 +08:00
|
|
|
!works
|
2013-04-26 09:46:48 +08:00
|
|
|
end
|
|
|
|
|
2013-04-25 15:17:43 +08:00
|
|
|
|
|
|
|
def process_change(files)
|
|
|
|
return unless files.length > 0
|
|
|
|
specs = []
|
2013-04-26 11:58:11 +08:00
|
|
|
hit = false
|
2013-04-25 15:17:43 +08:00
|
|
|
files.each do |file|
|
|
|
|
MATCHERS.each do |k,v|
|
|
|
|
if m = k.match(file)
|
2013-04-26 11:58:11 +08:00
|
|
|
hit = true
|
2013-04-25 15:17:43 +08:00
|
|
|
spec = v ? ( v.arity == 1 ? v.call(m) : v.call ) : file
|
|
|
|
if File.exists?(spec) || Dir.exists?(spec)
|
|
|
|
specs << [file, spec]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-04-26 11:58:11 +08:00
|
|
|
queue_specs(specs) if hit
|
2013-04-25 15:17:43 +08:00
|
|
|
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
|
|
|
|
spork_service.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-26 11:58:11 +08:00
|
|
|
if focus[1].include?(spec) || c != spec
|
2013-04-25 15:17:43 +08:00
|
|
|
@queue << focus
|
|
|
|
end
|
|
|
|
else
|
|
|
|
@queue << [c,spec]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@signal.signal
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-04-26 11:58:11 +08:00
|
|
|
def thread_loop
|
|
|
|
@mutex.synchronize do
|
|
|
|
last_failed = false
|
|
|
|
current = @queue.last
|
|
|
|
if current
|
|
|
|
result = run_spec(current[1])
|
|
|
|
if result == 0
|
|
|
|
@queue.pop
|
|
|
|
else
|
|
|
|
last_failed = true
|
|
|
|
if result.to_i > 0
|
|
|
|
focus_on_failed_tests
|
|
|
|
ensure_all_specs_will_run
|
|
|
|
end
|
|
|
|
end
|
|
|
|
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-04-25 15:17:43 +08:00
|
|
|
def start_service_queue
|
|
|
|
@worker ||= Thread.new do
|
|
|
|
while true
|
2013-04-26 11:58:11 +08:00
|
|
|
thread_loop
|
2013-04-25 15:17:43 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-04-26 09:46:48 +08:00
|
|
|
def focus_on_failed_tests
|
2013-04-26 11:58:11 +08:00
|
|
|
current = @queue.last
|
2013-04-26 09:46:48 +08: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
|
|
|
|
|
2013-04-25 15:17:43 +08:00
|
|
|
def failed_specs
|
|
|
|
specs = []
|
|
|
|
path = './tmp/rspec_result'
|
|
|
|
if File.exist?(path)
|
|
|
|
specs = File.open(path) { |file| file.read.split("\n") }
|
|
|
|
File.delete(path)
|
|
|
|
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
|
|
|
|
|
|
|
|
spork_service.run(args,$stderr,$stdout)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def spork_pid_file
|
|
|
|
Rails.root + "tmp/pids/spork.pid"
|
|
|
|
end
|
|
|
|
|
|
|
|
def pid_file
|
|
|
|
Rails.root + "tmp/pids/autospec.pid"
|
|
|
|
end
|
|
|
|
|
|
|
|
def already_running?(pid_file)
|
|
|
|
if File.exists? pid_file
|
|
|
|
pid = File.read(pid_file).to_i
|
|
|
|
Process.getpgid(pid) rescue nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def write_pid_file(file,pid)
|
|
|
|
FileUtils.mkdir_p(Rails.root + "tmp/pids")
|
|
|
|
File.open(file,'w') do |f|
|
|
|
|
f.write(pid)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def spork_running?
|
|
|
|
spork_service.port rescue nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def spork_service
|
|
|
|
|
|
|
|
unless @drb_listener_running
|
|
|
|
begin
|
|
|
|
DRb.start_service("druby://127.0.0.1:0")
|
|
|
|
rescue SocketError, Errno::EADDRNOTAVAIL
|
|
|
|
DRb.start_service("druby://:0")
|
|
|
|
end
|
|
|
|
|
|
|
|
@drb_listener_running = true
|
|
|
|
end
|
|
|
|
|
|
|
|
@spork_service ||= DRbObject.new_with_uri("druby://127.0.0.1:8989")
|
|
|
|
end
|
|
|
|
|
|
|
|
def stop_spork
|
|
|
|
pid = File.read(spork_pid_file).to_i
|
|
|
|
Process.kill("SIGHUP",pid)
|
|
|
|
end
|
|
|
|
|
|
|
|
def start_spork
|
|
|
|
|
|
|
|
if already_running?(spork_pid_file)
|
|
|
|
puts "Killing old orphan spork instance"
|
|
|
|
stop_spork
|
|
|
|
sleep 1
|
|
|
|
end
|
|
|
|
|
2013-04-30 10:43:21 +08:00
|
|
|
@spork_pid = Process.spawn({'RAILS_ENV' => 'test'}, "bundle exec spork")
|
2013-04-25 15:17:43 +08:00
|
|
|
write_pid_file(spork_pid_file, @spork_pid)
|
|
|
|
|
|
|
|
running = false
|
|
|
|
while !running
|
|
|
|
running = spork_running?
|
|
|
|
sleep 0.1
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|