diff --git a/HISTORY.md b/HISTORY.md index 550bdb9..dd5d2db 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -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 diff --git a/README.md b/README.md index a8afe26..f3f580e 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/demo/06_rake_task.md b/demo/06_rake_task.md new file mode 100644 index 0000000..e63628c --- /dev/null +++ b/demo/06_rake_task.md @@ -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 diff --git a/lib/rubytest/rake.rb b/lib/rubytest/rake.rb new file mode 100644 index 0000000..5058f01 --- /dev/null +++ b/lib/rubytest/rake.rb @@ -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] + 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