| 1 |
# Define a task library for running unit tests. |
| 2 |
|
| 3 |
require 'rake' |
| 4 |
require 'rake/tasklib' |
| 5 |
|
| 6 |
module Rake |
| 7 |
|
| 8 |
# Create a task that runs a set of tests. |
| 9 |
# |
| 10 |
# Example: |
| 11 |
# |
| 12 |
# Rake::TestTask.new do |t| |
| 13 |
# t.libs << "test" |
| 14 |
# t.test_files = FileList['test/test*.rb'] |
| 15 |
# t.verbose = true |
| 16 |
# end |
| 17 |
# |
| 18 |
# If rake is invoked with a "TEST=filename" command line option, |
| 19 |
# then the list of test files will be overridden to include only the |
| 20 |
# filename specified on the command line. This provides an easy way |
| 21 |
# to run just one test. |
| 22 |
# |
| 23 |
# If rake is invoked with a "TESTOPTS=options" command line option, |
| 24 |
# then the given options are passed to the test process after a |
| 25 |
# '--'. This allows Test::Unit options to be passed to the test |
| 26 |
# suite. |
| 27 |
# |
| 28 |
# Examples: |
| 29 |
# |
| 30 |
# rake test # run tests normally |
| 31 |
# rake test TEST=just_one_file.rb # run just one test file. |
| 32 |
# rake test TESTOPTS="-v" # run in verbose mode |
| 33 |
# rake test TESTOPTS="--runner=fox" # use the fox test runner |
| 34 |
# |
| 35 |
class TestTask < TaskLib |
| 36 |
|
| 37 |
# Name of test task. (default is :test) |
| 38 |
attr_accessor :name |
| 39 |
|
| 40 |
# List of directories to added to $LOAD_PATH before running the |
| 41 |
# tests. (default is 'lib') |
| 42 |
attr_accessor :libs |
| 43 |
|
| 44 |
# True if verbose test output desired. (default is false) |
| 45 |
attr_accessor :verbose |
| 46 |
|
| 47 |
# Test options passed to the test suite. An explicit |
| 48 |
# TESTOPTS=opts on the command line will override this. (default |
| 49 |
# is NONE) |
| 50 |
attr_accessor :options |
| 51 |
|
| 52 |
# Request that the tests be run with the warning flag set. |
| 53 |
# E.g. warning=true implies "ruby -w" used to run the tests. |
| 54 |
attr_accessor :warning |
| 55 |
|
| 56 |
# Glob pattern to match test files. (default is 'test/test*.rb') |
| 57 |
attr_accessor :pattern |
| 58 |
|
| 59 |
# Style of test loader to use. Options are: |
| 60 |
# |
| 61 |
# * :rake -- Rake provided test loading script (default). |
| 62 |
# * :testrb -- Ruby provided test loading script. |
| 63 |
# * :direct -- Load tests using command line loader. |
| 64 |
# |
| 65 |
attr_accessor :loader |
| 66 |
|
| 67 |
# Array of commandline options to pass to ruby when running test loader. |
| 68 |
attr_accessor :ruby_opts |
| 69 |
|
| 70 |
# Explicitly define the list of test files to be included in a |
| 71 |
# test. +list+ is expected to be an array of file names (a |
| 72 |
# FileList is acceptable). If both +pattern+ and +test_files+ are |
| 73 |
# used, then the list of test files is the union of the two. |
| 74 |
def test_files=(list) |
| 75 |
@test_files = list |
| 76 |
end |
| 77 |
|
| 78 |
# Create a testing task. |
| 79 |
def initialize(name=:test) |
| 80 |
@name = name |
| 81 |
@libs = ["lib"] |
| 82 |
@pattern = nil |
| 83 |
@options = nil |
| 84 |
@test_files = nil |
| 85 |
@verbose = false |
| 86 |
@warning = false |
| 87 |
@loader = :rake |
| 88 |
@ruby_opts = [] |
| 89 |
yield self if block_given? |
| 90 |
@pattern = 'test/test*.rb' if @pattern.nil? && @test_files.nil? |
| 91 |
define |
| 92 |
end |
| 93 |
|
| 94 |
# Create the tasks defined by this task lib. |
| 95 |
def define |
| 96 |
desc "Run tests" + (@name==:test ? "" : " for #{@name}") |
| 97 |
task @name do |
| 98 |
FileUtilsExt.verbose(@verbose) do |
| 99 |
ruby "#{ruby_opts_string} #{run_code} #{file_list_string} #{option_list}" |
| 100 |
end |
| 101 |
end |
| 102 |
self |
| 103 |
end |
| 104 |
|
| 105 |
def option_list # :nodoc: |
| 106 |
(ENV['TESTOPTS'] || |
| 107 |
ENV['TESTOPT'] || |
| 108 |
ENV['TEST_OPTS'] || |
| 109 |
ENV['TEST_OPT'] || |
| 110 |
@options || |
| 111 |
"") |
| 112 |
end |
| 113 |
|
| 114 |
def ruby_opts_string |
| 115 |
opts = @ruby_opts.dup |
| 116 |
opts.unshift( "-I\"#{lib_path}\"" ) unless @libs.empty? |
| 117 |
opts.unshift( "-w" ) if @warning |
| 118 |
opts.join(" ") |
| 119 |
end |
| 120 |
|
| 121 |
def lib_path |
| 122 |
@libs.join(File::PATH_SEPARATOR) |
| 123 |
end |
| 124 |
|
| 125 |
def file_list_string |
| 126 |
file_list.collect { |fn| "\"#{fn}\"" }.join(' ') |
| 127 |
end |
| 128 |
|
| 129 |
def file_list # :nodoc: |
| 130 |
if ENV['TEST'] |
| 131 |
FileList[ ENV['TEST'] ] |
| 132 |
else |
| 133 |
result = [] |
| 134 |
result += @test_files.to_a if @test_files |
| 135 |
result << @pattern if @pattern |
| 136 |
result |
| 137 |
end |
| 138 |
end |
| 139 |
|
| 140 |
def fix # :nodoc: |
| 141 |
case ruby_version |
| 142 |
when '1.8.2' |
| 143 |
"\"#{find_file 'rake/ruby182_test_unit_fix'}\"" |
| 144 |
else |
| 145 |
nil |
| 146 |
end || '' |
| 147 |
end |
| 148 |
|
| 149 |
def ruby_version |
| 150 |
RUBY_VERSION |
| 151 |
end |
| 152 |
|
| 153 |
def run_code |
| 154 |
case @loader |
| 155 |
when :direct |
| 156 |
"-e \"ARGV.each{|f| require f}\"" |
| 157 |
when :testrb |
| 158 |
"-S testrb #{fix}" |
| 159 |
when :rake |
| 160 |
"-I\"#{rake_lib_dir}\" \"#{rake_loader}\"" |
| 161 |
end |
| 162 |
end |
| 163 |
|
| 164 |
def rake_loader # :nodoc: |
| 165 |
find_file('rake/rake_test_loader') or |
| 166 |
fail "unable to find rake test loader" |
| 167 |
end |
| 168 |
|
| 169 |
def find_file(fn) # :nodoc: |
| 170 |
$LOAD_PATH.each do |path| |
| 171 |
file_path = File.join(path, "#{fn}.rb") |
| 172 |
return file_path if File.exist? file_path |
| 173 |
end |
| 174 |
nil |
| 175 |
end |
| 176 |
|
| 177 |
def rake_lib_dir # :nodoc: |
| 178 |
find_dir('rake') or |
| 179 |
fail "unable to find rake lib" |
| 180 |
end |
| 181 |
|
| 182 |
def find_dir(fn) # :nodoc: |
| 183 |
$LOAD_PATH.each do |path| |
| 184 |
file_path = File.join(path, "#{fn}.rb") |
| 185 |
return path if File.exist? file_path |
| 186 |
end |
| 187 |
nil |
| 188 |
end |
| 189 |
|
| 190 |
end |
| 191 |
end |