123456789_123456789_123456789_123456789_123456789_

Class: Rake::TestTask

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, TaskLib
Instance Chain:
Inherits: Rake::TaskLib
Defined in: lib/rake/testtask.rb

Overview

Create a task that runs a set of tests.

Example:

require "rake/testtask"

Rake::TestTask.new do |t|
  t.libs << "test"
  t.test_files = FileList['test/test*.rb']
  t.verbose = true
end

If rake is invoked with a “TEST=filename” command line option, then the list of test files will be overridden to include only the filename specified on the command line. This provides an easy way to run just one test.

If rake is invoked with a “TESTOPTS=options” command line option, then the given options are passed to the test process after a ‘–’. This allows Test::Unit options to be passed to the test suite.

Examples:

rake test                           # run tests normally
rake test TEST=just_one_file.rb     # run just one test file.
rake test TESTOPTS="-v"             # run in verbose mode
rake test TESTOPTS="--runner=fox"   # use the fox test runner

Constant Summary

::FileUtils - Included

LN_SUPPORTED, RUBY

FileUtilsExt - Included

DEFAULT

Class Method Summary

Instance Attribute Summary

  • #deps rw

    Task prerequisites.

  • #description rw

    Description of the test task.

  • #libs rw

    List of directories added to $LOAD_PATH before running the tests.

  • #loader rw

    Style of test loader to use.

  • #name rw

    Name of test task.

  • #options rw

    Test options passed to the test suite.

  • #pattern rw

    Glob pattern to match test files.

  • #ruby_opts rw

    Array of command line options to pass to ruby when running test loader.

  • #test_files=(list) writeonly

    Explicitly define the list of test files to be included in a test.

  • #verbose rw

    True if verbose test output desired.

  • #warning rw

    Request that the tests be run with the warning flag set.

Instance Method Summary

DSL - Included

#desc

Describes the next rake task.

#directory

Declare a set of files tasks to create the given directories on demand.

#file

Declare a file task.

#file_create

Declare a file creation task.

#import

Import the partial Rakefiles fn.

#multitask

Declare a task that performs its prerequisites in parallel.

#namespace

Create a new rake namespace and use it for evaluating the given block.

#rule

Declare a rule for auto-tasks.

#task

Declare a basic task.

FileUtilsExt - Included

#nowrite

Get/set the nowrite flag controlling output from the ::FileUtils utilities.

#rake_check_options

Check that the options do not contain options not listed in optdecl.

#rake_merge_option

Merge the given options with the default values.

#rake_output_message

Send the message to the default rake output (which is $stderr).

#verbose

Get/set the verbose flag controlling output from the ::FileUtils utilities.

#when_writing

Use this function to prevent potentially destructive ruby code from running when the :nowrite flag is set.

::FileUtils - Included

#ruby

Run a Ruby interpreter with the given arguments.

#safe_ln

Attempt to do a normal file link, but fall back to a copy if the link fails.

#sh

Run the system command cmd.

#split_all

Split a file path into individual directory names.

#create_shell_runner, #set_verbose_option, #sh_show_command

Cloneable - Included

#initialize_copy

The hook that is invoked by ‘clone’ and ‘dup’ methods.

Constructor Details

.new(name = :test) {|_self| ... } ⇒ TestTask

Create a testing task.

Yields:

  • (_self)

Yield Parameters:

  • _self (TestTask)

    the object that the method was called on

[ GitHub ]

  
# File 'lib/rake/testtask.rb', line 86

def initialize(name=:test)
  @name = name
  @libs = ["lib"]
  @pattern = nil
  @options = nil
  @test_files = nil
  @verbose = false
  @warning = true
  @loader = :rake
  @ruby_opts = []
  @description = "Run tests" + (@name == :test ? "" : " for #{@name}")
  @deps = []
  if @name.is_a?(Hash)
    @deps = @name.values.first
    @name = @name.keys.first
  end
  yield self if block_given?
  @pattern = "test/test*.rb" if @pattern.nil? && @test_files.nil?
  define
end

Instance Attribute Details

#deps (rw)

Task prerequisites.

[ GitHub ]

  
# File 'lib/rake/testtask.rb', line 75

attr_accessor :deps

#description (rw)

Description of the test task. (default is ‘Run tests’)

[ GitHub ]

  
# File 'lib/rake/testtask.rb', line 72

attr_accessor :description

#libs (rw)

List of directories added to $LOAD_PATH before running the tests. (default is ‘lib’)

[ GitHub ]

  
# File 'lib/rake/testtask.rb', line 42

attr_accessor :libs

#loader (rw)

Style of test loader to use. Options are:

  • :rake::Rake provided test loading script (default).

  • :testrb – Ruby provided test loading script.

  • :direct – Load tests using command line loader.

[ GitHub ]

  
# File 'lib/rake/testtask.rb', line 66

attr_accessor :loader

#name (rw)

Name of test task. (default is :test)

[ GitHub ]

  
# File 'lib/rake/testtask.rb', line 38

attr_accessor :name

#options (rw)

Test options passed to the test suite. An explicit TESTOPTS=opts on the command line will override this. (default is NONE)

[ GitHub ]

  
# File 'lib/rake/testtask.rb', line 50

attr_accessor :options

#pattern (rw)

Glob pattern to match test files. (default is ‘test/test*.rb’)

[ GitHub ]

  
# File 'lib/rake/testtask.rb', line 58

attr_accessor :pattern

#ruby_opts (rw)

Array of command line options to pass to ruby when running test loader.

[ GitHub ]

  
# File 'lib/rake/testtask.rb', line 69

attr_accessor :ruby_opts

#test_files=(list) (writeonly)

Explicitly define the list of test files to be included in a test. list is expected to be an array of file names (a FileList is acceptable). If both #pattern and test_files are used, then the list of test files is the union of the two.

[ GitHub ]

  
# File 'lib/rake/testtask.rb', line 81

def test_files=(list)
  @test_files = list
end

#verbose (rw)

True if verbose test output desired. (default is false)

[ GitHub ]

  
# File 'lib/rake/testtask.rb', line 45

attr_accessor :verbose

#warning (rw)

Request that the tests be run with the warning flag set. E.g. warning=true implies “ruby -w” used to run the tests. (default is true)

[ GitHub ]

  
# File 'lib/rake/testtask.rb', line 55

attr_accessor :warning

Instance Method Details

#define

Create the tasks defined by this task lib.

[ GitHub ]

  
# File 'lib/rake/testtask.rb', line 108

def define
  desc @description
  task @name => Array(deps) do
    FileUtilsExt.verbose(@verbose) do
      puts "Use TESTOPTS=\"--verbose\" to pass --verbose" \
        ", etc. to runners." if ARGV.include? "--verbose"
      args =
        "#{ruby_opts_string} #{run_code} " +
        "#{file_list_string} #{option_list}"
      ruby args do |ok, status|
        if !ok && status.respond_to?(:signaled?) && status.signaled?
          raise SignalException.new(status.termsig)
        elsif !ok
          status  = "Command failed with status (#{status.exitstatus})"
          details = ": [ruby #{args}]"
          message =
            if Rake.application.options.trace or @verbose
              status + details
            else
              status
            end

          fail message
        end
      end
    end
  end
  self
end

#file_list

This method is for internal use only.
[ GitHub ]

  
# File 'lib/rake/testtask.rb', line 162

def file_list # :nodoc:
  if ENV["TEST"]
    FileList[ENV["TEST"]]
  else
    result = []
    result += @test_files.to_a if @test_files
    result += FileList[@pattern].to_a if @pattern
    result
  end
end

#file_list_string

This method is for internal use only.
[ GitHub ]

  
# File 'lib/rake/testtask.rb', line 158

def file_list_string # :nodoc:
  file_list.map { |fn| "\"#{fn}\"" }.join(" ")
end

#find_dir(fn)

This method is for internal use only.
[ GitHub ]

  
# File 'lib/rake/testtask.rb', line 215

def find_dir(fn) # :nodoc:
  $LOAD_PATH.each do |path|
    file_path = File.join(path, "#{fn}.rb")
    return path if File.exist? file_path
  end
  nil
end

#find_file(fn)

This method is for internal use only.
[ GitHub ]

  
# File 'lib/rake/testtask.rb', line 193

def find_file(fn) # :nodoc:
  $LOAD_PATH.each do |path|
    file_path = File.join(path, "#{fn}.rb")
    return file_path if File.exist? file_path
  end
  nil
end

#lib_path

This method is for internal use only.
[ GitHub ]

  
# File 'lib/rake/testtask.rb', line 154

def lib_path # :nodoc:
  @libs.join(File::PATH_SEPARATOR)
end

#option_list

This method is for internal use only.
[ GitHub ]

  
# File 'lib/rake/testtask.rb', line 138

def option_list # :nodoc:
  (ENV["TESTOPTS"] ||
    ENV["TESTOPT"] ||
    ENV["TEST_OPTS"] ||
    ENV["TEST_OPT"] ||
    @options ||
    "")
end

#rake_include_arg

This method is for internal use only.
[ GitHub ]

  
# File 'lib/rake/testtask.rb', line 201

def rake_include_arg # :nodoc:
  spec = Gem.loaded_specs["rake"]
  if spec.respond_to?(:default_gem?) && spec.default_gem?
    ""
  else
    "-I\"#{rake_lib_dir}\""
  end
end

#rake_lib_dir

This method is for internal use only.
[ GitHub ]

  
# File 'lib/rake/testtask.rb', line 210

def rake_lib_dir # :nodoc:
  find_dir("rake") or
    fail "unable to find rake lib"
end

#rake_loader

This method is for internal use only.
[ GitHub ]

  
# File 'lib/rake/testtask.rb', line 188

def rake_loader # :nodoc:
  find_file("rake/rake_test_loader") or
    fail "unable to find rake test loader"
end

#ruby_opts_string

This method is for internal use only.
[ GitHub ]

  
# File 'lib/rake/testtask.rb', line 147

def ruby_opts_string # :nodoc:
  opts = @ruby_opts.dup
  opts.unshift("-I\"#{lib_path}\"") unless @libs.empty?
  opts.unshift("-w") if @warning
  opts.join(" ")
end

#ruby_version

This method is for internal use only.
[ GitHub ]

  
# File 'lib/rake/testtask.rb', line 173

def ruby_version # :nodoc:
  RUBY_VERSION
end

#run_code

This method is for internal use only.
[ GitHub ]

  
# File 'lib/rake/testtask.rb', line 177

def run_code # :nodoc:
  case @loader
  when :direct
    "-e \"ARGV.each{|f| require f}\""
  when :testrb
    "-S testrb"
  when :rake
    "#{rake_include_arg} \"#{rake_loader}\""
  end
end