Skip to content

Instantly share code, notes, and snippets.

@qrush
Forked from jferris/gist:73948
Created April 24, 2009 14:39
Show Gist options
  • Save qrush/101130 to your computer and use it in GitHub Desktop.
Save qrush/101130 to your computer and use it in GitHub Desktop.
tu/tf autocompletion for zsh
# autocompletion for ruby_test
# # works with tu/tf aliases
# # see also ~/bin/ruby_test.rb
_ruby_tests() {
if [[ -n $words[2] ]]; then
compadd `ruby_test -l ${words[2]}`
fi
}
compdef _ruby_tests ruby_test
alias tu="ruby_test unit"
alias tf="ruby_test functional"
alias ti="ruby_test integration"
#!/usr/bin/env ruby
require 'fileutils'
class TestFinder
attr_reader :prefix
def initialize(prefix)
@prefix = prefix
end
def name_to_file(name)
"#{prefix}/#{name}_test.rb"
end
def file_to_name(file)
file.sub(%r{^#{Regexp.escape(prefix)}/}, '').sub(%r{_test\.rb$}, '')
end
def test_files
@test_files ||= Dir["#{prefix}/**/*_test.rb"]
end
def test_names
@test_names ||= test_files.collect {|file| file_to_name(file) }
end
def run(tests)
if tests.empty?
if test_files.empty?
puts "Nothing to do."
else
run(test_files)
end
else
tests = tests.collect {|test| force_test_file(test) }
command = command_to_run(tests)
puts command
exec command
end
end
def list
puts test_names.sort.join("\n")
end
private
def force_test_file(test)
if test_files.include?(test)
test
elsif test_names.include?(test)
name_to_file(test)
else
raise "No such test: #{test.inspect}"
end
end
def command_to_run(files)
files_string = files.inspect
require_string = 'require "#{f}"'
%{ruby -Itest -e '#{files_string}.each {|f| #{require_string} }'}
end
end
list = ARGV.delete('-l') || ARGV.delete('--list')
prefix = ARGV.shift
prefix = prefix ? "test/#{prefix}" : 'test'
finder = TestFinder.new(prefix)
tests = ARGV.dup
if list
finder.list
else
finder.run(tests)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment