Skip to content

Instantly share code, notes, and snippets.

@daveyeu
Created March 30, 2010 05:00
Show Gist options
  • Save daveyeu/348777 to your computer and use it in GitHub Desktop.
Save daveyeu/348777 to your computer and use it in GitHub Desktop.
require "highline"
# Allows Autotest scope to be set on the fly
#
# This owes a lot to the original autotest/menu.rb and David Chelimsky's post
# on scoping Autotest:
# http://blog.davidchelimsky.net/articles/2008/03/05/limiting-scope-of-autotest
#
# ^C will now bring up a menu, with some self-explanatory choices and a
# "Re-scope" option. Choosing that prompts for a new scope. Some magic words
# will act as expected:
#
# models controllers views lib all
#
# In addition, a space separated list of directories and/or files is acceptable.
# Note that Autotest needs to know about both test and implementation files, so
# for example if you have presenters under app/presenters:
#
# Scope? app/presenters spec/presenters # Works
# Scope? app/presenters # Doesn't work
#
# Model, controller and lib files are magically matched so:
#
# Scope? app/models/user.rb # Automatically adds "spec/models/user_spec.rb"
#
# Currently, only RSpec is supported for the niceties.
#
module Autotest::ScopeMenu
def self.set_scope(args, autotest)
directories = []
extra_files = []
[ args ].flatten.each do |arg|
if arg =~ /\.\w+$/
case arg
when %r{(app|spec)/(models|controllers)/(.+?)(_spec)?\.rb}
extra_files.push "spec/#{$2}/#{$3}_spec.rb", "app/#{$2}/#{$3}.rb"
when %r{(spec/)?lib/(.+?)(_spec)?\.rb}
extra_files.push "lib/#{$2}.rb", "spec/lib/#{$2}_spec.rb"
else
extra_files.push arg
end
else
case arg
when "models", "controllers", "views"
directories.push "spec/#{arg}", "app/#{arg}"
when "lib"
directories.push "spec/lib", "lib"
when "all"
directories.push "."
else
directories.push arg
end
end
end
directories = ["."] if directories.empty? and extra_files.empty?
autotest.find_directories = directories
autotest.extra_files = extra_files
end
Autotest.add_hook(:initialize) do |at|
set_scope(ARGV, at) unless ARGV.empty?
false
end
Autotest.add_hook(:interrupt) do |at|
HighLine.new.choose do |menu|
menu.prompt = "> "
menu.choice("Quit") { at.wants_to_quit = true }
menu.choice("Continue") { }
menu.choice("Restart") { at.reset }
menu.choice("Re-scope") do
set_scope(HighLine.new.ask("New scope? ").split, at)
end
end
true
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment