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:



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:



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

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 command line shell.

Returns:

  • nothing



80
81
82
83
# File 'lib/rubytest/rake.rb', line 80

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

#test_filesArray<String>

TODO:

Implementation probably cna be simplified.

Resolve test globs.

Returns:

  • (Array<String>)

    List of test files.



89
90
91
92
93
94
95
96
# File 'lib/rubytest/rake.rb', line 89

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