Skip to content

Instantly share code, notes, and snippets.

@coffeeaddict
Created December 14, 2010 14:54
Show Gist options
  • Save coffeeaddict/740527 to your computer and use it in GitHub Desktop.
Save coffeeaddict/740527 to your computer and use it in GitHub Desktop.
Test a unit/functional file or test with little effort
#!/usr/bin/env ruby
#
# = USAGE
# rails_test (f|functional|u|unit|p|performance|i|integration) test_case_name test_name
#
# = SYNOPSIS
#
# rails_test f customers
# # will run functional/customers_controller.rb
#
# rails_test f customers create_customer
# # will run functional/customers_controller.rb -n test_should_create_customer
#
# rails test f customers show
# > test_should_get_index
# > test_should_show_customer
# > ...
# # eg; displays the test methods of the test
#
require 'fileutils'
require 'shellwords'
include FileUtils
type = ARGV.shift
file = ARGV.shift
test = ARGV.shift
if type.length == 1
type = case type
when "f"; "functional"
when "u"; "unit"
when "i"; "integration"
when "p"; "performace"
end
end
file.gsub!(/\.rb$/, "")
if type == "unit"
if file !~ /_test$/
file += "_test"
end
elsif type == "functional"
if file !~ /_controller/
file += "_controller"
end
if file !~ /_test$/
file += "_test"
end
end
file += ".rb"
if pwd !~ /\/test$/
if File.exists? "test"
cd "test"
else
$stderr.puts "Are you in the Rails.root?"
exit 1
end
end
test_file = File.join(type, file)
if !File.exists? test_file
puts "No such file: #{test_file}"
exit 1
end
if test == "show"
# list the available tests and leave
contents = File.read(test_file)
tests = contents.scan /test [\'\"][^\'\"]+[\'\"] do/
puts tests.collect { |t|
t.gsub('"', '').gsub(' do', '').gsub(' ', '_')
}.join "\n"
exit
elsif test
test = "should_" + test if test !~ /should_/
test = "test_" + test if test !~ /test_/
end
puts "Testing #{test ? test : :all} from #{type}/#{file}"
command = [ "/opt/ruby-enterprise/bin/ruby", "-I../", test_file ]
command += [ "-n", test ] if test
exec command.shelljoin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment