Skip to content

Commit

Permalink
make sure that skipping context hooks works for nested example groups
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Dec 4, 2024
1 parent b2fe2bb commit 1d61ee5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
10 changes: 7 additions & 3 deletions lib/datadog/ci/contrib/rspec/example_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ module ClassMethods
def run(reporter = ::RSpec::Core::NullReporter)
return super if ::RSpec.configuration.dry_run? && !datadog_configuration[:dry_run_enabled]
return super unless datadog_configuration[:enabled]

# skip the context hooks if all descendant example are going to be skipped
# IMPORTANT: must happen before top_level? check because skipping hooks must happen
# even if it is a nested context
metadata[:skip] = true if all_examples_skipped_by_datadog?

# return early because we create Datadog::CI::TestSuite only for top-level example groups
return super unless top_level?

suite_name = "#{description} at #{file_path}"
Expand All @@ -29,9 +36,6 @@ def run(reporter = ::RSpec::Core::NullReporter)
}
)

# skip the context hooks if all descendant example are going to be skipped
metadata[:skip] = true if all_examples_skipped_by_datadog?

success = super
return success unless test_suite

Expand Down
19 changes: 17 additions & 2 deletions spec/datadog/ci/contrib/rspec/instrumentation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
RSpec.describe "RSpec instrumentation" do
let(:integration) { Datadog::CI::Contrib::Instrumentation.fetch_integration(:rspec) }
let(:before_all_spy) { spy(:before_all, call: nil) }
let(:before_context_spy) { spy(:before_context, call: nil) }

before do
# expect that public manual API isn't used
Expand All @@ -19,6 +20,7 @@ def rspec_session_run(
with_flaky_test: false,
with_canceled_test: false,
with_flaky_test_that_fails_once: false,
with_test_outside_context: false,
unskippable: {
test: false,
context: false,
Expand All @@ -38,6 +40,7 @@ def rspec_session_run(

current_let_value = 0
before_all_spy_local = before_all_spy
before_context_spy_local = before_context_spy

with_new_rspec_environment do
spec = RSpec.describe "SomeTest", suite_meta do
Expand All @@ -46,6 +49,10 @@ def rspec_session_run(
end

context "nested", context_meta do
before(:all) do
before_context_spy_local.call
end

let(:let_value) { current_let_value += 1 }

it "foo", test_meta do
Expand Down Expand Up @@ -100,6 +107,12 @@ def rspec_session_run(
end
end
end

if with_test_outside_context
it "is outside of context" do
expect(1 + 1).to eq(2)
end
end
end

options_array = %w[--pattern none]
Expand Down Expand Up @@ -156,7 +169,7 @@ def rspec_session_run(
:source_file,
"spec/datadog/ci/contrib/rspec/instrumentation_spec.rb"
)
expect(first_test_span).to have_test_tag(:source_start, "129")
expect(first_test_span).to have_test_tag(:source_start, "142")
expect(first_test_span).to have_test_tag(
:codeowners,
"[\"@DataDog/ruby-guild\", \"@DataDog/ci-app-libraries\"]"
Expand Down Expand Up @@ -586,7 +599,7 @@ def expect_failure
:source_file,
"spec/datadog/ci/contrib/rspec/instrumentation_spec.rb"
)
expect(first_test_suite_span).to have_test_tag(:source_start, "43")
expect(first_test_suite_span).to have_test_tag(:source_start, "46")
expect(first_test_suite_span).to have_test_tag(
:codeowners,
"[\"@DataDog/ruby-guild\", \"@DataDog/ci-app-libraries\"]"
Expand Down Expand Up @@ -761,6 +774,7 @@ def rspec_skipped_session_run
rspec_session_run(with_failed_test: true)

expect(before_all_spy).to have_received(:call)
expect(before_context_spy).to have_received(:call)
end

it "sends test session level tags" do
Expand Down Expand Up @@ -801,6 +815,7 @@ def rspec_skipped_session_run
rspec_session_run(with_failed_test: true)

expect(before_all_spy).not_to have_received(:call)
expect(before_context_spy).not_to have_received(:call)
end

context "but some tests are unskippable" do
Expand Down

0 comments on commit 1d61ee5

Please sign in to comment.