From 5a90fb577a184ab0c3af4d701f45ef2ba43eb3d0 Mon Sep 17 00:00:00 2001 From: Thomas Sawyer Date: Tue, 17 Nov 2026 13:57:33 -0500 Subject: [PATCH 1/2] 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 --- HISTORY.md | 7 +++ README.md | 27 ++++++++- demo/06_rake_task.md | 87 +++++++++++++++++++++++++++++ lib/rubytest/rake.rb | 128 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 247 insertions(+), 2 deletions(-) create mode 100644 demo/06_rake_task.md create mode 100644 lib/rubytest/rake.rb 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 From bc3cdee451feabca67a985d8049f1a0253a89d96 Mon Sep 17 00:00:00 2001 From: Thomas Sawyer Date: Tue, 17 Nov 2026 13:59:06 -0500 Subject: [PATCH 2/2] Bring the HTML reporter into Rubytest rubytest-html's reporter predates the recorder and no longer loads. Rewrite it on record and finish: one self-contained document, written when the run finishes, with results nested under their cases. Escape names, messages and output, report skipped tests and cases, and capture test output so it appears with its test. Co-Authored-By: Claude Fable 5.2 --- HISTORY.md | 3 + README.md | 6 +- demo/05_builtin_reporters.md | 24 ++++++ lib/rubytest/format/html.rb | 144 +++++++++++++++++++++++++++++++++++ lib/rubytest/runner.rb | 2 +- 5 files changed, 176 insertions(+), 3 deletions(-) create mode 100644 lib/rubytest/format/html.rb diff --git a/HISTORY.md b/HISTORY.md index dd5d2db..2615e40 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -2,6 +2,9 @@ ## Unreleased +* Bring the HTML reporter in from rubytest-html, rewritten for the recorder: + `rubytest -f html`. It escapes what it writes, reports skipped tests and + cases, and shows captured output with each test. * 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` diff --git a/README.md b/README.md index f3f580e..1a858d9 100644 --- a/README.md +++ b/README.md @@ -58,8 +58,10 @@ test), `outline` (nested cases), `progress` (running count and timing), `tap` (TAP version 13), and `tapy` and `tapj` (TAP-Y and TAP-J, the structured streams that [tapout](https://github.com/rubyworks/tapout) reads). Select one with `rubytest -f FORMAT`; `-y` and `-j` are short for `-f tapy` and `-f tapj`. -Formats load only when selected. The TAP formats capture test output: TAP -emits it as diagnostics, and TAP-Y and TAP-J record it with each test. +`html` writes the whole run as one self-contained HTML document +(`rubytest -f html > report.html`). Formats load only when selected. The TAP +and HTML formats capture test output: TAP emits it as diagnostics, and TAP-Y, +TAP-J and HTML record it with each test. ## Installation diff --git a/demo/05_builtin_reporters.md b/demo/05_builtin_reporters.md index bab28b4..2534679 100644 --- a/demo/05_builtin_reporters.md +++ b/demo/05_builtin_reporters.md @@ -116,3 +116,27 @@ The command line has a shortcut for each. cli.options.parse!([flag]) cli.format.assert == format end + +The HTML format writes one self-contained document once the run has +finished. Names, messages and captured output are escaped. + + markup = probe_type.new('markup &') { print '