Skip to content

Commit ee7be1d

Browse files
p-datadogp
andauthored
DI: eliminate Ruby warnings in test suite (#4950)
Co-authored-by: Oleg Pudeyev <[email protected]>
1 parent cc8344d commit ee7be1d

11 files changed

+97
-38
lines changed

spec/datadog/di/hook_line_load.rb

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,37 @@
1-
# Comment line - not executable
1+
# This file is loaded repeatedly in the test suite due to the way DI performs
2+
# code tracking. Remove the constants if they have already been defined
3+
# to avoid Ruby warnings.
4+
begin
5+
Object.send(:remove_const, :HookLineLoadTestClass)
6+
Object.send(:remove_const, :HookLineIvarLoadTestClass)
7+
rescue NameError
8+
end
9+
10+
# padding
11+
# padding
12+
# padding
13+
# padding
14+
# padding
15+
# padding
16+
# padding
17+
# padding
18+
# padding
19+
# padding
20+
21+
# Comment line - not executable - line 21
222

323
class HookLineLoadTestClass
4-
def test_method # Line 4
5-
42 # Line 5
6-
end # Line 6
24+
def test_method # Line 24
25+
42 # Line 25
26+
end # Line 26
727

828
def test_method_with_local
929
local = 42 # standard:disable Style/RedundantAssignment
10-
local # Line 10 # standard:disable Style/RedundantAssignment
30+
local # Line 30 # standard:disable Style/RedundantAssignment
1131
end
1232

1333
def test_method_with_arg(arg)
14-
arg # Line 14
34+
arg # Line 34
1535
end
1636
end
1737

@@ -24,16 +44,16 @@ def initialize
2444
end
2545

2646
def test_method
27-
1337 # Line 24
47+
1337 # Line 47
2848
end
2949

3050
def test_exception
3151
local = 42
32-
raise TestException, 'Intentional exception' # Line 32
33-
local # Line 33 # standard:disable Lint/UnreachableCode
52+
raise TestException, 'Intentional exception' # Line 52
53+
local # Line 53 # standard:disable Lint/UnreachableCode
3454
end
3555
end
3656

37-
unless (actual = File.read(__FILE__).count("\n")) == 39
38-
raise "Wrong number of lines in hook_line_load.rb: actual #{actual}, expected 39"
57+
unless (actual = File.read(__FILE__).count("\n")) == 59
58+
raise "Wrong number of lines in hook_line_load.rb: actual #{actual}, expected 59"
3959
end
Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
1+
# This file is loaded repeatedly in the test suite due to the way DI performs
2+
# code tracking. Remove the constants if they have already been defined
3+
# to avoid Ruby warnings.
4+
begin
5+
Object.send(:remove_const, :HookLineRecursiveTestClass)
6+
rescue NameError
7+
end
8+
9+
# padding
10+
111
class HookLineRecursiveTestClass
212
def recursive(depth)
3-
if depth > 0 # Line 3
13+
if depth > 0 # Line 13
414
recursive(depth - 1) + '-'
515
else
616
'+'
717
end
818
end
919

1020
def infinitely_recursive(depth = 0)
11-
infinitely_recursive(depth + 1) # Line 11
21+
infinitely_recursive(depth + 1) # Line 21
1222
end
1323
end
1424

15-
unless (actual = File.read(__FILE__).count("\n")) == 17
25+
unless (actual = File.read(__FILE__).count("\n")) == 27
1626
raise "Wrong number of lines in hook_line_recursive.rb: actual #{actual}, expected 17"
1727
end
Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1+
# This file is loaded repeatedly in the test suite due to the way DI performs
2+
# code tracking. Remove the constants if they have already been defined
3+
# to avoid Ruby warnings.
4+
begin
5+
Object.send(:remove_const, :HookLineTargetedTestClass)
6+
rescue NameError
7+
end
8+
9+
# padding
10+
111
class HookLineTargetedTestClass
2-
def test_method # Line 2
3-
42 # Line 3
12+
def test_method # Line 12
13+
42 # Line 13
414
end
515
end
616

7-
unless (actual = File.read(__FILE__).count("\n")) == 9
17+
unless (actual = File.read(__FILE__).count("\n")) == 19
818
raise "Wrong number of lines in hook_line_targeted.rb: actual #{actual}, expected 9"
919
end

spec/datadog/di/hook_method.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
# This file is loaded repeatedly in the test suite due to the way DI performs
2+
# code tracking. Remove the constants if they have already been defined
3+
# to avoid Ruby warnings.
4+
begin
5+
Object.send(:remove_const, :HookTestClass)
6+
rescue NameError
7+
end
8+
begin
9+
Object.send(:remove_const, :YieldingMethodMissingHookTestClass)
10+
rescue NameError
11+
end
12+
begin
13+
Object.send(:remove_const, :HookIvarTestClass)
14+
rescue NameError
15+
end
16+
117
class HookTestClass
218
class TestException < StandardError
319
end

spec/datadog/di/instrumenter_spec.rb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,7 +1008,7 @@
10081008
end
10091009

10101010
let(:probe) do
1011-
Datadog::DI::Probe.new(file: 'hook_line_load.rb', line_no: 5,
1011+
Datadog::DI::Probe.new(file: 'hook_line_load.rb', line_no: 25,
10121012
id: 1, type: :log, rate_limit: rate_limit)
10131013
end
10141014

@@ -1060,7 +1060,7 @@
10601060
let(:code_tracker) { nil }
10611061

10621062
let(:probe) do
1063-
Datadog::DI::Probe.new(file: 'hook_line_load.rb', line_no: 1,
1063+
Datadog::DI::Probe.new(file: 'hook_line_load.rb', line_no: 21,
10641064
id: 1, type: :log)
10651065
end
10661066

@@ -1096,7 +1096,7 @@
10961096
end
10971097

10981098
let(:probe) do
1099-
Datadog::DI::Probe.new(file: 'hook_line_load.rb', line_no: 1,
1099+
Datadog::DI::Probe.new(file: 'hook_line_load.rb', line_no: 21,
11001100
id: 1, type: :log)
11011101
end
11021102

@@ -1240,7 +1240,7 @@
12401240
end
12411241

12421242
let(:probe) do
1243-
Datadog::DI::Probe.new(file: 'hook_line_targeted.rb', line_no: 3,
1243+
Datadog::DI::Probe.new(file: 'hook_line_targeted.rb', line_no: 13,
12441244
id: 1, type: :log)
12451245
end
12461246

@@ -1249,7 +1249,7 @@
12491249
target = code_tracker.send(:registry)[path]
12501250
expect(target).to be_a(RubyVM::InstructionSequence)
12511251

1252-
expect_any_instance_of(TracePoint).to receive(:enable).with(target: target, target_line: 3).and_call_original
1252+
expect_any_instance_of(TracePoint).to receive(:enable).with(target: target, target_line: 13).and_call_original
12531253

12541254
instrumenter.hook_line(probe) do |payload|
12551255
observed_calls << payload
@@ -1267,7 +1267,7 @@
12671267
end
12681268

12691269
let(:probe) do
1270-
Datadog::DI::Probe.new(file: 'hook_line_load.rb', line_no: 6,
1270+
Datadog::DI::Probe.new(file: 'hook_line_load.rb', line_no: 26,
12711271
id: 1, type: :log, rate_limit: rate_limit)
12721272
end
12731273

@@ -1332,7 +1332,7 @@
13321332

13331333
context 'non-enriched probe' do
13341334
let(:probe_args) do
1335-
{file: 'hook_line_recursive.rb', line_no: 3}
1335+
{file: 'hook_line_recursive.rb', line_no: 13}
13361336
end
13371337

13381338
it 'invokes callback for every method invocation' do
@@ -1365,7 +1365,7 @@
13651365
include_context 'with code tracking'
13661366

13671367
before do
1368-
require_relative 'hook_line_recursive'
1368+
load File.join(File.dirname(__FILE__), 'hook_line_recursive.rb')
13691369
end
13701370

13711371
# We need to use a rate limiter, otherwise the stack is exhausted
@@ -1377,7 +1377,7 @@
13771377

13781378
context 'non-enriched probe' do
13791379
let(:probe) do
1380-
Datadog::DI::Probe.new(file: 'hook_line_recursive.rb', line_no: 11,
1380+
Datadog::DI::Probe.new(file: 'hook_line_recursive.rb', line_no: 21,
13811381
id: 1, type: :log, rate_limit: rate_limit)
13821382
end
13831383

@@ -1408,7 +1408,7 @@
14081408
end
14091409

14101410
let(:probe) do
1411-
Datadog::DI::Probe.new(file: 'hook_line_load.rb', line_no: 32,
1411+
Datadog::DI::Probe.new(file: 'hook_line_load.rb', line_no: 52,
14121412
id: 1, type: :log, rate_limit: rate_limit)
14131413
end
14141414

@@ -1436,7 +1436,7 @@
14361436
include_context 'with code tracking'
14371437

14381438
let(:probe) do
1439-
Datadog::DI::Probe.new(file: 'hook_line_load.rb', line_no: 10,
1439+
Datadog::DI::Probe.new(file: 'hook_line_load.rb', line_no: 30,
14401440
id: 1, type: :log, rate_limit: rate_limit, condition: condition)
14411441
end
14421442

@@ -1489,7 +1489,7 @@
14891489

14901490
context 'when condition is on instance variable' do
14911491
let(:probe) do
1492-
Datadog::DI::Probe.new(file: 'hook_line_load.rb', line_no: 24,
1492+
Datadog::DI::Probe.new(file: 'hook_line_load.rb', line_no: 47,
14931493
id: 1, type: :log, rate_limit: rate_limit, condition: condition)
14941494
end
14951495

spec/datadog/di/integration/everything_from_remote_config_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ def assert_received_and_errored
612612
{
613613
id: '11', name: 'bar', type: 'LOG_PROBE',
614614
where: {
615-
sourceFile: 'hook_line_load.rb', lines: [14],
615+
sourceFile: 'hook_line_load.rb', lines: [34],
616616
},
617617
when: {json: {'contains' => [{'ref' => 'bar'}, 'baz']}, dsl: '(expression)'},
618618
}

spec/datadog/di/integration/instrumentation_integration_test_class.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ def method_with_no_locals
1111

1212
def test_method
1313
a = 21
14-
password = 'password'
15-
redacted = {b: 33, session: 'blah'}
14+
$__password = password = 'password'
15+
$__redacted = redacted = {b: 33, session: 'blah'}
1616
# The following condition causes instrumentation trace point callback
1717
# to be invoked multiple times in CircleCI on Ruby 3.0-3.2 and 3.4
1818
#if true || password || redacted

spec/datadog/di/integration/instrumentation_integration_test_class_2.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
class InstrumentationIntegrationTestClass2
44
def test_method
55
a = 21
6-
password = 'password'
7-
redacted = {b: 33, session: 'blah'}
6+
$__password = password = 'password'
7+
$__redacted = redacted = {b: 33, session: 'blah'}
88
# padding
99
# padding
1010
a * 2 # line 10

spec/datadog/di/integration/instrumentation_integration_test_class_3.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
class InstrumentationIntegrationTestClass3
44
def test_method
55
a = 21
6-
password = 'password'
7-
redacted = {b: 33, session: 'blah'}
6+
$__password = password = 'password'
7+
$__redacted = redacted = {b: 33, session: 'blah'}
88
# padding
99
# padding
1010
a * 2 # line 10

spec/datadog/di/integration/instrumentation_integration_test_class_4.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
class InstrumentationIntegrationTestClass4
44
def test_method
55
a = 21
6-
password = 'password'
7-
redacted = {b: 33, session: 'blah'}
6+
$__password = password = 'password'
7+
$__redacted = redacted = {b: 33, session: 'blah'}
88
# padding
99
# padding
1010
a * 2 # line 10

0 commit comments

Comments
 (0)