From 473b2ed5efcfa0fc89d60b5bd6746ac11b52d192 Mon Sep 17 00:00:00 2001 From: Alex Moinet Date: Wed, 24 Jan 2018 15:29:11 +0000 Subject: [PATCH 1/5] Added configuration option to disable magic config & tests --- lib/bugsnag.rb | 32 ++++++++++++++++----- spec/bugsnag_spec.rb | 67 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 7 deletions(-) diff --git a/lib/bugsnag.rb b/lib/bugsnag.rb index ba1dcced5..41f1a45ef 100644 --- a/lib/bugsnag.rb +++ b/lib/bugsnag.rb @@ -28,6 +28,7 @@ module Bugsnag LOCK = Mutex.new + INTEGRATIONS = [:resque, :sidekiq, :mailman, :delayed_job, :shoryuken, :que] class << self # Configure the Bugsnag notifier application-wide settings. @@ -140,13 +141,30 @@ def start_session def before_notify_callbacks Bugsnag.configuration.request_data[:before_callbacks] ||= [] end - end -end -require "bugsnag/integrations/railtie" if defined?(Rails::Railtie) -[:resque, :sidekiq, :mailman, :delayed_job, :shoryuken, :que].each do |integration| - begin - require "bugsnag/integrations/#{integration}" - rescue LoadError + # Attempts to load all integrations through auto-discovery + def load_integrations + require "bugsnag/integrations/railtie" if defined?(Rails::Railtie) + INTEGRATIONS.each do |integration| + begin + require "bugsnag/integrations/#{integration}" + rescue LoadError + end + end + end + + # Load a specific integration + def load_integration(integration) + integration = :railtie if integration == :rails + if INTEGRATIONS.include?(integration) || integration == :railtie + require "bugsnag/integrations/#{integration}" + else + configuration.debug("Integration #{integration} is not currently supported") + end + end end end + +if !ENV["BUGSNAG_DISABLE_AUTOCONFIGURE"] + Bugsnag.load_integrations +end \ No newline at end of file diff --git a/spec/bugsnag_spec.rb b/spec/bugsnag_spec.rb index df4aadb69..97190a500 100644 --- a/spec/bugsnag_spec.rb +++ b/spec/bugsnag_spec.rb @@ -16,4 +16,71 @@ expect(Bugsnag.configuration.logger).to have_received(:warn) end end + + describe 'loading integrations' do + before do + module Kernel + REQUIRED = [] + alias_method :old_require, :require + def require(path) + if path.include?("bugsnag/integrations/") + REQUIRED << path + else + old_require(path) + end + end + end + end + + it 'attempts to load integrations' do + Kernel::REQUIRED = [] + ENV["BUGSNAG_DISABLE_AUTOCONFIGURE"] = nil + load "./lib/bugsnag.rb" + Bugsnag::INTEGRATIONS.each do |integration| + expect(Kernel::REQUIRED).to include("bugsnag/integrations/#{integration}") + end + end + + it 'does not load integrations when BUGSNAG_DISABLE_AUTOCONFIGURE is true' do + Kernel::REQUIRED = [] + ENV["BUGSNAG_DISABLE_AUTOCONFIGURE"] = 'true' + load "./lib/bugsnag.rb" + expect(Kernel::REQUIRED).to eq(['bugsnag/integrations/rack']) + end + + it 'loads all integrations if requested' do + Kernel::REQUIRED = [] + expect(Kernel::REQUIRED).to eq([]) + Bugsnag.load_integrations + Bugsnag::INTEGRATIONS.each do |integration| + expect(Kernel::REQUIRED).to include("bugsnag/integrations/#{integration}") + end + end + + it 'loads singular integrations' do + Kernel::REQUIRED = [] + expect(Kernel::REQUIRED).to eq([]) + Bugsnag::INTEGRATIONS.each do |integration| + Kernel::REQUIRED = [] + Bugsnag.load_integration(integration) + expect(Kernel::REQUIRED).to include("bugsnag/integrations/#{integration}") + end + end + + it 'loads railtie for :rails or :railtie' do + Kernel::REQUIRED = [] + Bugsnag.load_integration(:rails) + expect(Kernel::REQUIRED).to include("bugsnag/integrations/railtie") + Kernel::REQUIRED = [] + Bugsnag.load_integration(:railtie) + expect(Kernel::REQUIRED).to include("bugsnag/integrations/railtie") + end + + after do + module Kernel + alias_method :require, :old_require + end + Kernel.send(:remove_const, :REQUIRED) + end + end end From 926b0507d8c9300061da45b429765cf7a286a9dc Mon Sep 17 00:00:00 2001 From: Alex Moinet Date: Mon, 29 Jan 2018 17:00:58 +0000 Subject: [PATCH 2/5] Added rack to the non-autoconfigured --- lib/bugsnag.rb | 4 +--- spec/bugsnag_spec.rb | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/bugsnag.rb b/lib/bugsnag.rb index 41f1a45ef..30608cf77 100644 --- a/lib/bugsnag.rb +++ b/lib/bugsnag.rb @@ -13,8 +13,6 @@ require "bugsnag/delivery/synchronous" require "bugsnag/delivery/thread_queue" -require "bugsnag/integrations/rack" - require "bugsnag/middleware/rack_request" require "bugsnag/middleware/warden_user" require "bugsnag/middleware/clearance_user" @@ -28,7 +26,7 @@ module Bugsnag LOCK = Mutex.new - INTEGRATIONS = [:resque, :sidekiq, :mailman, :delayed_job, :shoryuken, :que] + INTEGRATIONS = [:rack, :resque, :sidekiq, :mailman, :delayed_job, :shoryuken, :que] class << self # Configure the Bugsnag notifier application-wide settings. diff --git a/spec/bugsnag_spec.rb b/spec/bugsnag_spec.rb index 97190a500..839965d60 100644 --- a/spec/bugsnag_spec.rb +++ b/spec/bugsnag_spec.rb @@ -45,7 +45,7 @@ def require(path) Kernel::REQUIRED = [] ENV["BUGSNAG_DISABLE_AUTOCONFIGURE"] = 'true' load "./lib/bugsnag.rb" - expect(Kernel::REQUIRED).to eq(['bugsnag/integrations/rack']) + expect(Kernel::REQUIRED).to eq([]) end it 'loads all integrations if requested' do From d2d64f5d3acb66bf46ff06a2aa3921c15cb92c64 Mon Sep 17 00:00:00 2001 From: Alex Moinet Date: Mon, 29 Jan 2018 17:22:33 +0000 Subject: [PATCH 3/5] Separated rack from other integrations with comment explaining why --- lib/bugsnag.rb | 6 +++++- spec/bugsnag_spec.rb | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/bugsnag.rb b/lib/bugsnag.rb index 30608cf77..902665314 100644 --- a/lib/bugsnag.rb +++ b/lib/bugsnag.rb @@ -13,6 +13,10 @@ require "bugsnag/delivery/synchronous" require "bugsnag/delivery/thread_queue" +# Rack is not bundled with the other integrations +# as it doesn't auto-configure when loaded +require "bugsnag/integrations/rack" + require "bugsnag/middleware/rack_request" require "bugsnag/middleware/warden_user" require "bugsnag/middleware/clearance_user" @@ -26,7 +30,7 @@ module Bugsnag LOCK = Mutex.new - INTEGRATIONS = [:rack, :resque, :sidekiq, :mailman, :delayed_job, :shoryuken, :que] + INTEGRATIONS = [:resque, :sidekiq, :mailman, :delayed_job, :shoryuken, :que] class << self # Configure the Bugsnag notifier application-wide settings. diff --git a/spec/bugsnag_spec.rb b/spec/bugsnag_spec.rb index 839965d60..25b41f18d 100644 --- a/spec/bugsnag_spec.rb +++ b/spec/bugsnag_spec.rb @@ -45,7 +45,7 @@ def require(path) Kernel::REQUIRED = [] ENV["BUGSNAG_DISABLE_AUTOCONFIGURE"] = 'true' load "./lib/bugsnag.rb" - expect(Kernel::REQUIRED).to eq([]) + expect(Kernel::REQUIRED).to eq(["bugsnag/integrations/rack"]) end it 'loads all integrations if requested' do From c35ed76d7a9e1633f550b6f537a1e50ae63c7b6a Mon Sep 17 00:00:00 2001 From: Alex Moinet Date: Mon, 19 Feb 2018 13:43:35 +0000 Subject: [PATCH 4/5] Rubocop fixes --- lib/bugsnag.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/bugsnag.rb b/lib/bugsnag.rb index b53f8a081..52baa5879 100644 --- a/lib/bugsnag.rb +++ b/lib/bugsnag.rb @@ -182,6 +182,4 @@ def load_integration(integration) end end -if !ENV["BUGSNAG_DISABLE_AUTOCONFIGURE"] - Bugsnag.load_integrations -end \ No newline at end of file +Bugsnag.load_integrations unless ENV["BUGSNAG_DISABLE_AUTOCONFIGURE"] \ No newline at end of file From 99826b368b62a69ac6f4cc54553f233caab92799 Mon Sep 17 00:00:00 2001 From: Alex Moinet Date: Mon, 19 Feb 2018 14:18:43 +0000 Subject: [PATCH 5/5] Rubocop part 2 --- lib/bugsnag.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/bugsnag.rb b/lib/bugsnag.rb index 52baa5879..cec483065 100644 --- a/lib/bugsnag.rb +++ b/lib/bugsnag.rb @@ -182,4 +182,4 @@ def load_integration(integration) end end -Bugsnag.load_integrations unless ENV["BUGSNAG_DISABLE_AUTOCONFIGURE"] \ No newline at end of file +Bugsnag.load_integrations unless ENV["BUGSNAG_DISABLE_AUTOCONFIGURE"]