Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewn617 committed Apr 4, 2024
1 parent 02f6c29 commit 3e45a93
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 9 deletions.
21 changes: 13 additions & 8 deletions activesupport/lib/active_support/backtrace_cleaner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def initialize
# against it. Filters run first, then silencers.
def clean(backtrace, kind = :silent)
filtered = filter_backtrace(backtrace)

require "debug"; binding.b
case kind
when :silent
silence(filtered)
Expand All @@ -66,9 +66,9 @@ def clean_frame(frame, kind = :silent)

case kind
when :silent
frame unless @silencers.any? { |s| s.call(frame) }
frame unless silencers.any? { |s| s.call(frame) }
when :noise
frame if @silencers.any? { |s| s.call(frame) }
frame if silencers.any? { |s| s.call(frame) }
else
frame
end
Expand All @@ -90,14 +90,14 @@ def add_filter(&block)
# # Will reject all lines that include the word "puma", like "/gems/puma/server.rb" or "/app/my_puma_server/rb"
# backtrace_cleaner.add_silencer { |line| /puma/.match?(line) }
def add_silencer(&block)
@silencers << block
silencers << block
end

# Removes all silencers, but leaves in the filters. Useful if your
# context of debugging suddenly expands as you suspect a bug in one of
# the libraries you use.
def remove_silencers!
@silencers = []
silencers = []
end

# Removes all filters, but leaves in the silencers. Useful if you suddenly
Expand All @@ -112,7 +112,7 @@ def remove_filters!

def initialize_copy(_other)
@filters = @filters.dup
@silencers = @silencers.dup
silencers = silencers.dup
end

def add_gem_filter
Expand Down Expand Up @@ -145,7 +145,7 @@ def filter_backtrace(backtrace)
end

def silence(backtrace)
@silencers.each do |s|
silencers.each do |s|
backtrace = backtrace.reject { |line| s.call(line.to_s) }
end

Expand All @@ -154,10 +154,15 @@ def silence(backtrace)

def noise(backtrace)
backtrace.select do |line|
@silencers.any? do |s|
silencers.any? do |s|
s.call(line.to_s)
end
end
end

def silencers
require "debug"; binding.b
@silencers
end
end
end
1 change: 1 addition & 0 deletions railties/lib/minitest/rails_plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ def self.plugin_rails_init(options)
# Plugin can run without Rails loaded, check before filtering.
if ::Rails.respond_to?(:backtrace_cleaner)
Minitest.backtrace_filter = BacktraceFilterWithFallback.new(::Rails.backtrace_cleaner, Minitest.backtrace_filter)
require "debug"; binding.b
end
end

Expand Down
5 changes: 4 additions & 1 deletion railties/lib/rails/application/bootstrap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ module Bootstrap
end

initializer :configure_backtrace_cleaner, group: :all do
Rails.backtrace_cleaner.remove_silencers! if ENV["BACKTRACE"]
config.after_initialize do
Rails.backtrace_cleaner.remove_silencers! if ENV["BACKTRACE"]
require "debug"; binding.b
end
end

# Initialize cache early in the stack so railties can make use of it.
Expand Down
36 changes: 36 additions & 0 deletions railties/test/application/backtrace_cleaner_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,23 @@ def teardown
assert_not_includes last_response.body, "rails/railties/test/env_helpers.rb"
end

test "backtrace is cleaned with custom silencers" do
setup_app

app_file "config/initializers/backtrace_silencers.rb", <<-RUBY
Rails.backtrace_cleaner.add_silencer { |line| require "debug"; binding.b; line.include?("foo_controller.rb") }
RUBY

app("development")
get "/"
if RUBY_VERSION >= "3.4"
assert_not_includes last_response.body, "app/app/controllers/foo_controller.rb:4:in 'FooController#index'"
else
assert_not_includes last_response.body, "app/app/controllers/foo_controller.rb:4:in `index'"
end
assert_not_includes last_response.body, "rails/railties/test/env_helpers.rb"
end

test "backtrace is not cleaned" do
switch_env("BACKTRACE", "1") do
setup_app
Expand All @@ -42,6 +59,25 @@ def teardown
end
end

test "custom silencers are not cleaned" do
switch_env("BACKTRACE", "1") do
setup_app

app_file "config/initializers/backtrace_silencers.rb", <<-RUBY
Rails.backtrace_cleaner.add_silencer { |line| line.include?("foo_controller.rb") }
RUBY

app("development")
get "/"
if RUBY_VERSION >= "3.4"
assert_includes last_response.body, "app/app/controllers/foo_controller.rb:4:in 'FooController#index'"
else
assert_includes last_response.body, "app/app/controllers/foo_controller.rb:4:in `index'"
end
assert_includes last_response.body, "rails/railties/test/env_helpers.rb"
end
end

private
def setup_app
build_app
Expand Down

0 comments on commit 3e45a93

Please sign in to comment.