Upgraded Rails and RSpec
[monkeycharger.git] / vendor / plugins / rspec / rspec / lib / autotest / rspec.rb
blobdb774f24e4ad46fd853ea76e6fc430731cf59331
1 require 'autotest'
3 class RspecCommandError < StandardError; end
5 class Autotest::Rspec < Autotest
6   
7   def initialize(kernel=Kernel, separator=File::SEPARATOR, alt_separator=File::ALT_SEPARATOR) # :nodoc:
8     super()
9     @kernel, @separator, @alt_separator = kernel, separator, alt_separator
10     @spec_command = spec_command
12     # watch out: Ruby bug (1.8.6):
13     # %r(/) != /\//
14     # since Ruby compares the REGEXP source, not the resulting pattern
15     @test_mappings = {
16       %r%^spec/.*\.rb$% => kernel.proc { |filename, _| 
17         filename 
18       },
19       %r%^lib/(.*)\.rb$% => kernel.proc { |_, m| 
20         ["spec/#{m[1]}_spec.rb"] 
21       },
22       %r%^spec/(spec_helper|shared/.*)\.rb$% => kernel.proc { 
23         files_matching %r%^spec/.*_spec\.rb$% 
24       }
25     }
26   end
27   
28   def tests_for_file(filename)
29     super.select { |f| @files.has_key? f }
30   end
31   
32   alias :specs_for_file :tests_for_file
33   
34   def failed_results(results)
35     results.scan(/^\d+\)\n(?:\e\[\d*m)?(?:.*?Error in )?'([^\n]*)'(?: FAILED)?(?:\e\[\d*m)?\n(.*?)\n\n/m)
36   end
38   def handle_results(results)
39     @files_to_test = consolidate_failures failed_results(results)
40     unless @files_to_test.empty? then
41       hook :red
42     else
43       hook :green
44     end unless $TESTING
45     @tainted = true unless @files_to_test.empty?
46   end
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)
53           filters[f] << spec
54           break
55         end
56       end
57     end
58     return filters
59   end
61   def make_test_cmd(files_to_test)
62     return "#{ruby} -S #{@spec_command} #{add_options_if_present} #{files_to_test.keys.flatten.join(' ')}"
63   end
64   
65   def add_options_if_present
66     File.exist?("spec/spec.opts") ? "-O spec/spec.opts " : ""
67   end
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.
73   def spec_command
74     spec_commands.each do |command|
75       if File.exists?(command)
76         return @alt_separator ? (command.gsub @separator, @alt_separator) : command
77       end
78     end
79     raise RspecCommandError, "No spec command could be found!"
80   end
81   
82   # Autotest will look for spec commands in the following
83   # locations, in this order:
84   #
85   #   * bin/spec
86   #   * default spec bin/loader installed in Rubygems
87   def spec_commands
88     [
89       File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'bin', 'spec')),
90       File.join(Config::CONFIG['bindir'], 'spec')
91     ]
92   end
94 end