Test a unit/functional file or test with little effort
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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