Skip to content

Commit

Permalink
test that total code coverage tag is populated in unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Sep 24, 2024
1 parent 9681ab4 commit 9b12d3d
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 5 deletions.
5 changes: 4 additions & 1 deletion lib/datadog/ci/contrib/simplecov/result_extractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ def self.included(base)

module ClassMethods
def __dd_peek_result
return nil unless datadog_configuration[:enabled]
unless datadog_configuration[:enabled]
Datadog.logger.debug("SimpleCov instrumentation is disabled")
return nil
end

result = ::SimpleCov::UselessResultsRemover.call(
::SimpleCov::ResultAdapter.call(::Coverage.peek_result)
Expand Down
22 changes: 18 additions & 4 deletions lib/datadog/ci/test_visibility/total_coverage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,26 @@ module CI
module TestVisibility
module TotalCoverage
def self.extract_lines_pct(test_session)
return unless defined?(::SimpleCov)
return unless ::SimpleCov.running
return unless ::SimpleCov.respond_to?(:__dd_peek_result)
unless defined?(::SimpleCov)
Datadog.logger.debug("SimpleCov is not loaded, skipping code coverage extraction")
return
end

unless ::SimpleCov.running
Datadog.logger.debug("SimpleCov is not running, skipping code coverage extraction")
return
end

unless ::SimpleCov.respond_to?(:__dd_peek_result)
Datadog.logger.debug("SimpleCov is not patched, skipping code coverage extraction")
return
end

result = ::SimpleCov.__dd_peek_result
return unless result
unless result
Datadog.logger.debug("SimpleCov result is nil, skipping code coverage extraction")
return
end

test_session.set_tag(Ext::Test::TAG_CODE_COVERAGE_LINES_PCT, result.covered_percent)
end
Expand Down
4 changes: 4 additions & 0 deletions spec/datadog/ci/contrib/cucumber/instrumentation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,14 @@
)
expect(test_session_span).to have_pass_status

# ITR
expect(test_session_span).to have_test_tag(:itr_test_skipping_enabled, "true")
expect(test_session_span).to have_test_tag(:itr_test_skipping_type, "test")
expect(test_session_span).to have_test_tag(:itr_tests_skipped, "false")
expect(test_session_span).to have_test_tag(:itr_test_skipping_count, 0)

# Total code coverage
expect(test_session_span).to have_test_tag(:code_coverage_lines_pct)
end

it "creates test module span" do
Expand Down
4 changes: 4 additions & 0 deletions spec/datadog/ci/contrib/minitest/instrumentation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -440,11 +440,15 @@ def test_pass_other
Datadog::CI::Contrib::Minitest::Integration.version.to_s
)

# ITR
expect(test_session_span).to have_test_tag(:itr_test_skipping_enabled, "true")
expect(test_session_span).to have_test_tag(:itr_test_skipping_type, "test")
expect(test_session_span).to have_test_tag(:itr_tests_skipped, "false")
expect(test_session_span).to have_test_tag(:itr_test_skipping_count, 0)

# Total code coverage
expect(test_session_span).to have_test_tag(:code_coverage_lines_pct)

expect(test_session_span).to have_pass_status
end

Expand Down
3 changes: 3 additions & 0 deletions spec/datadog/ci/contrib/rspec/instrumentation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,9 @@ def expect_failure
expect(test_session_span).not_to have_test_tag(:itr_tests_skipped)
expect(test_session_span).not_to have_test_tag(:itr_test_skipping_count)

# Total code coverage
expect(test_session_span).to have_test_tag(:code_coverage_lines_pct)

expect(test_session_span).to have_pass_status
end

Expand Down

0 comments on commit 9b12d3d

Please sign in to comment.