Skip to content

Commit

Permalink
Merge pull request guard#75 from ashmoran/enable_multiple_guard_insta…
Browse files Browse the repository at this point in the history
…nces

Enable multiple guard instances
  • Loading branch information
thibaudgg committed Dec 5, 2011
2 parents 9e3a9c7 + 468d402 commit 5e93457
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 182 deletions.
17 changes: 10 additions & 7 deletions lib/guard/rspec.rb
Expand Up @@ -17,19 +17,22 @@ def initialize(watchers=[], options={})
@last_failed = false
@failed_paths = []

Runner.set_rspec_version(options)
Inspector.excluded = @options[:exclude]
Inspector.spec_paths = @options[:spec_paths]
@runner = Runner.new
@inspector = Inspector.new

@runner.set_rspec_version(options)
@inspector.excluded = @options[:exclude]
@inspector.spec_paths = @options[:spec_paths]
end

# Call once when guard starts
def start
UI.info "Guard::RSpec is running, with RSpec #{Runner.rspec_version}!"
UI.info "Guard::RSpec is running, with RSpec #{@runner.rspec_version}!"
run_all if @options[:all_on_start]
end

def run_all
passed = Runner.run(options[:spec_paths], options.merge(options[:run_all] || {}).merge(:message => "Running all specs"))
passed = @runner.run(options[:spec_paths], options.merge(options[:run_all] || {}).merge(:message => "Running all specs"))

@last_failed = !passed
if passed
Expand All @@ -45,8 +48,8 @@ def reload

def run_on_change(paths)
paths += @failed_paths if @options[:keep_failed]
paths = Inspector.clean(paths)
passed = Runner.run(paths, options)
paths = @inspector.clean(paths)
passed = @runner.run(paths, options)

if passed
# clean failed paths memory
Expand Down
96 changes: 47 additions & 49 deletions lib/guard/rspec/inspector.rb
@@ -1,69 +1,67 @@
module Guard
class RSpec
module Inspector
class << self
def excluded
@excluded || []
end
class Inspector
def excluded
@excluded || []
end

def excluded=(glob)
@excluded = Dir[glob.to_s]
end
def excluded=(glob)
@excluded = Dir[glob.to_s]
end

def spec_paths
@spec_paths || []
end
def spec_paths
@spec_paths || []
end

def spec_paths=(path_array)
@spec_paths = Array(path_array)
end
def spec_paths=(path_array)
@spec_paths = Array(path_array)
end

def clean(paths)
paths.uniq!
paths.compact!
clear_spec_files_list_after do
paths = paths.select { |path| should_run_spec_file?(path) }
end
paths.reject { |p| included_in_other_path?(p, paths) }
def clean(paths)
paths.uniq!
paths.compact!
clear_spec_files_list_after do
paths = paths.select { |path| should_run_spec_file?(path) }
end
paths.reject { |p| included_in_other_path?(p, paths) }
end

private

def should_run_spec_file?(path)
(spec_file?(path) || feature_file?(path) || spec_folder?(path)) && !excluded.include?(path)
end
private

def spec_file?(path)
spec_files.include?(path)
end
def should_run_spec_file?(path)
(spec_file?(path) || feature_file?(path) || spec_folder?(path)) && !excluded.include?(path)
end

def feature_file?(path)
feature_files.include?(path)
end
def spec_file?(path)
spec_files.include?(path)
end

