Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# RELEASE HISTORY

## Unreleased

* Bring the Rake task in from rubytest-rake: `require 'rubytest/rake'` for
`Test::Rake::TestTask`. The task now fails when the run does (it always
exited 0), runs only the default globs that match a file, and lets `TEST`
replace the task's files for one invocation.

## 0.10.0 / 2026-11-17

Results now go through a recorder to the reporters, and the reporters that
Expand Down
27 changes: 25 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,31 @@ named profile, use the `-p/--profile` option:
The configuration file can also be at `config/test.rb`, `Testfile`,
or `.test` (with optional `.rb` extension).

If you are using Rake, shelling out to `rubytest` keeps your test
environment pristine:
### Via Rake

Rubytest has a Rake task. In your Rakefile:

require 'rubytest/rake'

Test::Rake::TestTask.new do |run|
run.loadpath 'lib'
run.test_files << 'test/*_test.rb'
end

The block takes the same settings as `Test.configure`. With no files, the
task runs whichever of `test/**/test_*.rb`, `*_test.rb`, `case_*.rb` and
`*_case.rb` it finds, and with no load path it adds `lib`. The task is named
`test` unless you give it another name (`TestTask.new('spec')`).

To run only some tests for one invocation, set `TEST`:

$ rake test TEST=test/foo_test.rb

Where the platform can fork, the tests run in a child process, so what they
load stays out of Rake's. `rake test` fails if the run does.

A configuration file isn't read by the task. To use one, shell out to
`rubytest` instead:

desc "run tests"
task :test do
Expand Down
87 changes: 87 additions & 0 deletions demo/06_rake_task.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
## Rake task

`rubytest/rake` provides a task for running tests from a Rakefile.

require 'rubytest/rake'
require 'tmpdir'

project = Dir.mktmpdir('rubytest-rake')
Dir.mkdir(File.join(project, 'test'))

probe = lambda do |label, body|
<<-RUBY
probe = Object.new
probe.define_singleton_method(:call) { #{body} }
probe.define_singleton_method(:to_s) { #{label.inspect} }
$TEST_SUITE << probe
RUBY
end

File.write(File.join(project, 'test', 'good_test.rb'), probe.call('good', 'true'))
File.write(File.join(project, 'bad.rb'), probe.call('bad', "raise Assertion, 'wrong value'"))

status_of = lambda do |task|
Dir.chdir(project) do
begin
task.run
0
rescue SystemExit => exit
exit.status
end
end
end

The block configures the run, as with `Test.configure`. The task is an
ordinary Rake task.

passing = Test::Rake::TestTask.new('demo:pass') do |run|
run.files << 'test/good_test.rb'
run.format = 'test'
end

Rake::Task.task_defined?('demo:pass').assert == true
status_of.call(passing).assert == 0

A run that fails makes the task exit with status 1, so that `rake` fails too.

failing = Test::Rake::TestTask.new('demo:fail') do |run|
run.files << 'bad.rb'
run.format = 'test'
end

status_of.call(failing).assert == 1

Given no files, the task looks under `test/` for files named `test_*.rb`,
`*_test.rb`, `case_*.rb` or `*_case.rb`, and runs whichever of those it
finds. It also puts `lib` on the load path.

defaults = Test::Rake::TestTask.new('demo:defaults') { |run| run.format = 'test' }

status_of.call(defaults).assert == 0
defaults.config.files.assert == ['test/**/*_test.rb']
defaults.config.loadpath.assert == ['lib']

If it finds none, it says so.

empty = Dir.mktmpdir('rubytest-rake-empty')
nothing = Test::Rake::TestTask.new('demo:nothing') { |run| run.format = 'test' }
missing = begin
Dir.chdir(empty) { nothing.run }
nil
rescue ArgumentError => error
error
end

missing.message.assert.start_with?('no test files found')

The `TEST` environment variable selects the tests for one invocation, in
place of the task's own.

previous = ENV['TEST']
begin
ENV['TEST'] = 'bad.rb'
status_of.call(passing).assert == 1
passing.config.files.assert == ['bad.rb']
ensure
ENV['TEST'] = previous
end
128 changes: 128 additions & 0 deletions lib/rubytest/rake.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
require 'rubytest'
require 'rake'
require 'rake/tasklib'

module Test

##
# Rake subspace.
#
module Rake

##
# Define a rake task that runs tests.
#
# require 'rubytest/rake'
#
# Test::Rake::TestTask.new do |run|
# run.loadpath 'lib'
# run.files << 'test/*_test.rb'
# end
#
# The block is given the task's Test::Config, the same object that
# `Test.configure` yields. Without any files, the task runs whichever
# of DEFAULT_TESTS match, and without a load path it adds `lib`.
#
# The `TEST` environment variable selects the tests to run for one
# invocation, in place of the task's own: `rake test TEST=test/foo_test.rb`.
# Separate several files or globs with `:` or `;`.
#
# Where the platform can fork, tests run in a child process, so that
# what they load doesn't stay in Rake's. If the run is unsuccessful the
# task exits with status 1.
#
class TestTask < ::Rake::TaskLib

# Globs used to find test files when the task is given none.
DEFAULT_TESTS = [
'test/**/case_*.rb',
'test/**/*_case.rb',
'test/**/test_*.rb',
'test/**/*_test.rb'
]

# Name of the task.
#
# @return [String]
attr :name

# Test run configuration.
#
# @return [Config]
attr :config

# Initialize new Rake::TestTask instance.
#
def initialize(name='test', desc='run tests', &block)
@name = (name || 'test').to_s
@desc = desc

@config = Test::Config.new

block.call(@config) if block

define_task
end

# Define rake task for testing.
#
# @return nothing
def define_task
desc @desc
task @name do
run
end
end

# Run tests, in a child process if possible, otherwise in this one.
# Exits with status 1 if the run is unsuccessful.
#
# @return nothing
def run
prepare
success = Process.respond_to?(:fork) ? run_in_fork : run_here
exit 1 unless success
end

private

# Settle the files and load path the run will use.
def prepare
if ENV['TEST']
config.files = ENV['TEST'].split(/[:;]/)
elsif config.files.empty?
config.files = default_tests
end
config.loadpath << 'lib' if config.loadpath.empty?
end

# The default globs that match at least one file. The runner fails a
# glob that matches nothing, so the others are left out.
#
# @return [Array<String>]
def default_tests
base = config.chdir || Dir.pwd
globs = DEFAULT_TESTS.reject{ |glob| Dir.glob(glob, base: base).empty? }
if globs.empty?
raise ArgumentError, "no test files found; looked for #{DEFAULT_TESTS.join(', ')}"
end
globs
end

# @return [Boolean] Whether the run was successful.
def run_here
Test::Runner.new(config).run
end

# @return [Boolean] Whether the run was successful.
def run_in_fork
pid = fork { exit(run_here ? 0 : 1) }
Process.wait(pid)
$?.success?
end

end

end

end
Loading