Skip to content

Commit 84eac5d

Browse files
committed
BACKTRACE environment variable to show unfiltered backtraces.
We used to support the `BACKTRACE` environment variable but when we switched to MiniTest it got removed: f9382cd This commit adds back the functionality to show the unfiltered backtrace when needed. This also works when you run your tests with `rake`: * `BACKTRACE=1 bin/rake test` * `BACKTRACE=1 ruby -Itest ...`
1 parent 876fd5a commit 84eac5d

File tree

3 files changed

+48
-6
lines changed

3 files changed

+48
-6
lines changed

railties/CHANGELOG.md

+11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
* `BACKTRACE` environment variable to show unfiltered backtraces for
2+
test failures.
3+
4+
Example:
5+
6+
`BACKTRACE=1 ruby -Itest ...`
7+
# or with rake
8+
`BAKCTRACE=1 bin/rake`
9+
10+
*Yves Senn*
11+
112
* Removal of all javascript stuff (gems and files) when generating a new
213
application using the `--skip-javascript` option.
314

railties/lib/rails/test_help.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010

1111
# Config Rails backtrace in tests.
1212
require 'rails/backtrace_cleaner'
13-
MiniTest.backtrace_filter = Rails.backtrace_cleaner
13+
if ENV["BACKTRACE"].nil?
14+
MiniTest.backtrace_filter = Rails.backtrace_cleaner
15+
end
1416

1517
if defined?(ActiveRecord::Base)
1618
class ActiveSupport::TestCase

railties/test/application/test_test.rb

+34-5
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def test_truth
2424
end
2525
RUBY
2626

27-
run_test_file 'unit/foo_test.rb'
27+
assert_successful_test_run 'unit/foo_test.rb'
2828
end
2929

3030
test "integration test" do
@@ -49,19 +49,48 @@ def test_index
4949
end
5050
RUBY
5151

52-
run_test_file 'integration/posts_test.rb'
52+
assert_successful_test_run 'integration/posts_test.rb'
53+
end
54+
55+
test "enable full backtraces on test failures" do
56+
app_file 'test/unit/failing_test.rb', <<-RUBY
57+
require 'test_helper'
58+
59+
class FailingTest < ActiveSupport::TestCase
60+
def test_failure
61+
raise "fail"
62+
end
63+
end
64+
RUBY
65+
66+
output = run_test_file('unit/failing_test.rb', env: { "BACKTRACE" => "1" })
67+
assert_match %r{/app/test/unit/failing_test\.rb}, output
5368
end
5469

5570
private
56-
def run_test_file(name)
57-
result = ruby '-Itest', "#{app_path}/test/#{name}"
71+
def assert_successful_test_run(name)
72+
result = run_test_file(name)
5873
assert_equal 0, $?.to_i, result
5974
end
6075

76+
def run_test_file(name, options = {})
77+
ruby '-Itest', "#{app_path}/test/#{name}", options
78+
end
79+
6180
def ruby(*args)
81+
options = args.extract_options!
82+
env = options.fetch(:env, {})
83+
env["RUBYLIB"] = $:.join(':')
84+
6285
Dir.chdir(app_path) do
63-
`RUBYLIB='#{$:.join(':')}' #{Gem.ruby} #{args.join(' ')}`
86+
`#{env_string(env)} #{Gem.ruby} #{args.join(' ')}`
6487
end
6588
end
89+
90+
def env_string(variables)
91+
variables.map do |key, value|
92+
"#{key}='#{value}'"
93+
end.join " "
94+
end
6695
end
6796
end

0 commit comments

Comments
 (0)