Skip to content

Commit 66829c1

Browse files
committed
Address RSpec lifecycle review feedback
1 parent a01eb4c commit 66829c1

4 files changed

Lines changed: 77 additions & 4 deletions

File tree

lib/datadog/ci/contrib/rspec/example.rb

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ module InstanceMethods
2626
# ============================================
2727

2828
def run(*args)
29-
return super unless datadog_configuration[:enabled]
30-
return super if ::RSpec.configuration.dry_run? && !datadog_configuration[:dry_run_enabled]
29+
return super unless datadog_tracing_enabled?
3130

3231
@datadog_test_tracing_component = test_tracing_component
3332

@@ -198,19 +197,39 @@ def datadog_context_ids
198197
# ============================================
199198

200199
def run_before_example
201-
test_tracing_component.trace(Ext::BEFORE_STEP_SPAN_NAME, type: Ext::STEP_SPAN_TYPE) { super }
200+
return super unless @datadog_test_tracing_component
201+
202+
skip_exception = nil
203+
# @type var skip_exception: ::RSpec::Core::Pending::SkipDeclaredInExample?
204+
result = test_tracing_component.trace(Ext::BEFORE_STEP_SPAN_NAME, type: Ext::STEP_SPAN_TYPE) do
205+
super
206+
rescue ::RSpec::Core::Pending::SkipDeclaredInExample => e
207+
skip_exception = e
208+
end
209+
210+
raise skip_exception if skip_exception
211+
212+
result
202213
end
203214

204215
def run_after_example
216+
return super unless @datadog_test_tracing_component
217+
205218
exception_before_hooks = exception
206219

207220
test_tracing_component.trace(Ext::AFTER_STEP_SPAN_NAME, type: Ext::STEP_SPAN_TYPE) do |span|
208221
result = super
209-
span.failed!(exception: exception) if exception && exception != exception_before_hooks
222+
span&.failed!(exception: exception) if exception && exception != exception_before_hooks
210223
result
211224
end
212225
end
213226

227+
def datadog_tracing_enabled?
228+
Datadog.configuration.ci.enabled &&
229+
datadog_configuration[:enabled] &&
230+
(!::RSpec.configuration.dry_run? || datadog_configuration[:dry_run_enabled])
231+
end
232+
214233
def build_test_tags
215234
# @type var tags : Hash[String, String]
216235
tags = {

sig/datadog/ci/contrib/rspec/example.rbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ module Datadog
4242
# Run method helpers
4343
def run_before_example: () -> untyped
4444
def run_after_example: () -> untyped
45+
def datadog_tracing_enabled?: () -> bool
4546
def build_test_tags: () -> Hash[String, String]
4647
def handle_test_result: (Datadog::CI::Test? test_span, Exception? test_failure) -> Exception?
4748
def update_formatter_metadata: (Datadog::CI::Test? test_span) -> void

spec/datadog/ci/contrib/rspec/instrumentation_spec.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,36 @@ def rspec_session_run(
307307
expect(custom_spans.map(&:name)).to contain_exactly("before", "after")
308308
end
309309

310+
it "does not trace lifecycle steps when the RSpec integration is disabled" do
311+
rspec_configuration = Datadog.configuration.ci[:rspec]
312+
allow(rspec_configuration).to receive(:[]).and_call_original
313+
allow(rspec_configuration).to receive(:[]).with(:enabled).and_return(false)
314+
315+
result = with_new_rspec_environment do
316+
RSpec.describe "some test" do
317+
after { raise "failure" }
318+
it("fails in after") {}
319+
end.run
320+
end
321+
322+
expect(result).to be(false)
323+
expect(spans).to be_empty
324+
end
325+
326+
it "does not trace lifecycle steps when Datadog CI is disabled" do
327+
allow(Datadog.configuration.ci).to receive(:enabled).and_return(false)
328+
329+
result = with_new_rspec_environment do
330+
RSpec.describe "some test" do
331+
after { raise "failure" }
332+
it("fails in after") {}
333+
end.run
334+
end
335+
336+
expect(result).to be(false)
337+
expect(spans).to be_empty
338+
end
339+
310340
context "catches failures" do
311341
def expect_failure
312342
expect(first_test_span).to have_fail_status
@@ -464,6 +494,23 @@ def expect_failure
464494
expect(first_test_span).not_to have_error
465495
end
466496

497+
it "with skip call in before hook" do
498+
with_new_rspec_environment do
499+
RSpec.describe "some skipped test" do
500+
before { skip }
501+
502+
it "foo" do
503+
expect(1 + 1).to eq(5)
504+
end
505+
end.run
506+
end
507+
508+
expect(first_test_span).to have_skip_status
509+
expect(first_test_span).not_to have_error
510+
expect(custom_spans.map(&:name)).to include("before")
511+
expect(custom_spans.find { |span| span.name == "before" }).not_to have_error
512+
end
513+
467514
it "with skip call and reason given" do
468515
with_new_rspec_environment do
469516
RSpec.describe "some skipped test" do

vendor/rbs/rspec/0/rspec.rbs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ end
66
module RSpec::Core
77
end
88

9+
module RSpec::Core::Pending
10+
end
11+
12+
class RSpec::Core::Pending::SkipDeclaredInExample < StandardError
13+
end
14+
915
module RSpec::Queue
1016
end
1117

0 commit comments

Comments
 (0)