Skip to content

Commit

Permalink
Correctly clean backtraces from vendor/gems and gems in alternate ins…
Browse files Browse the repository at this point in the history
…tall locations

Signed-off-by: David Heinemeier Hansson <david@loudthinking.com>
  • Loading branch information
al2o3cr authored and dhh committed Mar 9, 2009
1 parent a3e67a1 commit f4bd0fb
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
20 changes: 16 additions & 4 deletions railties/lib/rails/backtrace_cleaner.rb
Expand Up @@ -4,27 +4,39 @@ class BacktraceCleaner < ActiveSupport::BacktraceCleaner


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


VENDOR_DIRS = %w( vendor/gems vendor/rails ) VENDOR_DIRS = %w( vendor/rails )
SERVER_DIRS = %w( lib/mongrel bin/mongrel SERVER_DIRS = %w( lib/mongrel bin/mongrel
lib/passenger bin/passenger-spawn-server lib/passenger bin/passenger-spawn-server
lib/rack ) lib/rack )
RAILS_NOISE = %w( script/server ) RAILS_NOISE = %w( script/server )
RUBY_NOISE = %w( rubygems/custom_require benchmark.rb ) RUBY_NOISE = %w( rubygems/custom_require benchmark.rb )


GEMS_DIR = Gem.default_dir

ALL_NOISE = VENDOR_DIRS + SERVER_DIRS + RAILS_NOISE + RUBY_NOISE ALL_NOISE = VENDOR_DIRS + SERVER_DIRS + RAILS_NOISE + RUBY_NOISE


def initialize def initialize
super super
add_filter { |line| line.sub("#{RAILS_ROOT}/", '') } add_filter { |line| line.sub("#{RAILS_ROOT}/", '') }
add_filter { |line| line.sub(ERB_METHOD_SIG, '') } add_filter { |line| line.sub(ERB_METHOD_SIG, '') }
add_filter { |line| line.sub('./', '/') } # for tests add_filter { |line| line.sub('./', '/') } # for tests
add_filter { |line| line.sub(/(#{GEMS_DIR})\/gems\/([a-z]+)-([0-9.]+)\/(.*)/, '\2 (\3) \4')} # http://gist.github.com/30430
add_gem_filters

add_silencer { |line| ALL_NOISE.any? { |dir| line.include?(dir) } } add_silencer { |line| ALL_NOISE.any? { |dir| line.include?(dir) } }
add_silencer { |line| RAILS_GEMS.any? { |gem| line =~ /^#{gem} / } } add_silencer { |line| RAILS_GEMS.any? { |gem| line =~ /^#{gem} / } }
add_silencer { |line| line =~ %r(vendor/plugins/[^\/]+/lib) } add_silencer { |line| line =~ %r(vendor/plugins/[^\/]+/lib) }
end end


private
def add_gem_filters
Gem.path.each do |path|
# http://gist.github.com/30430
add_filter { |line| line.sub(/(#{path})\/gems\/([a-z]+)-([0-9.]+)\/(.*)/, '\2 (\3) \4')}
end

vendor_gems_path = Rails::GemDependency.unpacked_path.sub("#{RAILS_ROOT}/",'')
add_filter { |line| line.sub(/(#{vendor_gems_path})\/([a-z]+)-([0-9.]+)\/(.*)/, '\2 (\3) [v] \4')}
end
end end


# For installing the BacktraceCleaner in the test/unit # For installing the BacktraceCleaner in the test/unit
Expand Down
29 changes: 29 additions & 0 deletions railties/test/backtrace_cleaner_test.rb
Expand Up @@ -30,3 +30,32 @@ def setup
else else
$stderr.puts 'No BacktraceFilter for minitest' $stderr.puts 'No BacktraceFilter for minitest'
end end

class BacktraceCleanerVendorGemTest < ActiveSupport::TestCase
def setup
@cleaner = Rails::BacktraceCleaner.new
end

test "should format installed gems correctly" do
@backtrace = [ "#{Gem.default_dir}/gems/nosuchgem-1.2.3/lib/foo.rb" ]
@result = @cleaner.clean(@backtrace)
assert_equal "nosuchgem (1.2.3) lib/foo.rb", @result[0]
end

test "should format installed gems not in Gem.default_dir correctly" do
@target_dir = Gem.path.detect { |p| p != Gem.default_dir }
# skip this test if default_dir is the only directory on Gem.path
if @target_dir
@backtrace = [ "#{@target_dir}/gems/nosuchgem-1.2.3/lib/foo.rb" ]
@result = @cleaner.clean(@backtrace)
assert_equal "nosuchgem (1.2.3) lib/foo.rb", @result[0]
end
end

test "should format vendor gems correctly" do
@backtrace = [ "#{Rails::GemDependency.unpacked_path}/nosuchgem-1.2.3/lib/foo.rb" ]
@result = @cleaner.clean(@backtrace)
assert_equal "nosuchgem (1.2.3) [v] lib/foo.rb", @result[0]
end

end

0 comments on commit f4bd0fb

Please sign in to comment.