testtask.rb 4.9 KB

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