Skip to content

Commit 40c16d3

Browse files
authored
Migrate error_test to RSpec (#1456)
1 parent da95afc commit 40c16d3

File tree

3 files changed

+92
-86
lines changed

3 files changed

+92
-86
lines changed

lib/ddtrace/error.rb

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ def self.build_from(value)
1616

1717
def initialize(type = nil, message = nil, backtrace = nil)
1818
backtrace = Array(backtrace).join("\n")
19+
20+
# DEV: We should measure if `Utils.utf8_encode` is still needed in practice.
1921
@type = Utils.utf8_encode(type)
2022
@message = Utils.utf8_encode(message)
2123
@backtrace = Utils.utf8_encode(backtrace)

spec/ddtrace/error_spec.rb

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
RSpec.describe Datadog::Error do
2+
context 'with default values' do
3+
let(:error) { described_class.new }
4+
5+
it do
6+
expect(error.type).to be_empty
7+
expect(error.message).to be_empty
8+
expect(error.backtrace).to be_empty
9+
end
10+
11+
# Empty strings were being interpreted as ASCII strings breaking `msgpack`
12+
# decoding on the agent-side.
13+
it 'encodes default values in UTF-8' do
14+
error = described_class.new
15+
16+
expect(error.type.encoding).to eq(::Encoding::UTF_8)
17+
expect(error.message.encoding).to eq(::Encoding::UTF_8)
18+
expect(error.backtrace.encoding).to eq(::Encoding::UTF_8)
19+
end
20+
end
21+
22+
context 'with all values provided' do
23+
let(:error) { described_class.new('ErrorClass', 'message', %w[line1 line2 line3]) }
24+
25+
it do
26+
expect(error.type).to eq('ErrorClass')
27+
expect(error.message).to eq('message')
28+
expect(error.backtrace).to eq("line1\nline2\nline3")
29+
end
30+
end
31+
32+
describe '.build_from' do
33+
subject(:error) { described_class.build_from(value) }
34+
35+
context 'with an exception' do
36+
let(:value) { ZeroDivisionError.new('divided by 0') }
37+
38+
it do
39+
expect(error.type).to eq('ZeroDivisionError')
40+
expect(error.message).to eq('divided by 0')
41+
expect(error.backtrace).to be_empty
42+
end
43+
end
44+
45+
context 'with an array' do
46+
let(:value) { ['ZeroDivisionError', 'divided by 0'] }
47+
48+
it do
49+
expect(error.type).to eq('ZeroDivisionError')
50+
expect(error.message).to eq('divided by 0')
51+
expect(error.backtrace).to be_empty
52+
end
53+
end
54+
55+
context 'with a custom object responding to :message' do
56+
let(:value) do
57+
# RSpec 'double' hijacks the #class method, thus not allowing us
58+
# to organically test the `Error#type` inferred for this object.
59+
clazz = stub_const('Test::CustomMessage', Struct.new(:message))
60+
clazz.new('custom msg')
61+
end
62+
63+
it do
64+
expect(error.type).to eq('Test::CustomMessage')
65+
expect(error.message).to eq('custom msg')
66+
expect(error.backtrace).to be_empty
67+
end
68+
end
69+
70+
context 'with nil' do
71+
let(:value) { nil }
72+
73+
it do
74+
expect(error.type).to be_empty
75+
expect(error.message).to be_empty
76+
expect(error.backtrace).to be_empty
77+
end
78+
end
79+
80+
context 'with a utf8 incompatible message' do
81+
let(:value) { StandardError.new("\xC2".force_encoding(::Encoding::ASCII_8BIT)) }
82+
83+
it 'discards unencodable value' do
84+
expect(error.type).to eq('StandardError')
85+
expect(error.message).to be_empty
86+
expect(error.backtrace).to be_empty
87+
end
88+
end
89+
end
90+
end

test/error_test.rb

-86
This file was deleted.

0 commit comments

Comments
 (0)