Skip to content

Commit f4bd0fb

Browse files
al2o3crDavid Heinemeier Hansson
authored andcommitted
Correctly clean backtraces from vendor/gems and gems in alternate install locations
Signed-off-by: David Heinemeier Hansson <david@loudthinking.com>
1 parent a3e67a1 commit f4bd0fb

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

railties/lib/rails/backtrace_cleaner.rb

Lines changed: 16 additions & 4 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -4,27 +4,39 @@ class BacktraceCleaner < ActiveSupport::BacktraceCleaner
4

4

5
RAILS_GEMS = %w( actionpack activerecord actionmailer activesupport activeresource rails )
5
RAILS_GEMS = %w( actionpack activerecord actionmailer activesupport activeresource rails )
6

6

7-
VENDOR_DIRS = %w( vendor/gems vendor/rails )
7+
VENDOR_DIRS = %w( vendor/rails )
8
SERVER_DIRS = %w( lib/mongrel bin/mongrel
8
SERVER_DIRS = %w( lib/mongrel bin/mongrel
9
lib/passenger bin/passenger-spawn-server
9
lib/passenger bin/passenger-spawn-server
10
lib/rack )
10
lib/rack )
11
RAILS_NOISE = %w( script/server )
11
RAILS_NOISE = %w( script/server )
12
RUBY_NOISE = %w( rubygems/custom_require benchmark.rb )
12
RUBY_NOISE = %w( rubygems/custom_require benchmark.rb )
13

13

14-
GEMS_DIR = Gem.default_dir
15-
16
ALL_NOISE = VENDOR_DIRS + SERVER_DIRS + RAILS_NOISE + RUBY_NOISE
14
ALL_NOISE = VENDOR_DIRS + SERVER_DIRS + RAILS_NOISE + RUBY_NOISE
17

15

18
def initialize
16
def initialize
19
super
17
super
20
add_filter { |line| line.sub("#{RAILS_ROOT}/", '') }
18
add_filter { |line| line.sub("#{RAILS_ROOT}/", '') }
21
add_filter { |line| line.sub(ERB_METHOD_SIG, '') }
19
add_filter { |line| line.sub(ERB_METHOD_SIG, '') }
22
add_filter { |line| line.sub('./', '/') } # for tests
20
add_filter { |line| line.sub('./', '/') } # for tests
23-
add_filter { |line| line.sub(/(#{GEMS_DIR})\/gems\/([a-z]+)-([0-9.]+)\/(.*)/, '\2 (\3) \4')} # http://gist.github.com/30430
21+
22+
add_gem_filters
23+
24
add_silencer { |line| ALL_NOISE.any? { |dir| line.include?(dir) } }
24
add_silencer { |line| ALL_NOISE.any? { |dir| line.include?(dir) } }
25
add_silencer { |line| RAILS_GEMS.any? { |gem| line =~ /^#{gem} / } }
25
add_silencer { |line| RAILS_GEMS.any? { |gem| line =~ /^#{gem} / } }
26
add_silencer { |line| line =~ %r(vendor/plugins/[^\/]+/lib) }
26
add_silencer { |line| line =~ %r(vendor/plugins/[^\/]+/lib) }
27
end
27
end
28+
29+
30+
private
31+
def add_gem_filters
32+
Gem.path.each do |path|
33+
# http://gist.github.com/30430
34+
add_filter { |line| line.sub(/(#{path})\/gems\/([a-z]+)-([0-9.]+)\/(.*)/, '\2 (\3) \4')}
35+
end
36+
37+
vendor_gems_path = Rails::GemDependency.unpacked_path.sub("#{RAILS_ROOT}/",'')
38+
add_filter { |line| line.sub(/(#{vendor_gems_path})\/([a-z]+)-([0-9.]+)\/(.*)/, '\2 (\3) [v] \4')}
39+
end
28
end
40
end
29

41

30
# For installing the BacktraceCleaner in the test/unit
42
# For installing the BacktraceCleaner in the test/unit

railties/test/backtrace_cleaner_test.rb

Lines changed: 29 additions & 0 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -30,3 +30,32 @@ def setup
30
else
30
else
31
$stderr.puts 'No BacktraceFilter for minitest'
31
$stderr.puts 'No BacktraceFilter for minitest'
32
end
32
end
33+
34+
class BacktraceCleanerVendorGemTest < ActiveSupport::TestCase
35+
def setup
36+
@cleaner = Rails::BacktraceCleaner.new
37+
end
38+
39+
test "should format installed gems correctly" do
40+
@backtrace = [ "#{Gem.default_dir}/gems/nosuchgem-1.2.3/lib/foo.rb" ]
41+
@result = @cleaner.clean(@backtrace)
42+
assert_equal "nosuchgem (1.2.3) lib/foo.rb", @result[0]
43+
end
44+
45+
test "should format installed gems not in Gem.default_dir correctly" do
46+
@target_dir = Gem.path.detect { |p| p != Gem.default_dir }
47+
# skip this test if default_dir is the only directory on Gem.path
48+
if @target_dir
49+
@backtrace = [ "#{@target_dir}/gems/nosuchgem-1.2.3/lib/foo.rb" ]
50+
@result = @cleaner.clean(@backtrace)
51+
assert_equal "nosuchgem (1.2.3) lib/foo.rb", @result[0]
52+
end
53+
end
54+
55+
test "should format vendor gems correctly" do
56+
@backtrace = [ "#{Rails::GemDependency.unpacked_path}/nosuchgem-1.2.3/lib/foo.rb" ]
57+
@result = @cleaner.clean(@backtrace)
58+
assert_equal "nosuchgem (1.2.3) [v] lib/foo.rb", @result[0]
59+
end
60+
61+
end

0 commit comments

Comments
 (0)