Skip to content

Commit 5a90fb5

Browse files
transclaude
andcommitted
Bring the Rake task into Rubytest
Add rubytest/rake with Test::Rake::TestTask, from rubytest-rake 0.2.0. Check the forked run's exit status, so the task fails when the run does: rubytest-rake waited for the child and then always exited 0. Use only the default globs that match a file, since the runner now fails a glob that matches nothing. Let TEST replace the task's files for one invocation. Drop the shell mode, which called an undefined method. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
1 parent 502ed9c commit 5a90fb5

4 files changed

Lines changed: 247 additions & 2 deletions

File tree

‎HISTORY.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# RELEASE HISTORY
22

3+
## Unreleased
4+
5+
* Bring the Rake task in from rubytest-rake: `require 'rubytest/rake'` for
6+
`Test::Rake::TestTask`. The task now fails when the run does (it always
7+
exited 0), runs only the default globs that match a file, and lets `TEST`
8+
replace the task's files for one invocation.
9+
310
## 0.10.0 / 2026-11-17
411

512
Results now go through a recorder to the reporters, and the reporters that

‎README.md‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,31 @@ named profile, use the `-p/--profile` option:
122122
The configuration file can also be at `config/test.rb`, `Testfile`,
123123
or `.test` (with optional `.rb` extension).
124124

125-
If you are using Rake, shelling out to `rubytest` keeps your test
126-
environment pristine:
125+
### Via Rake
126+
127+
Rubytest has a Rake task. In your Rakefile:
128+
129+
require 'rubytest/rake'
130+
131+
Test::Rake::TestTask.new do |run|
132+
run.loadpath 'lib'
133+
run.test_files << 'test/*_test.rb'
134+
end
135+
136+
The block takes the same settings as `Test.configure`. With no files, the
137+
task runs whichever of `test/**/test_*.rb`, `*_test.rb`, `case_*.rb` and
138+
`*_case.rb` it finds, and with no load path it adds `lib`. The task is named
139+
`test` unless you give it another name (`TestTask.new('spec')`).
140+
141+
To run only some tests for one invocation, set `TEST`:
142+
143+
$ rake test TEST=test/foo_test.rb
144+
145+
Where the platform can fork, the tests run in a child process, so what they
146+
load stays out of Rake's. `rake test` fails if the run does.
147+
148+
A configuration file isn't read by the task. To use one, shell out to
149+
`rubytest` instead:
127150

128151
desc "run tests"
129152
task :test do

