]> git.openstreetmap.org Git - rails.git/blob - vendor/gems/rspec-1.1.2/lib/autotest/rspec.rb
added RSpec and RSpec on Rails
[rails.git] / vendor / gems / rspec-1.1.2 / lib / autotest / rspec.rb
1 require 'autotest'
2
3 Autotest.add_hook :initialize do |at|
4   at.clear_mappings
5   # watch out: Ruby bug (1.8.6):
6   # %r(/) != /\//
7   at.add_mapping(%r%^spec/.*\.rb$%) { |filename, _| 
8     filename 
9   }
10   at.add_mapping(%r%^lib/(.*)\.rb$%) { |_, m| 
11     ["spec/#{m[1]}_spec.rb"]
12   }
13   at.add_mapping(%r%^spec/(spec_helper|shared/.*)\.rb$%) { 
14     at.files_matching %r%^spec/.*_spec\.rb$%
15   }
16 end
17
18 class RspecCommandError < StandardError; end
19
20 class Autotest::Rspec < Autotest
21   
22   def tests_for_file(filename)
23     super.select { |f| @files.has_key? f }
24   end
25   
26   alias :specs_for_file :tests_for_file
27   
28   def failed_results(results)
29     results.scan(/^\d+\)\n(?:\e\[\d*m)?(?:.*?Error in )?'([^\n]*)'(?: FAILED)?(?:\e\[\d*m)?\n(.*?)\n\n/m)
30   end
31
32   def handle_results(results)
33     @files_to_test = consolidate_failures failed_results(results)
34     unless @files_to_test.empty? then
35       hook :red
36     else
37       hook :green
38     end unless $TESTING
39     @tainted = true unless @files_to_test.empty?
40   end
41
42   def consolidate_failures(failed)
43     filters = Hash.new { |h,k| h[k] = [] }
44     failed.each do |spec, failed_trace|
45       @files.keys.select{|f| f =~ /spec\//}.each do |f|
46         if failed_trace =~ Regexp.new(f)
47           filters[f] << spec
48           break
49         end
50       end
51     end
52     return filters
53   end
54
55   def make_test_cmd(files_to_test)
56     return "#{ruby} -S #{spec_command} #{add_options_if_present} #{files_to_test.keys.flatten.join(' ')}"
57   end
58   
59   def add_options_if_present
60     File.exist?("spec/spec.opts") ? "-O spec/spec.opts " : ""
61   end
62
63   # Finds the proper spec command to use.  Precendence
64   # is set in the lazily-evaluated method spec_commands.  Alias + Override
65   # that in ~/.autotest to provide a different spec command
66   # then the default paths provided.
67   def spec_command
68     @spec_command ||= spec_commands.each do |command|
69       if File.exists?(command)
70         return @alt_separator ? (command.gsub @separator, @alt_separator) : command
71       end
72     end
73     raise RspecCommandError, "No spec command could be found!"
74   end
75   
76   # Autotest will look for spec commands in the following
77   # locations, in this order:
78   #
79   #   * bin/spec
80   #   * default spec bin/loader installed in Rubygems
81   def spec_commands
82     [
83       File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'bin', 'spec')),
84       File.join(Config::CONFIG['bindir'], 'spec')
85     ]
86   end
87
88 end