def spec_folder?(path)
path.match(%r{^(#{spec_paths.join("|")})[^\.]*$})
# path.match(%r{^spec[^\.]*$})
end
def feature_file?(path)
feature_files.include?(path)
end

def spec_files
@spec_files ||= spec_paths.collect { |path| Dir[File.join(path, "**", "*_spec.rb")] }.flatten
end
def spec_folder?(path)
path.match(%r{^(#{spec_paths.join("|")})[^\.]*$})
# path.match(%r{^spec[^\.]*$})
end

def feature_files
@feature_files ||= spec_paths.collect { |path| Dir[File.join(path, "**", "*.feature")] }.flatten
end
def spec_files
@spec_files ||= spec_paths.collect { |path| Dir[File.join(path, "**", "*_spec.rb")] }.flatten
end

def clear_spec_files_list_after
yield
@spec_files = nil
end
def feature_files
@feature_files ||= spec_paths.collect { |path| Dir[File.join(path, "**", "*.feature")] }.flatten
end

def included_in_other_path?(path, paths)
(paths - [path]).any? { |p| path.include?(p) && path.sub(p, '').include?('/') }
end
def clear_spec_files_list_after
yield
@spec_files = nil
end

def included_in_other_path?(path, paths)
(paths - [path]).any? { |p| path.include?(p) && path.sub(p, '').include?('/') }
end

end
end
end
161 changes: 79 additions & 82 deletions lib/guard/rspec/runner.rb
@@ -1,108 +1,105 @@
module Guard
class RSpec
module Runner
class << self
attr_reader :rspec_version

def run(paths, options={})
return false if paths.empty?
message = options[:message] || "Running: #{paths.join(' ')}"
UI.info(message, :reset => true)
system(rspec_command(paths, options))

if options[:notification] != false && !drb?(options) && failure_exit_code_supported?(options) && $? && !$?.success? && $?.exitstatus != failure_exit_code
Notifier.notify("Failed", :title => "RSpec results", :image => :failed, :priority => 2)
end
class Runner
attr_reader :rspec_version

$?.success?
end
def run(paths, options={})
return false if paths.empty?
message = options[:message] || "Running: #{paths.join(' ')}"
UI.info(message, :reset => true)
system(rspec_command(paths, options))

def set_rspec_version(options={})
@rspec_version = options[:version] || determine_rspec_version
if options[:notification] != false && !drb?(options) && failure_exit_code_supported?(options) && $? && !$?.success? && $?.exitstatus != failure_exit_code
Notifier.notify("Failed", :title => "RSpec results", :image => :failed, :priority => 2)
end

private
$?.success?
end

def rspec_command(paths, options={})
warn_deprectation(options)
def set_rspec_version(options={})
@rspec_version = options[:version] || determine_rspec_version
end

cmd_parts = []
cmd_parts << "rvm #{options[:rvm].join(',')} exec" if options[:rvm].is_a?(Array)
cmd_parts << "bundle exec" if (bundler? && options[:binstubs] == true && options[:bundler] != false) || (bundler? && options[:bundler] != false)
cmd_parts << rspec_exec(options)
cmd_parts << options[:cli] if options[:cli]
cmd_parts << "-f progress" if options[:cli].nil? || !options[:cli].split(' ').any? { |w| %w[-f --format].include?(w) }
cmd_parts << "-r #{File.dirname(__FILE__)}/formatters/notification_#{rspec_class.downcase}.rb -f Guard::RSpec::Formatter::Notification#{rspec_class}#{rspec_version == 1 ? ":" : " --out "}/dev/null" if options[:notification] != false
cmd_parts << "--failure-exit-code #{failure_exit_code}" if failure_exit_code_supported?(options)
cmd_parts << paths.join(' ')
private

cmd_parts.join(' ')
end
def rspec_command(paths, options={})
warn_deprectation(options)

def drb?(options)
!options[:cli].nil? && options[:cli].include?('--drb')
end
cmd_parts = []
cmd_parts << "rvm #{options[:rvm].join(',')} exec" if options[:rvm].is_a?(Array)
cmd_parts << "bundle exec" if (bundler? && options[:binstubs] == true && options[:bundler] != false) || (bundler? && options[:bundler] != false)
cmd_parts << rspec_exec(options)
cmd_parts << options[:cli] if options[:cli]
cmd_parts << "-f progress" if options[:cli].nil? || !options[:cli].split(' ').any? { |w| %w[-f --format].include?(w) }
cmd_parts << "-r #{File.dirname(__FILE__)}/formatters/notification_#{rspec_class.downcase}.rb -f Guard::RSpec::Formatter::Notification#{rspec_class}#{rspec_version == 1 ? ":" : " --out "}/dev/null" if options[:notification] != false
cmd_parts << "--failure-exit-code #{failure_exit_code}" if failure_exit_code_supported?(options)
cmd_parts << paths.join(' ')

def bundler?
@bundler ||= File.exist?("#{Dir.pwd}/Gemfile")
end
cmd_parts.join(' ')
end

def failure_exit_code_supported?(options={})
return @failure_exit_code_supported if defined?(@failure_exit_code_supported)
@failure_exit_code_supported ||= begin
cmd_parts = []
cmd_parts << "bundle exec" if (bundler? && options[:bundler].is_a?(TrueClass)) || (bundler? && options[:binstubs].is_a?(TrueClass))
( saved = true; options[:binstubs] = false ) if options[:binstubs].is_a?(TrueClass) # failure exit code support is independent of rspec location
cmd_parts << rspec_exec(options)
options[:binstubs] = true if saved
cmd_parts << "--help"
`#{cmd_parts.join(' ')}`.include? "--failure-exit-code"
end
end
def drb?(options)
!options[:cli].nil? && options[:cli].include?('--drb')
end

def failure_exit_code
2
def bundler?
@bundler ||= File.exist?("#{Dir.pwd}/Gemfile")
end

def failure_exit_code_supported?(options={})
return @failure_exit_code_supported if defined?(@failure_exit_code_supported)
@failure_exit_code_supported ||= begin
cmd_parts = []
cmd_parts << "bundle exec" if (bundler? && options[:bundler].is_a?(TrueClass)) || (bundler? && options[:binstubs].is_a?(TrueClass))
( saved = true; options[:binstubs] = false ) if options[:binstubs].is_a?(TrueClass) # failure exit code support is independent of rspec location
cmd_parts << rspec_exec(options)
options[:binstubs] = true if saved
cmd_parts << "--help"
`#{cmd_parts.join(' ')}`.include? "--failure-exit-code"
end
end

def determine_rspec_version
if File.exist?("#{Dir.pwd}/spec/spec_helper.rb")
File.new("#{Dir.pwd}/spec/spec_helper.rb").read.include?("Spec::Runner") ? 1 : 2
elsif bundler?
# Allow RSpactor to be tested with RSpactor (bundle show inside a bundle exec)
ENV['BUNDLE_GEMFILE'] = "#{Dir.pwd}/Gemfile"
`bundle show rspec`.include?("/rspec-1.") ? 1 : 2
else
2
end
def failure_exit_code
2
end

def determine_rspec_version
if File.exist?("#{Dir.pwd}/spec/spec_helper.rb")
File.new("#{Dir.pwd}/spec/spec_helper.rb").read.include?("Spec::Runner") ? 1 : 2
elsif bundler?
# Allow RSpactor to be tested with RSpactor (bundle show inside a bundle exec)
ENV['BUNDLE_GEMFILE'] = "#{Dir.pwd}/Gemfile"
`bundle show rspec`.include?("/rspec-1.") ? 1 : 2
else
2
end
end

def rspec_class
case rspec_version
when 1
"Spec"
when 2
"RSpec"
end
def rspec_class
case rspec_version
when 1
"Spec"
when 2
"RSpec"
end
end

def rspec_exec(options = {})
case rspec_version
when 1
options[:binstubs] == true && options[:bundler] != false ? "bin/spec" : "spec"
when 2
options[:binstubs] == true && options[:bundler] != false ? "bin/rspec" : "rspec"
end
def rspec_exec(options = {})
case rspec_version
when 1
options[:binstubs] == true && options[:bundler] != false ? "bin/spec" : "spec"
when 2
options[:binstubs] == true && options[:bundler] != false ? "bin/rspec" : "rspec"
end
end

def warn_deprectation(options={})
[:color, :drb, :fail_fast, [:formatter, "format"]].each do |option|
key, value = option.is_a?(Array) ? option : [option, option.to_s.gsub('_', '-')]
if options.key?(key)
UI.info %{DEPRECATION WARNING: The :#{key} option is deprecated. Pass standard command line argument "--#{value}" to RSpec with the :cli option.}
end
def warn_deprectation(options={})
[:color, :drb, :fail_fast, [:formatter, "format"]].each do |option|
key, value = option.is_a?(Array) ? option : [option, option.to_s.gsub('_', '-')]
if options.key?(key)
UI.info %{DEPRECATION WARNING: The :#{key} option is deprecated. Pass standard command line argument "--#{value}" to RSpec with the :cli option.}
end
end

end
end
end
Expand Down

0 comments on commit 5e93457

Please sign in to comment.