guard-hydra/lib/guard/hydra.rb

115 lines
2.6 KiB
Ruby
Raw Normal View History

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
@did_fail = false
2011-08-04 17:35:32 +00:00
run_all if @options[:all_on_start]
end
def run_on_change(files = [])
if !(files = ensure_files(files)).empty?
Guard::UI.info "Running Hydra on #{files.join(', ')}"
if run_hydra(files)
run_all if @did_fail
@did_fail = false
else
@did_fail = true
end
end
2011-08-04 17:35:32 +00:00
end
def run_all
Guard::UI.info "Running Hydra on all matching tests..."
run_hydra(ensure_files(matching_tests))
2011-08-04 17:35:32 +00:00
end
private
def run_hydra(files = [])
if !files.empty?
File.unlink @options[:runner_log] if runner_log? && @options[:clear_runner_log]
2011-08-04 17:35:32 +00:00
start = Time.now
2011-08-04 17:35:32 +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-04 17:35:32 +00:00
Guard::UI.info sprintf("Tests completed in %.6f seconds", Time.now - start)
2011-08-04 17:35:32 +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
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