| 1 |
require 'autotest' |
|---|
| 2 |
|
|---|
| 3 |
# cloned from Rspec's autotest settings, but modified |
|---|
| 4 |
# to cope w/ Merb's specs directory layout |
|---|
| 5 |
|
|---|
| 6 |
class RspecCommandError < StandardError; end |
|---|
| 7 |
|
|---|
| 8 |
class Autotest::MerbsourceRspec < Autotest |
|---|
| 9 |
def initialize(kernel = Kernel, separator = File::SEPARATOR, alt_separator = File::ALT_SEPARATOR) # :nodoc: |
|---|
| 10 |
super() # need parens so that Ruby doesn't pass our args |
|---|
| 11 |
# to the superclass version which takes none.. |
|---|
| 12 |
|
|---|
| 13 |
@kernel, @separator, @alt_separator = kernel, separator, alt_separator |
|---|
| 14 |
@spec_command = spec_command |
|---|
| 15 |
|
|---|
| 16 |
# watch out: Ruby bug (1.8.6): |
|---|
| 17 |
# %r(/) != /\// |
|---|
| 18 |
# since Ruby compares the REGEXP source, not the resulting pattern |
|---|
| 19 |
@test_mappings = { |
|---|
| 20 |
%r{^spec/.*_spec\.rb$} => kernel.proc { |filename, _| |
|---|
| 21 |
filename |
|---|
| 22 |
}, |
|---|
| 23 |
|
|---|
| 24 |
%r{^lib/merb/(.*)\.rb$} => kernel.proc { |_, m| |
|---|
| 25 |
["spec/#{m[1]}_spec.rb", |
|---|
| 26 |
"spec/merb/#{m[1]}_spec.rb"] |
|---|
| 27 |
}, |
|---|
| 28 |
|
|---|
| 29 |
%r{^lib/merb/mixins/(.*)\.rb$} => kernel.proc { |_, m| |
|---|
| 30 |
["spec/#{m[1]}_spec.rb", |
|---|
| 31 |
"spec/merb/#{m[1]}_spec.rb", |
|---|
| 32 |
"spec/merb/#{m[1]}_mixin_spec.rb"] |
|---|
| 33 |
}, |
|---|
| 34 |
|
|---|
| 35 |
%r{^lib/merb\.rb$} => kernel.proc { |
|---|
| 36 |
files_matching %r{^spec/.*_spec\.rb$} |
|---|
| 37 |
}, |
|---|
| 38 |
|
|---|
| 39 |
%r{^spec/(spec_helper|shared/.*)\.rb$} => kernel.proc { |
|---|
| 40 |
files_matching %r{^spec/.*_spec\.rb$} |
|---|
| 41 |
} |
|---|
| 42 |
} |
|---|
| 43 |
end |
|---|
| 44 |
|
|---|
| 45 |
def tests_for_file(filename) |
|---|
| 46 |
super.select { |f| @files.has_key? f } |
|---|
| 47 |
end |
|---|
| 48 |
|
|---|
| 49 |
alias :specs_for_file :tests_for_file |
|---|
| 50 |
|
|---|
| 51 |
def failed_results(results) |
|---|
| 52 |
results.scan() |
|---|
| 53 |
end |
|---|
| 54 |
|
|---|
| 55 |
def handle_results(results) |
|---|
| 56 |
@files_to_test = consolidate_failures failed_results(results) |
|---|
| 57 |
unless $TESTING |
|---|
| 58 |
if @files_to_test.empty? |
|---|
| 59 |
hook :green |
|---|
| 60 |
else |
|---|
| 61 |
hook :red |
|---|
| 62 |
end |
|---|
| 63 |
end |
|---|
| 64 |
@tainted = true unless @files_to_test.empty? |
|---|
| 65 |
end |
|---|
| 66 |
|
|---|
| 67 |
def consolidate_failures(failed) |
|---|
| 68 |
filters = Hash.new { |h,k| h[k] = [] } |
|---|
| 69 |
failed.each do |spec, failed_trace| |
|---|
| 70 |
@files.keys.select { |f| f =~ }.each do |f| |
|---|
| 71 |
if failed_trace =~ Regexp.new(f) |
|---|
| 72 |
filters[f] << spec |
|---|
| 73 |
break |
|---|
| 74 |
end |
|---|
| 75 |
end |
|---|
| 76 |
end |
|---|
| 77 |
filters |
|---|
| 78 |
end |
|---|
| 79 |
|
|---|
| 80 |
def make_test_cmd(files_to_test) |
|---|
| 81 |
"#{ruby} -S #{@spec_command} #{test_cmd_options} #{files_to_test.keys.flatten.join(' ')}" |
|---|
| 82 |
end |
|---|
| 83 |
|
|---|
| 84 |
def test_cmd_options |
|---|
| 85 |
'-O specs/spec.opts' if File.exist?('specs/spec.opts') |
|---|
| 86 |
end |
|---|
| 87 |
|
|---|
| 88 |
# Finds the proper spec command to use. Precendence |
|---|
| 89 |
# is set in the lazily-evaluated method spec_commands. Alias + Override |
|---|
| 90 |
# that in ~/.autotest to provide a different spec command |
|---|
| 91 |
# then the default paths provided. |
|---|
| 92 |
def spec_command |
|---|
| 93 |
if cmd = spec_commands.detect { |c| File.exist? c } |
|---|
| 94 |
@alt_separator ? (cmd.gsub @separator, @alt_separator) : cmd |
|---|
| 95 |
else |
|---|
| 96 |
raise RspecCommandError, 'No spec command could be found!' |
|---|
| 97 |
end |
|---|
| 98 |
end |
|---|
| 99 |
|
|---|
| 100 |
# Autotest will look for spec commands in the following |
|---|
| 101 |
# locations, in this order: |
|---|
| 102 |
# |
|---|
| 103 |
# * bin/spec |
|---|
| 104 |
# * default spec bin/loader installed in Rubygems |
|---|
| 105 |
def spec_commands |
|---|
| 106 |
[File.join('bin', 'spec'), |
|---|
| 107 |
File.join(Config::CONFIG['bindir'], 'spec')] |
|---|
| 108 |
end |
|---|
| 109 |
end |
|---|