diff --git a/lib/datadog/ci/test_optimisation/component.rb b/lib/datadog/ci/test_optimisation/component.rb index cb25ea2c..465fe5c1 100644 --- a/lib/datadog/ci/test_optimisation/component.rb +++ b/lib/datadog/ci/test_optimisation/component.rb @@ -109,13 +109,15 @@ def stop_coverage(test) Telemetry.code_coverage_finished(test) coverage = coverage_collector&.stop + + # if test was skipped, we discard coverage data + return if test.skipped? + if coverage.nil? || coverage.empty? Telemetry.code_coverage_is_empty return end - return if test.skipped? - test_source_file = test.source_file # cucumber's gherkin files are not covered by the code coverage collector diff --git a/spec/datadog/ci/test_optimisation/component_spec.rb b/spec/datadog/ci/test_optimisation/component_spec.rb index f3ca2744..1b946405 100644 --- a/spec/datadog/ci/test_optimisation/component_spec.rb +++ b/spec/datadog/ci/test_optimisation/component_spec.rb @@ -235,6 +235,24 @@ expect(subject).to be_nil expect(writer).not_to have_received(:write) end + + it_behaves_like "emits no metric", :inc, Datadog::CI::Ext::Telemetry::METRIC_CODE_COVERAGE_IS_EMPTY + end + + context "when test is skipped and coverage is empty" do + before do + allow_any_instance_of(Datadog::CI::TestOptimisation::Coverage::DDCov).to receive(:stop).and_return(nil) + + component.start_coverage(test_span) + test_span.skipped! + end + + it "does not write coverage event" do + expect(subject).to be_nil + expect(writer).not_to have_received(:write) + end + + it_behaves_like "emits no metric", :inc, Datadog::CI::Ext::Telemetry::METRIC_CODE_COVERAGE_IS_EMPTY end context "when coverage was not collected" do