DataDog / datadog-ci-rb

Ruby library for Datadog Test Optimization
https://docs.datadoghq.com/tests/setup/ruby/?tab=cloudciprovideragentless
Other
11 stars 4 forks source link

[CIVIS-10061] Fix Knapsack Pro integration #180

Closed anmarchenko closed 6 months ago

anmarchenko commented 6 months ago

Fixes #147

This PR fixes the following issues with our current Knapsack Pro support:

Dropping tests because of missing test session

When running knapsack_pro:rspec:queue command, Datadog.configure block gets called when rails_helper is required which in this case happens after KnapsackPro::Extensions::RSpecExtension.setup! had been run.

To fix this, we check whether RSpec::Core::Runner was already instrumented by knapsack: if yes, we instrument RSpec::Core::Runner directly. If not, we hook into KnapsackPro::Extensions::RSpecExtension to instrument the runner after knapsack injects its methods:

if defined?(::KnapsackPro::Extensions::RSpecExtension::Runner) &&
  ::RSpec::Core::Runner.ancestors.include?(::KnapsackPro::Extensions::RSpecExtension::Runner)
  # knapsack already patched rspec runner
  require_relative "runner"
  ::RSpec::Core::Runner.include(KnapsackPro::Runner)
else
  # knapsack didn't patch rspec runner yet
  require_relative "extension"
  ::KnapsackPro::Extensions::RSpecExtension.include(KnapsackPro::Extension)
end

Noise from knapsack_pro:rspec_test_example_detector test command

We noticed that in many cases when using knapsack, we got a lot more test runs than there are unique tests; additional test runs all have very short duration and are part of knapsack_pro:rspec_test_example_detector.

It turned out, that we trace rspec session that is running with --dry-run: test example detector from knapsack uses dry run to generate a report with all rspec examples.

This issue is fixed by never tracing rspec tests when dry run is enabled:

return super if ::RSpec.configuration.dry_run?

How to test the change? Unit tests are provided and additionally tested with Github Actions: https://github.com/DataDog/rails-app-with-knapsack_pro/actions/workflows/main.yaml

The test runs arrived correctly:

image

There are no additional test runs from rspec_test_example_detector - the number of tests is equal to the number of test runs (51 vs 51)

codecov-commenter commented 6 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 98.95%. Comparing base (80e7b13) to head (25bf96c).

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #180 +/- ## ======================================= Coverage 98.94% 98.95% ======================================= Files 205 209 +4 Lines 9688 9758 +70 Branches 445 452 +7 ======================================= + Hits 9586 9656 +70 Misses 102 102 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.