-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathinstance_spec.cr
255 lines (219 loc) · 7.97 KB
/
instance_spec.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
require "../spec_helper"
module Raven::Test
class BaseException < ::Exception; end
class SubException < BaseException; end
end
private class InstanceTest < Raven::Instance
getter last_sent_event : Raven::Event?
def send_event(event, hint = nil)
super.tap do
@last_sent_event = event
end
end
end
private def with_instance(context = nil, &)
yield InstanceTest.new(context, build_configuration)
end
describe Raven::Instance do
describe "#context" do
it "is Raven.context by default" do
with_instance do |instance|
instance.context.should be(Raven.context)
end
end
context "initialized with a context" do
it "is not Raven.context" do
with_instance(Raven::Context.new) do |instance|
instance.context.should_not be(Raven.context)
end
end
end
end
describe "#capture" do
it "returns the generated event" do
with_instance do |instance|
returned = instance.capture("Test message", foo: "bar")
returned.should be_a(Raven::Event)
returned.as?(Raven::Event).try(&.message).should eq("Test message")
end
end
it "yields the event to a passed block" do
with_instance do |instance|
instance.capture("Test message", id: "foo", logger: "bar") do |event|
event.message.should eq("Test message")
event.id.should eq("foo")
event.logger.should eq("bar")
end
end
end
{% for key in %i(user extra tags) %}
context "with {{key.id}} context specified" do
it "merges context hierarchy" do
with_instance do |instance|
Raven::Context.clear!
Raven.{{key.id}}_context(foo: :foo, bar: :bar)
instance.capture("Test message", {{key.id}}: {bar: "baz"}) do |event|
event.{{key.id}}.should eq({:foo => :foo, :bar => "baz"})
end
end
end
it "use passed values only within the block" do
with_instance do |instance|
Raven::Context.clear!
Raven.{{key.id}}_context(will: :stay_there)
ctx = Raven.{{key.id}}_context(foo: :foo, bar: :bar) do
instance.capture("Test message", {{key.id}}: {bar: "baz"}) do |event|
event.{{key.id}}.should eq({
:will => :stay_there,
:foo => :foo,
:bar => "baz"
})
end
end
ctx.should eq({:will => :stay_there})
Raven.{{key.id}}_context.should be(ctx)
end
end
end
{% end %}
context "with String" do
it "sends the result of Event.from" do
with_instance do |instance|
instance.capture("Test message", id: "foo", logger: "bar")
instance.last_sent_event.try(&.message).should eq("Test message")
instance.last_sent_event.try(&.id).should eq("foo")
instance.last_sent_event.try(&.logger).should eq("bar")
end
end
end
context "with Exception" do
it "sends the result of Event.from" do
with_instance do |instance|
instance.capture(build_exception, id: "foo", logger: "bar")
instance.last_sent_event.try(&.id).should eq("foo")
instance.last_sent_event.try(&.logger).should eq("bar")
end
end
it "ignores Raven::Error" do
with_instance do |instance|
instance.capture(Raven::Error.new).should be_false
instance.last_sent_event.should be_nil
end
end
context "for an excluded exception type" do
context "defined by string type" do
it "returns false for a class match" do
with_instance do |instance|
instance.configuration.excluded_exceptions << "Raven::Test::BaseException"
instance.capture(Raven::Test::BaseException.new).should be_false
end
end
it "returns Raven::Event for an undefined exception class" do
with_instance do |instance|
instance.configuration.excluded_exceptions << "Raven::Test::NonExistentException"
instance.capture(Raven::Test::BaseException.new).should be_a(Raven::Event)
end
end
end
context "defined by class type" do
it "returns false for a class match" do
with_instance do |instance|
instance.configuration.excluded_exceptions << Raven::Test::BaseException
instance.capture(Raven::Test::BaseException.new).should be_false
end
end
it "returns false for a sub class match" do
with_instance do |instance|
instance.configuration.excluded_exceptions << Raven::Test::BaseException
instance.capture(Raven::Test::SubException.new).should be_false
end
end
end
end
end
context "when async" do
it "sends the result of Event.from" do
with_instance do |instance|
async_event = nil
instance.configuration.async = ->(e : Raven::Event) { async_event = e }
instance.capture("Test message", foo: "bar")
instance.last_sent_event.should be_nil
async_event.try(&.message).should eq("Test message")
end
end
end
context "when async raises an exception" do
it "sends the result of Event.capture via fallback" do
with_instance do |instance|
instance.configuration.async = ->(_e : Raven::Event) { raise ArgumentError.new }
instance.capture("Test message", id: "foo", logger: "bar")
instance.last_sent_event.try(&.message).should eq("Test message")
instance.last_sent_event.try(&.id).should eq("foo")
instance.last_sent_event.try(&.logger).should eq("bar")
end
end
end
context "with should_capture callback" do
it "sends the result of Event.capture according to the result of should_capture" do
with_instance do |instance|
instance.configuration.should_capture = ->(_obj : Exception | String) { false }
instance.capture(build_exception).should be_false
instance.last_sent_event.should be_nil
end
end
end
end
describe "#report_status" do
not_ready_message = "Raven #{Raven::VERSION} configured not to capture errors"
ready_message = "Raven #{Raven::VERSION} ready to catch errors"
it "logs a ready message when configured" do
with_instance do |instance|
instance.configuration.silence_ready = false
Log.capture do |logs|
instance.report_status
logs.check(:info, ready_message)
end
end
end
it "logs nothing if 'silence_ready' option is true" do
with_instance do |instance|
instance.configuration.silence_ready = true
Log.capture do |logs|
instance.report_status
logs.empty
end
end
end
it "logs not ready message when not configured" do
with_instance do |instance|
instance.configuration.silence_ready = false
instance.configuration.dsn = "dummy://foo"
Log.capture do |logs|
instance.report_status
logs.check(:info, /#{not_ready_message}/)
end
end
end
it "logs not ready message if the config does not send in current environment" do
with_instance do |instance|
instance.configuration.silence_ready = false
instance.configuration.environments = %w(production)
Log.capture do |logs|
instance.report_status
logs.check(:info,
"#{not_ready_message}: Not configured to send/capture in environment 'default'"
)
end
end
end
end
describe ".last_event_id" do
it "sends the result of Event.capture" do
with_instance do |instance|
event = instance.capture("Test message")
last_sent_event = instance.last_sent_event.should_not be_nil
last_sent_event.id.should eq(event.as(Raven::Event).id)
end
end
end
end