Skip to content

Commit

Permalink
Migrate from to_hash to to_h
Browse files Browse the repository at this point in the history
As @solnic pointed out in #2350 (comment)
`to_hash` has special meaning in Ruby and could be called implicitly
in contexts like double splatting argument. So we should switch to `to_h`
to avoid potential issues.
  • Loading branch information
st0012 committed Jul 28, 2024
1 parent 7db04c9 commit a559f0d
Show file tree
Hide file tree
Showing 53 changed files with 184 additions and 184 deletions.
16 changes: 8 additions & 8 deletions sentry-delayed_job/spec/sentry/delayed_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def do_nothing_with_args(a)
enqueued_job.invoke_job

expect(transport.events.count).to eq(1)
event = transport.events.last.to_hash
event = transport.events.last.to_h
expect(event[:message]).to eq("report")
expect(event[:contexts][:"Delayed-Job"][:id]).to eq(enqueued_job.id.to_s)
expect(event[:tags]).to eq({ "delayed_job.id" => enqueued_job.id.to_s, "delayed_job.queue" => nil })
Expand All @@ -66,7 +66,7 @@ def do_nothing_with_args(a)
enqueued_job.invoke_job

expect(transport.events.count).to eq(1)
event = transport.events.last.to_hash
event = transport.events.last.to_h
expect(event[:message]).to eq("tagged report")
expect(event[:tags]).to eq({ "delayed_job.id" => enqueued_job.id.to_s, "delayed_job.queue" => nil, number: 1 })

Expand All @@ -75,7 +75,7 @@ def do_nothing_with_args(a)
enqueued_job.invoke_job

expect(transport.events.count).to eq(2)
event = transport.events.last.to_hash
event = transport.events.last.to_h
expect(event[:tags]).to eq({ "delayed_job.id" => enqueued_job.id.to_s, "delayed_job.queue" => nil })
end

Expand All @@ -91,7 +91,7 @@ def do_nothing_with_args(a)
end.to raise_error(ZeroDivisionError)

expect(transport.events.count).to eq(1)
event = transport.events.last.to_hash
event = transport.events.last.to_h

expect(event[:sdk]).to eq({ name: "sentry.ruby.delayed_job", version: described_class::VERSION })
expect(event.dig(:exception, :values, 0, :type)).to eq("ZeroDivisionError")
Expand All @@ -107,7 +107,7 @@ def do_nothing_with_args(a)
end.to raise_error(RuntimeError)

expect(transport.events.count).to eq(1)
event = transport.events.last.to_hash
event = transport.events.last.to_h

expect(event[:tags]).to eq({ "delayed_job.id" => enqueued_job.id.to_s, "delayed_job.queue" => nil, number: 1 })
expect(Sentry.get_current_scope.extra).to eq({})
Expand All @@ -121,7 +121,7 @@ def do_nothing_with_args(a)
end.to raise_error(ZeroDivisionError)

expect(transport.events.count).to eq(2)
event = transport.events.last.to_hash
event = transport.events.last.to_h
expect(event[:tags]).to eq({ "delayed_job.id" => enqueued_job.id.to_s, "delayed_job.queue" => nil })
expect(Sentry.get_current_scope.extra).to eq({})
expect(Sentry.get_current_scope.tags).to eq({})
Expand Down Expand Up @@ -226,7 +226,7 @@ def perform
it "injects ActiveJob information to the event" do
expect(transport.events.count).to eq(1)

event = transport.events.last.to_hash
event = transport.events.last.to_h
expect(event[:message]).to eq("report from ActiveJob")
expect(event[:tags]).to match({ "delayed_job.id" => anything, "delayed_job.queue" => "default", number: 1 })
expect(event[:contexts][:"Active-Job"][:job_class]).to eq("ReportingJob")
Expand All @@ -253,7 +253,7 @@ def perform
it "injects ActiveJob information to the event" do
expect(transport.events.count).to eq(1)

