3 class RspecCommandError < StandardError; end
5 class Autotest::Rspec < Autotest
7 def initialize(kernel=Kernel, separator=File::SEPARATOR, alt_separator=File::ALT_SEPARATOR) # :nodoc:
9 @kernel, @separator, @alt_separator = kernel, separator, alt_separator
10 @spec_command = spec_command
12 # watch out: Ruby bug (1.8.6):
14 # since Ruby compares the REGEXP source, not the resulting pattern
16 %r%^spec/.*\.rb$% => kernel.proc { |filename, _|
19 %r%^lib/(.*)\.rb$% => kernel.proc { |_, m|
20 ["spec/#{m[1]}_spec.rb"]
22 %r%^spec/(spec_helper|shared/.*)\.rb$% => kernel.proc {
23 files_matching %r%^spec/.*_spec\.rb$%
28 def tests_for_file(filename)
29 super.select { |f| @files.has_key? f }
32 alias :specs_for_file :tests_for_file
34 def failed_results(results)
35 results.scan(/^\d+\)\n(?:\e\[\d*m)?(?:.*?Error in )?'([^\n]*)'(?: FAILED)?(?:\e\[\d*m)?\n(.*?)\n\n/m)
38 def handle_results(results)
39 @files_to_test = consolidate_failures failed_results(results)
40 unless @files_to_test.empty? then
45 @tainted = true unless @files_to_test.empty?
48 def consolidate_failures(failed)
49 filters = Hash.new { |h,k| h[k] = [] }
50 failed.each do |spec, failed_trace|
51 @files.keys.select{|f| f =~ /spec\//}.each do |f|
52 if failed_trace =~ Regexp.new(f)
61 def make_test_cmd(files_to_test)
62 return "#{ruby} -S #{@spec_command} #{add_options_if_present} #{files_to_test.keys.flatten.join(' ')}"
65 def add_options_if_present
66 File.exist?("spec/spec.opts") ? "-O spec/spec.opts " : ""
69 # Finds the proper spec command to use. Precendence
70 # is set in the lazily-evaluated method spec_commands. Alias + Override
71 # that in ~/.autotest to provide a different spec command
72 # then the default paths provided.
74 spec_commands.each do |command|
75 if File.exists?(command)
76 return @alt_separator ? (command.gsub @separator, @alt_separator) : command
79 raise RspecCommandError, "No spec command could be found!"
82 # Autotest will look for spec commands in the following
83 # locations, in this order:
86 # * default spec bin/loader installed in Rubygems
89 File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'bin', 'spec')),
90 File.join(Config::CONFIG['bindir'], 'spec')