diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 9eb6151..36ff2a8 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -11,7 +11,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - ruby: ['3.1', '3.2', '3.3', '3.4'] + ruby: ['2.7', '3.0', '3.1', '3.2', '3.3', '3.4'] steps: - uses: actions/checkout@v4 - uses: ruby/setup-ruby@v1 diff --git a/HISTORY.md b/HISTORY.md index 21a1239..ce90a91 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -2,6 +2,10 @@ ## Unreleased +* Support Ruby 2.7 and 3.0 again (0.9.0 required 3.1). +* Report an error raised outside of a test, such as while a test file loads, + with its class, where it was raised, and a pointer to `--debug`, instead of + the bare message. * Bring summary, outline, and progress reporters into Rubytest and add a TAP 13 reporter that counts completed results, including skipped and broken cases. * Remove CLI shortcuts for the unbundled TAP-Y and TAP-J formats. diff --git a/demo/03_runner_reliability.md b/demo/03_runner_reliability.md index 70c4f6a..1e3ff75 100644 --- a/demo/03_runner_reliability.md +++ b/demo/03_runner_reliability.md @@ -120,3 +120,27 @@ The `--config` option accepts an alternate configuration file. cli = Test::CLI.new cli.options.parse!(['--config', 'custom-test.rb']) cli.config_file.assert == 'custom-test.rb' + +### Errors outside of tests + +An error raised outside of any test, such as one raised while a test file +loads, ends the run. The report names the error's class, says where it was +raised, and points to the full backtrace. + + load_error = begin + ''.freeze << 'x' + rescue FrozenError => error + error + end + + report = Test.error_report(load_error) + lines = report.lines.map(&:chomp) + + lines[0].assert.start_with?('ERROR: FrozenError: ') + lines[1].assert.start_with?(' at ') + lines[1].include?(load_error.backtrace.first).assert == true + lines[2].include?('--debug').assert == true + +An error without a message or a backtrace still names its class. + + Test.error_report(RuntimeError.new).lines.first.chomp.assert == 'ERROR: RuntimeError' diff --git a/lib/rubytest/runner.rb b/lib/rubytest/runner.rb index 4812ccc..1cb2f24 100644 --- a/lib/rubytest/runner.rb +++ b/lib/rubytest/runner.rb @@ -21,11 +21,27 @@ def self.run!(config=nil, &config_proc) exit -1 unless success rescue => error raise error if $DEBUG - $stderr.puts('ERROR: ' + error.to_s) + $stderr.puts(error_report(error)) exit -1 end end + # What `run!` prints for an error raised outside of any test, such as one + # raised while a test file loads: the error's class and message, where it + # was raised, and how to see the rest of the backtrace. + # + # @return [String] + def self.error_report(error) + message = error.message.to_s + report = "ERROR: #{error.class}" + report << ": #{message}" unless message.empty? || message == error.class.name + if location = (error.backtrace || []).first + report << "\n at #{location}" + end + report << "\n(Use --debug, or ruby -d, for the full backtrace.)" + report + end + # The Test::Runner class handles the execution of tests. # class Runner diff --git a/rubytest.gemspec b/rubytest.gemspec index 3ab09c5..6c745f0 100644 --- a/rubytest.gemspec +++ b/rubytest.gemspec @@ -11,7 +11,7 @@ Gem::Specification.new do |s| s.homepage = 'https://github.com/rubyworks/rubytest' s.license = 'BSD-2-Clause' - s.required_ruby_version = '>= 3.1' + s.required_ruby_version = '>= 2.7' s.files = Dir['lib/**/*', 'bin/*', 'LICENSE.txt', 'README.md', 'HISTORY.md', 'demo/**/*'] s.bindir = 'bin'