event = transport.events.last.to_hash
event = transport.events.last.to_h
expect(event.dig(:exception, :values, 0, :type)).to eq("ZeroDivisionError")
expect(event[:tags]).to match({ "delayed_job.id" => anything, "delayed_job.queue" => "default", number: 2 })
expect(event[:contexts][:"Active-Job"][:job_class]).to eq("FailedJob")
Expand Down
10 changes: 5 additions & 5 deletions sentry-rails/spec/sentry/rails/activejob_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def perform
expect(transport.events.size).to eq(1)

event = transport.events.first
exceptions_data = event.exception.to_hash[:values]
exceptions_data = event.exception.to_h[:values]

expect(exceptions_data.count).to eq(2)
expect(exceptions_data[0][:type]).to eq("FailedJob::TestError")
Expand Down Expand Up @@ -295,7 +295,7 @@ def perform
first = transport.events[0]
check_in_id = first.check_in_id
expect(first).to be_a(Sentry::CheckInEvent)
expect(first.to_hash).to include(
expect(first.to_h).to include(
type: 'check_in',
check_in_id: check_in_id,
monitor_slug: "normaljobwithcron",
Expand All @@ -304,7 +304,7 @@ def perform

second = transport.events[1]
expect(second).to be_a(Sentry::CheckInEvent)
expect(second.to_hash).to include(
expect(second.to_h).to include(
:duration,
type: 'check_in',
check_in_id: check_in_id,
Expand All @@ -323,7 +323,7 @@ def perform
first = transport.events[0]
check_in_id = first.check_in_id
expect(first).to be_a(Sentry::CheckInEvent)
expect(first.to_hash).to include(
expect(first.to_h).to include(
type: 'check_in',
check_in_id: check_in_id,
monitor_slug: "failed_job",
Expand All @@ -333,7 +333,7 @@ def perform

second = transport.events[1]
expect(second).to be_a(Sentry::CheckInEvent)
expect(second.to_hash).to include(
expect(second.to_h).to include(
:duration,
type: 'check_in',
check_in_id: check_in_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@

expect(transport.events.count).to eq(1)

transaction = transport.events.last.to_hash
transaction = transport.events.last.to_h
breadcrumbs = transaction[:breadcrumbs][:values]
process_action_crumb = breadcrumbs.last
expect(process_action_crumb[:category]).to eq("process_action.action_controller")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@

expect(transport.events.count).to eq(1)

transaction = transport.events.last.to_hash
transaction = transport.events.last.to_h
breadcrumbs = transaction[:breadcrumbs][:values]
process_action_crumb = breadcrumbs.last
expect(process_action_crumb[:category]).to eq("process_action.action_controller")
Expand Down
6 changes: 3 additions & 3 deletions sentry-rails/spec/sentry/rails/controller_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def request
event = transport.events.last
expect(event.message).to eq("foo")
expect(event.tags).to eq({ new_tag: true })
expect(event.to_hash.dig(:request, :url)).to eq("http://example.org/test")
expect(event.to_h.dig(:request, :url)).to eq("http://example.org/test")
end
end

Expand All @@ -47,8 +47,8 @@ def request

event = transport.events.last
expect(event.tags).to eq({ new_tag: true })
expect(event.to_hash.dig(:exception, :values, 0, :type)).to eq("ZeroDivisionError")
expect(event.to_hash.dig(:request, :url)).to eq("http://example.org/test")
expect(event.to_h.dig(:exception, :values, 0, :type)).to eq("ZeroDivisionError")
expect(event.to_h.dig(:request, :url)).to eq("http://example.org/test")
end
end
end
4 changes: 2 additions & 2 deletions sentry-rails/spec/sentry/rails/event_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
end

it "sets right SDK information" do
event_hash = Sentry::Rails.capture_message("foo").to_hash
event_hash = Sentry::Rails.capture_message("foo").to_h

expect(event_hash[:sdk]).to eq(name: "sentry.ruby.rails", version: Sentry::Rails::VERSION)
end
Expand All @@ -25,7 +25,7 @@
e
end

let(:hash) { Sentry::Rails.capture_exception(exception).to_hash }
let(:hash) { Sentry::Rails.capture_exception(exception).to_h }

it 'marks in_app correctly' do
frames = hash[:exception][:values][0][:stacktrace][:frames]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

expect(transport.events.count).to eq(1)

transaction = transport.events.first.to_hash
transaction = transport.events.first.to_h
expect(transaction[:type]).to eq("transaction")
expect(transaction[:spans].count).to eq(2)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

expect(transport.events.count).to eq(1)

transaction = transport.events.first.to_hash
transaction = transport.events.first.to_h
expect(transaction[:type]).to eq("transaction")
expect(transaction[:spans].count).to eq(2)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

expect(transport.events.count).to eq(1)

transaction = transport.events.first.to_hash
transaction = transport.events.first.to_h
expect(transaction[:type]).to eq("transaction")
expect(transaction[:spans].count).to eq(1)

Expand Down Expand Up @@ -65,7 +65,7 @@ def foo
it "doesn't record query's source location" do
expect(transport.events.count).to eq(1)

transaction = transport.events.first.to_hash
transaction = transport.events.first.to_h
expect(transaction[:type]).to eq("transaction")
expect(transaction[:spans].count).to eq(1)

Expand All @@ -84,7 +84,7 @@ def foo
it "records query's source location" do
expect(transport.events.count).to eq(1)

transaction = transport.events.first.to_hash
transaction = transport.events.first.to_h
expect(transaction[:type]).to eq("transaction")
expect(transaction[:spans].count).to eq(1)

Expand All @@ -102,7 +102,7 @@ def foo
it "doesn't record query's source location" do
expect(transport.events.count).to eq(1)

transaction = transport.events.first.to_hash
transaction = transport.events.first.to_h
expect(transaction[:type]).to eq("transaction")
expect(transaction[:spans].count).to eq(1)

Expand All @@ -129,7 +129,7 @@ def foo

expect(transport.events.count).to eq(1)

transaction = transport.events.first.to_hash
transaction = transport.events.first.to_h
expect(transaction[:type]).to eq("transaction")
expect(transaction[:spans].count).to eq(2)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
expect(response).to have_http_status(:ok)
expect(transport.events.count).to eq(2)

analysis_transaction = transport.events.first.to_hash
analysis_transaction = transport.events.first.to_h
expect(analysis_transaction[:type]).to eq("transaction")

if Rails.version.to_f > 6.1
Expand All @@ -38,7 +38,7 @@
expect(analysis_transaction[:spans][0][:origin]).to eq("auto.file.rails")
end

request_transaction = transport.events.last.to_hash
request_transaction = transport.events.last.to_h
expect(request_transaction[:type]).to eq("transaction")
expect(request_transaction[:spans].count).to eq(2)

Expand Down
8 changes: 4 additions & 4 deletions sentry-rails/spec/sentry/rails/tracing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
expect(response).to have_http_status(:internal_server_error)
expect(transport.events.count).to eq(2)

event = transport.events.first.to_hash
transaction = transport.events.last.to_hash
event = transport.events.first.to_h
transaction = transport.events.last.to_h

expect(event.dig(:contexts, :trace, :trace_id).length).to eq(32)
expect(event.dig(:contexts, :trace, :trace_id)).to eq(transaction.dig(:contexts, :trace, :trace_id))
Expand Down Expand Up @@ -62,7 +62,7 @@
expect(response).to have_http_status(:ok)
expect(transport.events.count).to eq(1)

transaction = transport.events.last.to_hash
transaction = transport.events.last.to_h

expect(transaction[:type]).to eq("transaction")
expect(transaction.dig(:contexts, :trace, :op)).to eq("http.server")
Expand Down Expand Up @@ -206,7 +206,7 @@

expect(transport.events.count).to eq(3)

transaction = transport.events.last.to_hash
transaction = transport.events.last.to_h

expect(transaction[:type]).to eq("transaction")
expect(transaction[:transaction]).to eq("PostsController#show")
Expand Down
22 changes: 11 additions & 11 deletions sentry-resque/spec/sentry/resque_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def self.perform(msg)
process_job(worker)

expect(transport.events.count).to eq(1)
event = transport.events.last.to_hash
event = transport.events.last.to_h
expect(event[:message]).to eq("report")
expect(event[:tags]).to eq({ "resque.queue" => "default" })
expect(event[:contexts][:"Resque"]).to include({ job_class: "MessageJob", arguments: ["report"], queue: "default" })
Expand All @@ -86,7 +86,7 @@ def self.perform(msg)
process_job(worker)

expect(transport.events.count).to eq(1)
event = transport.events.last.to_hash
event = transport.events.last.to_h
expect(event[:message]).to eq("tagged report")
expect(event[:tags]).to include({ number: 1 })

Expand All @@ -95,7 +95,7 @@ def self.perform(msg)
process_job(worker)

expect(transport.events.count).to eq(2)
event = transport.events.last.to_hash
event = transport.events.last.to_h
expect(event[:tags]).to eq({ "resque.queue" => "default" })
end

Expand All @@ -107,7 +107,7 @@ def self.perform(msg)
end.to change { Resque::Stat.get("failed") }.by(1)

expect(transport.events.count).to eq(1)
event = transport.events.last.to_hash
event = transport.events.last.to_h

expect(event[:sdk]).to eq({ name: "sentry.ruby.resque", version: described_class::VERSION })
expect(event.dig(:exception, :values, 0, :type)).to eq("ZeroDivisionError")
Expand All @@ -121,7 +121,7 @@ def self.perform(msg)
end.to change { Resque::Stat.get("failed") }.by(1)

expect(transport.events.count).to eq(1)
event = transport.events.last.to_hash
event = transport.events.last.to_h

expect(event[:tags]).to eq({ "resque.queue" => "default", number: 1 })
expect(Sentry.get_current_scope.extra).to eq({})
Expand All @@ -133,7 +133,7 @@ def self.perform(msg)
end.to change { Resque::Stat.get("failed") }.by(1)

expect(transport.events.count).to eq(2)
event = transport.events.last.to_hash
event = transport.events.last.to_h
expect(event[:tags]).to eq({ "resque.queue" => "default" })
expect(Sentry.get_current_scope.extra).to eq({})
expect(Sentry.get_current_scope.tags).to eq({})
Expand All @@ -159,7 +159,7 @@ def self.perform(msg)
end
end.to change { transport.events.count }.by(1)

event = transport.events.last.to_hash
event = transport.events.last.to_h

expect(event[:sdk]).to eq({ name: "sentry.ruby.resque", version: described_class::VERSION })
expect(event.dig(:exception, :values, 0, :type)).to eq("ZeroDivisionError")
Expand All @@ -173,7 +173,7 @@ def self.perform(msg)
end.to change { Resque::Stat.get("failed") }.by(1)
.and change { transport.events.count }.by(1)

event = transport.events.last.to_hash
event = transport.events.last.to_h

expect(event[:sdk]).to eq({ name: "sentry.ruby.resque", version: described_class::VERSION })
expect(event.dig(:exception, :values, 0, :type)).to eq("ZeroDivisionError")
Expand All @@ -199,7 +199,7 @@ def self.perform(msg)
end
end.to change { transport.events.count }.by(3)

event = transport.events.last.to_hash
event = transport.events.last.to_h

expect(event[:sdk]).to eq({ name: "sentry.ruby.resque", version: described_class::VERSION })
expect(event.dig(:exception, :values, 0, :type)).to eq("ZeroDivisionError")
Expand Down Expand Up @@ -264,7 +264,7 @@ def perform
it "injects ActiveJob information to the event" do
expect(transport.events.count).to eq(1)

event = transport.events.last.to_hash
event = transport.events.last.to_h
expect(event[:message]).to eq("report from ActiveJob")
expect(event[:tags]).to match({ "resque.queue" => "default", number: 1 })
expect(event[:contexts][:"Active-Job"][:job_class]).to eq("AJMessageJob")
Expand All @@ -289,7 +289,7 @@ def perform
it "injects ActiveJob information to the event" do
expect(transport.events.count).to eq(1)

event = transport.events.last.to_hash
event = transport.events.last.to_h
expect(event.dig(:exception, :values, 0, :type)).to eq("ZeroDivisionError")
expect(event[:tags]).to match({ "resque.queue" => "default", number: 2 })
expect(event[:contexts][:"Active-Job"][:job_class]).to eq("AJFailedJob")
Expand Down
Loading

0 comments on commit a559f0d

Please sign in to comment.