Skip to content

Commit

Permalink
Merge pull request #25222 from vipulnsward/25219-fix-logs
Browse files Browse the repository at this point in the history
Clean backtrace in IRB
  • Loading branch information
matthewd committed Jul 2, 2016
2 parents 8fb6995 + 8e1714b commit edc5603
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
2 changes: 1 addition & 1 deletion railties/lib/rails/backtrace_cleaner.rb
Expand Up @@ -2,7 +2,7 @@

module Rails
class BacktraceCleaner < ActiveSupport::BacktraceCleaner
APP_DIRS_PATTERN = /^\/?(app|config|lib|test)/
APP_DIRS_PATTERN = /^\/?(app|config|lib|test|\(\w*\))/
RENDER_TEMPLATE_PATTERN = /:in `_render_template_\w*'/
EMPTY_STRING = ''.freeze
SLASH = '/'.freeze
Expand Down
12 changes: 12 additions & 0 deletions railties/lib/rails/commands/console.rb
Expand Up @@ -7,6 +7,14 @@ module Rails
class Console
include ConsoleHelper

module BacktraceCleaner
def filter_backtrace(bt)
if result = super
Rails.backtrace_cleaner.filter([result]).first
end
end
end

class << self
def parse_arguments(arguments)
options = {}
Expand Down Expand Up @@ -34,6 +42,10 @@ def initialize(app, options={})
app.load_console

@console = app.config.console || IRB

if @console == IRB
IRB::WorkSpace.prepend(BacktraceCleaner)
end
end

def sandbox?
Expand Down
24 changes: 16 additions & 8 deletions railties/test/backtrace_cleaner_test.rb
@@ -1,24 +1,32 @@
require 'abstract_unit'
require 'rails/backtrace_cleaner'

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

test "should format installed gems correctly" do
@backtrace = [ "#{Gem.path[0]}/gems/nosuchgem-1.2.3/lib/foo.rb" ]
@result = @cleaner.clean(@backtrace, :all)
assert_equal "nosuchgem (1.2.3) lib/foo.rb", @result[0]
backtrace = [ "#{Gem.path[0]}/gems/nosuchgem-1.2.3/lib/foo.rb" ]
result = @cleaner.clean(backtrace, :all)
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 }
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, :all)
assert_equal "nosuchgem (1.2.3) lib/foo.rb", @result[0]
backtrace = [ "#{target_dir}/gems/nosuchgem-1.2.3/lib/foo.rb" ]
result = @cleaner.clean(backtrace, :all)
assert_equal "nosuchgem (1.2.3) lib/foo.rb", result[0]
end
end

test "should consider traces from irb lines as User code" do
backtrace = [ "from (irb):1",
"from /Path/to/rails/railties/lib/rails/commands/console.rb:77:in `start'",
"from bin/rails:4:in `<main>'" ]
result = @cleaner.clean(backtrace, :all)
assert_equal "from (irb):1", result[0]
end
end

0 comments on commit edc5603

Please sign in to comment.