Skip to content

Instantly share code, notes, and snippets.

@seandenigris
Created February 26, 2010 20:44
Show Gist options
  • Save seandenigris/316139 to your computer and use it in GitHub Desktop.
Save seandenigris/316139 to your computer and use it in GitHub Desktop.
By saving as [project_dir]/.autotest, allows custom (non-"spec") naming conventions, and file names with spaces
require 'autotest/timestamp'
require "autotest/fsevent"
#require "autotest/restart"
class Autotest::Rspec < Autotest
#I'm going to keep using spec for Rails because it got too complicated
unless File.exist?("spec")
puts "Using 'example' for consolidate failures and options file"
# This method tells Autotest which files had failures
# so it knows which hooks to call, and whether to run features.
# The existing method must be removed to avoid a warning
remove_method :consolidate_failures
def consolidate_failures(failed)
filters = new_hash_of_arrays
failed.each do |spec, trace|
if trace =~ /\n(\.\/)?(.*example\.rb):[\d]+:/
filters[$2] << spec
end
end
return filters
end
# This method tells Rspec where the options file is.
# It's necessary because we're using a non-Rspec-standard directory
# The existing method must be removed to avoid a warning
remove_method :add_options_if_present
def add_options_if_present # :nodoc:
File.exist?("examples/spec.opts") ? "-O examples/spec.opts " : ""
end
end
puts "Fixing Autospec for file names including spaces"
remove_method :make_test_cmd
def make_test_cmd(files_to_test)
p files_to_test
files_to_test.empty? ? '' :
"#{ruby} #{SPEC_PROGRAM} --autospec #{fix_spaces(normalize(files_to_test).keys.flatten).join(' ')} #{add_options_if_present}"
end
private
def fix_spaces(file_names)
file_names.map {|arg| arg.include?(' ') ? '"' + arg + '"' : arg}
end
end
Autotest.add_hook(:initialize) {|at|
# Ignore irrelevant files to prevent unnecessary runs
puts "Adding file exceptions..."
%w{.git .svn .hg .swp .DS_Store ._* tmp contract.rb rakefile .txt .vis}.each do |exception|
at.add_exception(exception)
end
unless File.exist?("spec")
puts "Clearing default mappings..."
# take out the rspec default mappings (spec/*_spec.rb)
at.clear_mappings
end
if File.exist?("example")
puts "Adding example mappings..."
# Mapping for all project class files
at.add_mapping(%r%^(examples|third_party)/.*_example.rb$%) { |filename, _|
filename
}
# Mapping for Cucumber helper examples
at.add_mapping(%r%^features/support/(.*)\.rb$%) { |_, m|
["examples/cucumber/#{m[1]}_example.rb"]
}
# Mapping for library class examples
at.add_mapping(%r%^lib/(.*)\.rb$%) { |_, m|
["examples/#{m[1]}_example.rb"]
}
# Mapping for shared example helper file
at.add_mapping(%r%^examples/(example_helper|shared/.*)\.rb$%) {
at.files_matching %r%^(examples|third_party)/.*_example\.rb$%
}
end
# Returning nil makes sure that any other initialize hooks get called
# Per http://wiki.github.com/dchelimsky/rspec/autotest-integration
nil
}
# These are tracing templates for all the available Autotest hooks.
# Uncomment them to see when they are called in the output.%
#Autotest.add_hook(:all_good) {|at|
#}
#Autotest.add_hook(:died) {|at|
#puts "died"
#nil
#}
#Autotest.add_hook(:green) {|at|
#puts "green"
#nil
#}
#Autotest.add_hook(:interrupt) {|at|
#puts "interrupt"
#nil
#}
#Autotest.add_hook(:quit) {|at|
#puts "quit"
#nil
#}
#Autotest.add_hook(:ran_command) {|at|
#puts "ran_command"
#nil
#}
#Autotest.add_hook(:red) {|at|
#puts "red"
#nil
#}
#Autotest.add_hook(:reset) {|at|
#puts "reset"
#nil
#}
#Autotest.add_hook(:run_command) {|at|
#puts "run_command"
#nil
#}
#Autotest.add_hook(:updated) {|at, updated|
#puts "updated: "
#p updated
#nil
#}
#Autotest.add_hook(:waiting) {|at|
#puts "waiting"
#nil
#}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment