Class: Test::Rake::TestTask

Inherits:
Rake::TaskLib
  • Object
show all
Defined in:
lib/test/rake.rb

Overview

Define a test rake task.

The ‘TEST` environment variable can be used to select tests when using the task.

TODO: The test task uses #fork. Maybe it should shell out instead?
      Or provide the option for either?

++

Constant Summary collapse

DEFAULT_TESTS =

Glob patterns are used by default to select test scripts.

[
  'test/**/case_*.rb',
  'test/**/*_case.rb',
  'test/**/test_*.rb',
  'test/**/*_test.rb'
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = 'test', desc = "run tests", &block) ⇒ TestTask

Returns a new instance of TestTask.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/test/rake.rb', line 45

def initialize(name='test', desc="run tests", &block)
  @name       = name
  @desc       = desc

  @loadpath   = ['lib']
  @requires   = []
  @tests      = [ENV['TEST'] || DEFAULT_TESTS].flatten
  @format     = nil
  @match      = nil
  @tags       = []

  block.call(self)

  define_task
end

Instance Attribute Details

#formatObject

Report format to use.



36
37
38
# File 'lib/test/rake.rb', line 36

def format
  @format
end

#loadpathObject

Paths to add to $LOAD_PATH.



30
31
32
# File 'lib/test/rake.rb', line 30

def loadpath
  @loadpath
end

#matchObject

Filter tests by matching description.



42
43
44
# File 'lib/test/rake.rb', line 42

def match
  @match
end

#requiresObject

Scripts to load prior to loading tests.



33
34
35
# File 'lib/test/rake.rb', line 33

def requires
  @requires
end

#tagsObject

Filter tests based by tags.



39
40
41
# File 'lib/test/rake.rb', line 39

def tags
  @tags
end

#testsObject

Test scripts to load. Can be a file glob.



27
28
29
# File 'lib/test/rake.rb', line 27

def tests
  @tests
end

Instance Method Details

#default_testsObject



103
104
105
106
107
108
109
# File 'lib/test/rake.rb', line 103

def default_tests
  if ENV['tests']
    ENV['tests'].split(/[:;]/)
  else
    DEFAULT_TESTS
  end
end

#define_taskObject



62
63
64
65
66
67
68
# File 'lib/test/rake.rb', line 62

def define_task
  desc @desc
  task @name do
    @tests ||= default_tests
    run
  end
end

#runObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/test/rake.rb', line 71

def run
  fork {
    #require 'test'
    require 'test/runner'

    loadpath.each   { |d| $LOAD_PATH.unshift(d) }
    requires.each   { |f| require f }
    test_files.each { |f| require f }

    suite   = $TEST_SUITE || []
    runner  = new(suite, :format=>format, :tags=>tags, :match=>match)
    success = runner.run

    exit -1 unless success
  }
  Process.wait
end

#test_filesObject

Resolve test globs. – TODO: simplify? ++



93
94
95
96
97
98
99
100
# File 'lib/test/rake.rb', line 93

def test_files
  files = tests
  files = files.map{ |f| Dir[f] }.flatten
  files = files.map{ |f| File.directory?(f) ? Dir[File.join(f, '**/*.rb')] : f }
  files = files.flatten.uniq
  files = files.map{ |f| File.expand_path(f) }
  files
end