Class: Test::Rake::TestTask

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

Overview

Define a test rake task.

The ‘TEST` environment variable can be used to select tests when using this task. Note, this is just a more convenient way than using `RUBYTEST_FILES`.

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

Initialize new Rake::TestTask instance.



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rubytest-rake.rb', line 35

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

  @config = Test::Config.new

  @config.files << default_tests if @config.files.empty?
  @config.loadpath << 'lib' if @config.loadpath.empty?

  block.call(@config)

  define_task
end

Instance Attribute Details

#configConfig (readonly)

Test run configuration.

Returns:

  • (Config)


31
32
33
# File 'lib/rubytest-rake.rb', line 31

def config
  @config
end

Instance Method Details

#default_testsArray<String>

Default test globs. For extra convenience will look for list in ‘ENV` first.

Returns:

  • (Array<String>)


106
107
108
109
110
111
112
# File 'lib/rubytest-rake.rb', line 106

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

#define_taskObject

Define rake task for testing.

Returns:

  • nothing



52
53
54
55
56
57
# File 'lib/rubytest-rake.rb', line 52

def define_task
  desc @desc
  task @name do
    config.mode == 'shell' ? run_shell : run
  end
end

#runObject

Run tests, via fork is possible, otherwise straight out.

Returns:

  • nothing



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rubytest-rake.rb', line 62

def run
  if Process.respond_to?(:fork)
    fork {
      runner  = Test::Runner.new(config)
      success = runner.run
      exit -1 unless success
    }
    Process.wait
  else
    runner  = Test::Runner.new(config)
    success = runner.run
    exit -1 unless success
  end
end

#shell_runObject

Run test via shell. (Not Currently Used)

Note, the problem with this approach is that before and after procedures cannot be passed along. In it’s current form it also requires that ‘rubytest-cli` be installed.

Returns:

  • nothing



84
85
86
87
# File 'lib/rubytest-rake.rb', line 84

def shell_run
  success = ruby('rubytest', *config.to_shellwords)
  exit -1 unless success
end