diff --git a/lib/datadog/ci/contrib/minitest/runner.rb b/lib/datadog/ci/contrib/minitest/runner.rb index 86c45d28..a64ad646 100644 --- a/lib/datadog/ci/contrib/minitest/runner.rb +++ b/lib/datadog/ci/contrib/minitest/runner.rb @@ -28,6 +28,19 @@ def init_plugins(*args) test_visibility_component.start_test_module(Ext::FRAMEWORK) end + def run_one_method(klass, method_name) + return super unless datadog_configuration[:enabled] + + result = nil + # retries here + test_retries_component.with_retries do |test_finished_callback| + Thread.current[:__dd_retry_callback] = test_finished_callback + + result = super + end + result + end + private def datadog_configuration @@ -37,6 +50,10 @@ def datadog_configuration def test_visibility_component Datadog.send(:components).test_visibility end + + def test_retries_component + Datadog.send(:components).test_retries + end end end end diff --git a/lib/datadog/ci/contrib/minitest/test.rb b/lib/datadog/ci/contrib/minitest/test.rb index 27ed2056..ff9f816a 100644 --- a/lib/datadog/ci/contrib/minitest/test.rb +++ b/lib/datadog/ci/contrib/minitest/test.rb @@ -50,7 +50,7 @@ def after_teardown test_span = test_visibility_component.active_test return super unless test_span - finish_with_result(test_span, result_code) + finish_with_result(test_span, result_code, Thread.current[:__dd_retry_callback]) if Helpers.parallel?(self.class) finish_with_result(test_span.test_suite, result_code) end @@ -60,7 +60,7 @@ def after_teardown private - def finish_with_result(span, result_code) + def finish_with_result(span, result_code, callback = nil) return unless span case result_code @@ -71,6 +71,9 @@ def finish_with_result(span, result_code) when "S" span.skipped!(reason: failure.message) end + + callback.call(span) if callback + span.finish end diff --git a/spec/datadog/ci/contrib/minitest/instrumentation_spec.rb b/spec/datadog/ci/contrib/minitest/instrumentation_spec.rb index 165f117f..f9f4e554 100644 --- a/spec/datadog/ci/contrib/minitest/instrumentation_spec.rb +++ b/spec/datadog/ci/contrib/minitest/instrumentation_spec.rb @@ -948,4 +948,47 @@ def test_with_background_thread ) end end + + context "with flaky test and test retries enabled" do + include_context "CI mode activated" do + let(:integration_name) { :minitest } + + let(:flaky_test_retries_enabled) { true } + end + + before do + Minitest.run([]) + end + + before(:context) do + Thread.current[:dd_coverage_collector] = nil + + Minitest::Runnable.reset + + require_relative "helpers/addition_helper" + class SomeTestWithThreads < Minitest::Test + def test_with_background_thread + # add thread to test that code coverage is collected + t = Thread.new do + AdditionHelper.add(1, 2) + end + t.join + assert true + end + end + end + + it "does not cover the background thread" do + skip if PlatformHelpers.jruby? + + expect(test_spans).to have(1).item + expect(coverage_events).to have(1).item + + # expect that background thread is not covered + cov_event = find_coverage_for_test(first_test_span) + expect(cov_event.coverage.keys).not_to include( + absolute_path("helpers/addition_helper.rb") + ) + end + end end