diff --git a/lib/raven/cli.rb b/lib/raven/cli.rb index 7ccd45c28..585c0e511 100644 --- a/lib/raven/cli.rb +++ b/lib/raven/cli.rb @@ -18,7 +18,7 @@ def self.test(dsn = nil, silent = false, config = nil) # rubocop:disable all # wipe out env settings to ensure we send the event unless config.capture_allowed? - env_name = config.environments.pop || 'production' + env_name = config.environments.last || 'production' config.current_environment = env_name end @@ -33,27 +33,16 @@ def self.test(dsn = nil, silent = false, config = nil) # rubocop:disable all evt = instance.capture_exception(e) end - if evt && !(evt.is_a? Thread) - if evt.is_a? Hash - instance.logger.debug "-> event ID: #{evt[:event_id]}" - else - instance.logger.debug "-> event ID: #{evt.id}" - end - elsif evt # async configuration - if evt.value.is_a? Hash - instance.logger.debug "-> event ID: #{evt.value[:event_id]}" - else - instance.logger.debug "-> event ID: #{evt.value.id}" - end + if evt + instance.logger.debug "-> event ID: #{evt.id}" + instance.logger.debug "" + instance.logger.debug "Done!" + evt else instance.logger.debug "" instance.logger.debug "An error occurred while attempting to send the event." - exit 1 + false end - - instance.logger.debug "" - instance.logger.debug "Done!" - evt end end end diff --git a/spec/raven/cli_spec.rb b/spec/raven/cli_spec.rb index 0bbb7aa20..9a8452e36 100644 --- a/spec/raven/cli_spec.rb +++ b/spec/raven/cli_spec.rb @@ -1,12 +1,69 @@ require 'spec_helper' require 'raven/cli' -RSpec.describe "CLI tests" do - it "posting an exception" do - event = Raven::CLI.test(Raven.configuration.server, true, Raven.configuration) - expect(event).to be_a(Raven::Event) - hash = event.to_hash - expect(hash[:exception][:values][0][:type]).to eq("ZeroDivisionError") - expect(hash[:exception][:values][0][:value]).to eq("divided by 0") +RSpec.describe Raven::CLI do + # avoid unexpectedly mutating the shared configuration object + let(:config) { Raven.configuration.dup } + + context "when there's no error" do + it "sends an event" do + event = described_class.test(config.server, true, config) + + expect(event).to be_a(Raven::Event) + hash = event.to_hash + expect(hash[:exception][:values][0][:type]).to eq("ZeroDivisionError") + expect(hash[:exception][:values][0][:value]).to eq("divided by 0") + end + + it "logs correct values" do + logger = spy + allow_any_instance_of(Raven::Instance).to receive(:logger).and_return(logger) + + event = described_class.test(config.server, true, config) + + expect(logger).to have_received(:debug).with("Sending a test event:") + expect(logger).to have_received(:debug).with("-> event ID: #{event.id}") + expect(logger).to have_received(:debug).with("Done!") + end + end + + context "when there's an error" do + before do + # make Configuration#sample_allowed? fail + config.sample_rate = 2.0 + allow(Random::DEFAULT).to receive(:rand).and_return(3.0) + end + + it "returns false" do + event = described_class.test(config.server, true, config) + + expect(event).to eq(false) + end + + it "logs correct values" do + logger = spy + allow_any_instance_of(Raven::Instance).to receive(:logger).and_return(logger) + + described_class.test(config.server, true, config) + + expect(logger).to have_received(:debug).with("Sending a test event:") + expect(logger).to have_received(:debug).with("An error occurred while attempting to send the event.") + expect(logger).not_to have_received(:debug).with("Done!") + end + end + + context "when with custom environments config" do + let(:config) { Raven.configuration.dup } + + before do + config.environments = %w(production test) + end + + it "still sends the test event" do + event = Raven::CLI.test(config.server, true, config) + + expect(event).to be_a(Raven::Event) + expect(config.errors).to be_empty + end end end