‎demo/06_rake_task.md‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
## Rake task
2+
3+
`rubytest/rake` provides a task for running tests from a Rakefile.
4+
5+
require 'rubytest/rake'
6+
require 'tmpdir'
7+
8+
project = Dir.mktmpdir('rubytest-rake')
9+
Dir.mkdir(File.join(project, 'test'))
10+
11+
probe = lambda do |label, body|
12+
<<-RUBY
13+
probe = Object.new
14+
probe.define_singleton_method(:call) { #{body} }
15+
probe.define_singleton_method(:to_s) { #{label.inspect} }
16+
$TEST_SUITE << probe
17+
RUBY
18+
end
19+
20+
File.write(File.join(project, 'test', 'good_test.rb'), probe.call('good', 'true'))
21+
File.write(File.join(project, 'bad.rb'), probe.call('bad', "raise Assertion, 'wrong value'"))
22+
23+
status_of = lambda do |task|
24+
Dir.chdir(project) do
25+
begin
26+
task.run
27+
0
28+
rescue SystemExit => exit
29+
exit.status
30+
end
31+
end
32+
end
33+
34+
The block configures the run, as with `Test.configure`. The task is an
35+
ordinary Rake task.
36+
37+
passing = Test::Rake::TestTask.new('demo:pass') do |run|
38+
run.files << 'test/good_test.rb'
39+
run.format = 'test'
40+
end
41+
42+
Rake::Task.task_defined?('demo:pass').assert == true
43+
status_of.call(passing).assert == 0
44+
45+
A run that fails makes the task exit with status 1, so that `rake` fails too.
46+
47+
failing = Test::Rake::TestTask.new('demo:fail') do |run|
48+
run.files << 'bad.rb'
49+
run.format = 'test'
50+
end
51+
52+
status_of.call(failing).assert == 1
53+
54+
Given no files, the task looks under `test/` for files named `test_*.rb`,
55+
`*_test.rb`, `case_*.rb` or `*_case.rb`, and runs whichever of those it
56+
finds. It also puts `lib` on the load path.
57+
58+
defaults = Test::Rake::TestTask.new('demo:defaults') { |run| run.format = 'test' }
59+
60+
status_of.call(defaults).assert == 0
61+
defaults.config.files.assert == ['test/**/*_test.rb']
62+
defaults.config.loadpath.assert == ['lib']
63+
64+
If it finds none, it says so.
65+
66+
empty = Dir.mktmpdir('rubytest-rake-empty')
67+
nothing = Test::Rake::TestTask.new('demo:nothing') { |run| run.format = 'test' }
68+
missing = begin
69+
Dir.chdir(empty) { nothing.run }
70+
nil
71+
rescue ArgumentError => error
72+
error
73+
end
74+
75+
missing.message.assert.start_with?('no test files found')
76+
77+
The `TEST` environment variable selects the tests for one invocation, in
78+
place of the task's own.
79+
80+
previous = ENV['TEST']
81+
begin
82+
ENV['TEST'] = 'bad.rb'
83+
status_of.call(passing).assert == 1
84+
passing.config.files.assert == ['bad.rb']
85+
ensure
86+
ENV['TEST'] = previous
87+
end

‎lib/rubytest/rake.rb‎

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
require 'rubytest'
2+
require 'rake'
3+
require 'rake/tasklib'
4+
5+
module Test
6+
7+
##
8+
# Rake subspace.
9+
#
10+
module Rake
11+
12+
##
13+
# Define a rake task that runs tests.
14+
#
15+
# require 'rubytest/rake'
16+
#
17+
# Test::Rake::TestTask.new do |run|
18+
# run.loadpath 'lib'
19+
# run.files << 'test/*_test.rb'
20+
# end
21+
#
22+
# The block is given the task's Test::Config, the same object that
23+
# `Test.configure` yields. Without any files, the task runs whichever
24+
# of DEFAULT_TESTS match, and without a load path it adds `lib`.
25+
#
26+
# The `TEST` environment variable selects the tests to run for one
27+
# invocation, in place of the task's own: `rake test TEST=test/foo_test.rb`.
28+
# Separate several files or globs with `:` or `;`.
29+
#
30+
# Where the platform can fork, tests run in a child process, so that
31+
# what they load doesn't stay in Rake's. If the run is unsuccessful the
32+
# task exits with status 1.
33+
#
34+
class TestTask < ::Rake::TaskLib
35+
36+
# Globs used to find test files when the task is given none.
37+
DEFAULT_TESTS = [
38+
'test/**/case_*.rb',
39+
'test/**/*_case.rb',
40+
'test/**/test_*.rb',
41+
'test/**/*_test.rb'
42+
]
43+
44+
# Name of the task.
45+
#
46+
# @return [String]
47+
attr :name
48+
49+
# Test run configuration.
50+
#
51+
# @return [Config]
52+
attr :config
53+
54+
# Initialize new Rake::TestTask instance.
55+
#
56+
def initialize(name='test', desc='run tests', &block)
57+
@name = (name || 'test').to_s
58+
@desc = desc
59+
60+
@config = Test::Config.new
61+
62+
block.call(@config) if block
63+
64+
define_task
65+
end
66+
67+
# Define rake task for testing.
68+
#
69+
# @return nothing
70+
def define_task
71+
desc @desc
72+
task @name do
73+
run
74+
end
75+
end
76+
77+
# Run tests, in a child process if possible, otherwise in this one.
78+
# Exits with status 1 if the run is unsuccessful.
79+
#
80+
# @return nothing
81+
def run
82+
prepare
83+
success = Process.respond_to?(:fork) ? run_in_fork : run_here
84+
exit 1 unless success
85+
end
86+
87+
private
88+
89+
# Settle the files and load path the run will use.
90+
def prepare
91+
if ENV['TEST']
92+
config.files = ENV['TEST'].split(/[:;]/)
93+
elsif config.files.empty?
94+
config.files = default_tests
95+
end
96+
config.loadpath << 'lib' if config.loadpath.empty?
97+
end
98+
99+
# The default globs that match at least one file. The runner fails a
100+
# glob that matches nothing, so the others are left out.
101+
#
102+
# @return [Array<String>]
103+
def default_tests
104+
base = config.chdir || Dir.pwd
105+
globs = DEFAULT_TESTS.reject{ |glob| Dir.glob(glob, base: base).empty? }
106+
if globs.empty?
107+
raise ArgumentError, "no test files found; looked for #{DEFAULT_TESTS.join(', ')}"
108+
end
109+
globs
110+
end
111+
112+
# @return [Boolean] Whether the run was successful.
113+
def run_here
114+
Test::Runner.new(config).run
115+
end
116+
117+
# @return [Boolean] Whether the run was successful.
118+
def run_in_fork
119+
pid = fork { exit(run_here ? 0 : 1) }
120+
Process.wait(pid)
121+
$?.success?
122+
end
123+
124+
end
125+
126+
end
127+
128+
end

0 commit comments

Comments
 (0)