2011-08-04 17:35:32 +00:00
|
|
|
require 'guard'
|
2011-08-24 15:36:38 +00:00
|
|
|
|
2011-08-04 17:35:32 +00:00
|
|
|
require 'guard/guard'
|
|
|
|
|
|
|
|
require 'hydra'
|
|
|
|
require 'hydra/master'
|
|
|
|
|
|
|
|
class Guard::Hydra < Guard::Guard
|
|
|
|
MATCHERS = {
|
|
|
|
:rspec => '**/*_spec.rb'
|
|
|
|
}
|
|
|
|
|
|
|
|
def initialize(watchers = [], options = {})
|
|
|
|
super
|
|
|
|
@options = {
|
|
|
|
:runner_log => 'hydra-runner.log',
|
|
|
|
:clear_runner_log => true,
|
|
|
|
:show_runner_log => true,
|
|
|
|
:hydra_config => 'config/hydra.yml',
|
|
|
|
:test_matchers => [ :rspec ],
|
2011-08-04 18:17:09 +00:00
|
|
|
:all_on_start => false,
|
2011-08-31 15:58:04 +00:00
|
|
|
:env => 'test',
|
|
|
|
:verbose => false
|
2011-08-04 17:35:32 +00:00
|
|
|
}.merge(@options)
|
|
|
|
end
|
|
|
|
|
|
|
|
def start
|
|
|
|
super
|
|
|
|
Guard::UI.info "Guard::Hydra is waiting to run tests..."
|
2011-08-04 18:17:09 +00:00
|
|
|
|
|
|
|
begin
|
|
|
|
ENV['RAILS_ENV'] = @options[:env]
|
|
|
|
require rails_application
|
|
|
|
rescue LoadError
|
|
|
|
Guard::UI.info "Not a Rails app, using default environment settings"
|
|
|
|
end
|
|
|
|
|
2011-08-07 17:17:05 +00:00
|
|
|
@did_fail = false
|
|
|
|
|
2011-08-04 17:35:32 +00:00
|
|
|
run_all if @options[:all_on_start]
|
|
|
|
end
|
|
|
|
|
|
|
|
def run_on_change(files = [])
|
2011-08-07 16:51:42 +00:00
|
|
|
if !(files = ensure_files(files)).empty?
|
|
|
|
Guard::UI.info "Running Hydra on #{files.join(', ')}"
|
2011-08-07 17:17:05 +00:00
|
|
|
if run_hydra(files)
|
|
|
|
run_all if @did_fail
|
|
|
|
@did_fail = false
|
|
|
|
else
|
|
|
|
@did_fail = true
|
|
|
|
end
|
2011-08-07 16:51:42 +00:00
|
|
|
end
|
2011-08-04 17:35:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def run_all
|
|
|
|
Guard::UI.info "Running Hydra on all matching tests..."
|
2011-08-04 18:29:46 +00:00
|
|
|
run_hydra(ensure_files(matching_tests))
|
2011-08-04 17:35:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def run_hydra(files = [])
|
2011-08-07 16:51:42 +00:00
|
|
|
if !files.empty?
|
|
|
|
File.unlink @options[:runner_log] if runner_log? && @options[:clear_runner_log]
|
2011-08-04 17:35:32 +00:00
|
|
|
|
2011-08-07 16:51:42 +00:00
|
|
|
start = Time.now
|
2011-08-04 17:35:32 +00:00
|
|
|
|
2011-08-07 16:51:42 +00:00
|
|
|
hydra = Hydra::Master.new(
|
|
|
|
:listeners => [ Hydra::Listener::ProgressBar.new ],
|
|
|
|
:files => files,
|
|
|
|
:environment => @options[:env],
|
2011-08-31 15:58:04 +00:00
|
|
|
:config => @options[:hydra_config],
|
|
|
|
:verbose => @options[:verbose]
|
2011-08-07 16:51:42 +00:00
|
|
|
)
|
2011-08-04 17:35:32 +00:00
|
|
|
|
2011-08-07 16:51:42 +00:00
|
|
|
Guard::UI.info sprintf("Tests completed in %.6f seconds", Time.now - start)
|
2011-08-04 17:35:32 +00:00
|
|
|
|
2011-08-07 16:51:42 +00:00
|
|
|
puts File.read(@options[:runner_log]) if runner_log? && @options[:show_runner_log]
|
|
|
|
hydra.failed_files.empty?
|
|
|
|
else
|
|
|
|
Guard::UI.info "No files matched!"
|
|
|
|
false
|
|
|
|
end
|
2011-08-04 17:35:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def runner_log?
|
|
|
|
File.exist?(@options[:runner_log])
|
|
|
|
end
|
|
|
|
|
|
|
|
def rails_application
|
|
|
|
File.expand_path('config/application')
|
|
|
|
end
|
|
|
|
|
|
|
|
def matching_tests
|
2011-08-04 18:29:46 +00:00
|
|
|
Guard::Watcher.match_files(self, match_test_matchers).uniq
|
|
|
|
end
|
|
|
|
|
|
|
|
def match_test_matchers(source = nil)
|
|
|
|
@options[:test_matchers].collect do |match|
|
|
|
|
path = MATCHERS[match]
|
|
|
|
path = File.join(source, path) if source
|
|
|
|
Dir[path]
|
|
|
|
end.flatten
|
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_files(files = [])
|
|
|
|
files.collect do |file|
|
|
|
|
if File.directory?(file)
|
|
|
|
match_test_matchers(file)
|
|
|
|
else
|
|
|
|
file
|
|
|
|
end
|
|
|
|
end.flatten.find_all { |file| File.file?(file) }.uniq
|
2011-08-04 17:35:32 +00:00
|
|
|
end
|
|
|
|
end
|