Class: Datadog::CI::TestImpactAnalysis::Component
- Inherits:
-
Object
- Object
- Datadog::CI::TestImpactAnalysis::Component
- Includes:
- Utils::Stateful
- Defined in:
- lib/datadog/ci/test_impact_analysis/component.rb
Overview
Test Impact Analysis implementation Integrates with backend to provide test impact analysis data and skip tests that are not impacted by the changes
Constant Summary collapse
- FILE_STORAGE_KEY =
"test_impact_analysis_component_state"
Instance Attribute Summary collapse
-
#code_coverage_enabled ⇒ Object
readonly
Returns the value of attribute code_coverage_enabled.
-
#correlation_id ⇒ Object
readonly
Returns the value of attribute correlation_id.
-
#enabled ⇒ Object
readonly
Returns the value of attribute enabled.
-
#skippable_tests ⇒ Object
readonly
Returns the value of attribute skippable_tests.
-
#skippable_tests_fetch_error ⇒ Object
readonly
Returns the value of attribute skippable_tests_fetch_error.
-
#test_skipping_enabled ⇒ Object
readonly
Returns the value of attribute test_skipping_enabled.
Instance Method Summary collapse
-
#clear_context_coverage(context_id) ⇒ void
Clears stored context coverage for a specific context.
- #code_coverage? ⇒ Boolean
- #configure(remote_configuration, test_session) ⇒ Object
-
#context_coverage_enabled? ⇒ Boolean
Returns whether context coverage collection is enabled.
- #enabled? ⇒ Boolean
-
#initialize(dd_env:, config_tags: {}, api: nil, coverage_writer: nil, enabled: false, bundle_location: nil, use_single_threaded_coverage: false, use_allocation_tracing: true, static_dependencies_tracking_enabled: false) ⇒ Component
constructor
A new instance of Component.
- #mark_if_skippable(test) ⇒ Object
-
#on_test_context_started(context_id) ⇒ void
Called when a test context (e.g., RSpec example group with before(:context)) starts.
-
#on_test_finished(test, context) ⇒ Datadog::CI::TestImpactAnalysis::Coverage::Event?
Called when a test finishes.
-
#on_test_started(test) ⇒ void
Called when a test starts within a context.
- #restore_state(state) ⇒ Object
- #restore_state_from_datadog_test_runner ⇒ Object
-
#serialize_state ⇒ Object
Implementation of Stateful interface.
- #shutdown! ⇒ Object
- #skippable?(datadog_test_id) ⇒ Boolean
- #skippable_tests_count ⇒ Object
- #skipping_tests? ⇒ Boolean
-
#start_coverage ⇒ void
Starts coverage collection.
-
#stop_coverage ⇒ Hash?
Stops coverage collection and returns raw coverage data.
- #storage_key ⇒ Object
- #write_test_session_tags(test_session, skipped_tests_count) ⇒ Object
Methods included from Utils::Stateful
#load_cached_known_tests, #load_cached_settings, #load_cached_skippable_tests, #load_cached_test_management, #load_component_state, #store_component_state, #test_optimization_cache
Constructor Details
#initialize(dd_env:, config_tags: {}, api: nil, coverage_writer: nil, enabled: false, bundle_location: nil, use_single_threaded_coverage: false, use_allocation_tracing: true, static_dependencies_tracking_enabled: false) ⇒ Component
Returns a new instance of Component.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 36 def initialize( dd_env:, config_tags: {}, api: nil, coverage_writer: nil, enabled: false, bundle_location: nil, use_single_threaded_coverage: false, use_allocation_tracing: true, static_dependencies_tracking_enabled: false ) @enabled = enabled @api = api @dd_env = dd_env @config_tags = || {} @bundle_location = if bundle_location && !File.absolute_path?(bundle_location) File.join(Git::LocalRepository.root, bundle_location) else bundle_location end @use_single_threaded_coverage = use_single_threaded_coverage @use_allocation_tracing = use_allocation_tracing @static_dependencies_tracking_enabled = static_dependencies_tracking_enabled @test_skipping_enabled = false @code_coverage_enabled = false @coverage_writer = coverage_writer @correlation_id = nil @skippable_tests = Set.new @mutex = Mutex.new # Context coverage: stores coverage collected during before(:context)/before(:all) hooks # keyed by context_id (e.g., RSpec scoped_id for example groups) # Only used when use_single_threaded_coverage is false (multi-threaded mode) @context_coverages = {} @context_coverages_mutex = Mutex.new # Currently active context ID for context coverage collection @current_context_id = nil @current_context_id_mutex = Mutex.new Datadog.logger.debug("TestImpactAnalysis initialized with enabled: #{@enabled}") end |
Instance Attribute Details
#code_coverage_enabled ⇒ Object (readonly)
Returns the value of attribute code_coverage_enabled.
33 34 35 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 33 def code_coverage_enabled @code_coverage_enabled end |
#correlation_id ⇒ Object (readonly)
Returns the value of attribute correlation_id.
33 34 35 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 33 def correlation_id @correlation_id end |
#enabled ⇒ Object (readonly)
Returns the value of attribute enabled.
33 34 35 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 33 def enabled @enabled end |
#skippable_tests ⇒ Object (readonly)
Returns the value of attribute skippable_tests.
33 34 35 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 33 def skippable_tests @skippable_tests end |
#skippable_tests_fetch_error ⇒ Object (readonly)
Returns the value of attribute skippable_tests_fetch_error.
33 34 35 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 33 def skippable_tests_fetch_error @skippable_tests_fetch_error end |
#test_skipping_enabled ⇒ Object (readonly)
Returns the value of attribute test_skipping_enabled.
33 34 35 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 33 def test_skipping_enabled @test_skipping_enabled end |
Instance Method Details
#clear_context_coverage(context_id) ⇒ void
This method returns an undefined value.
Clears stored context coverage for a specific context. Should be called when a context finishes (e.g., after(:context) completes).
259 260 261 262 263 264 265 266 267 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 259 def clear_context_coverage(context_id) return unless context_coverage_enabled? @context_coverages_mutex.synchronize do @context_coverages.delete(context_id) Datadog.logger.debug { "Cleared context coverage for [#{context_id}]" } end end |
#code_coverage? ⇒ Boolean
123 124 125 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 123 def code_coverage? @code_coverage_enabled end |
#configure(remote_configuration, test_session) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 84 def configure(remote_configuration, test_session) return unless enabled? Datadog.logger.debug("Configuring TestImpactAnalysis with remote configuration: #{remote_configuration}") @enabled = remote_configuration.itr_enabled? @test_skipping_enabled = @enabled && remote_configuration.tests_skipping_enabled? @code_coverage_enabled = @enabled && remote_configuration.code_coverage_enabled? test_session.set_tag(Ext::Test::TAG_ITR_TEST_SKIPPING_ENABLED, @test_skipping_enabled) test_session.set_tag(Ext::Test::TAG_CODE_COVERAGE_ENABLED, @code_coverage_enabled) # we skip tests, not suites test_session.set_tag(Ext::Test::TAG_ITR_TEST_SKIPPING_TYPE, Ext::Test::ITR_TEST_SKIPPING_MODE) if @code_coverage_enabled load_datadog_cov! populate_static_dependencies_map! end # Load external cache or component state first, and if successful, skip fetching skippable tests if skipping_tests? return if load_component_state fetch_skippable_tests(test_session) store_component_state if test_session.distributed end Datadog.logger.debug("Configured TestImpactAnalysis with enabled: #{@enabled}, skipping_tests: #{@test_skipping_enabled}, code_coverage: #{@code_coverage_enabled}") end |
#context_coverage_enabled? ⇒ Boolean
Returns whether context coverage collection is enabled. Context coverage is disabled in single-threaded mode.
273 274 275 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 273 def context_coverage_enabled? enabled? && code_coverage? && !@use_single_threaded_coverage end |
#enabled? ⇒ Boolean
115 116 117 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 115 def enabled? @enabled end |
#mark_if_skippable(test) ⇒ Object
283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 283 def mark_if_skippable(test) return if !enabled? || !skipping_tests? if skippable?(test.datadog_test_id) && !test.attempt_to_fix? test.set_tag(Ext::Test::TAG_ITR_SKIPPED_BY_ITR, "true") Datadog.logger.debug { "Marked test as skippable: #{test.datadog_test_id}" } else Datadog.logger.debug { "Test is not skippable: #{test.datadog_test_id}" } end end |
#on_test_context_started(context_id) ⇒ void
This method returns an undefined value.
Called when a test context (e.g., RSpec example group with before(:context)) starts. Starts collecting coverage that will be merged into all tests within this context.
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 152 def on_test_context_started(context_id) return unless context_coverage_enabled? # Stop and store any existing context coverage before starting new one. # This ensures that outer context coverage is preserved when nested contexts start. stop_context_coverage_and_store Datadog.logger.debug { "Starting context coverage collection for context [#{context_id}]" } # Store the context_id we're collecting for @current_context_id_mutex.synchronize do @current_context_id = context_id end coverage_collector&.start end |
#on_test_finished(test, context) ⇒ Datadog::CI::TestImpactAnalysis::Coverage::Event?
Called when a test finishes. This method:
-
Stops test coverage collection
-
Merges context coverage from all relevant contexts
-
Writes the combined coverage event
-
Records ITR statistics if test was skipped by TIA
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 201 def on_test_finished(test, context) return unless enabled? # Handle ITR statistics if test.skipped_by_test_impact_analysis? Telemetry.itr_skipped context.incr_tests_skipped_by_tia_count end # Handle code coverage return unless code_coverage? Telemetry.code_coverage_finished(test) coverage = coverage_collector&.stop # if test was skipped, we discard coverage data return if test.skipped? coverage ||= {} # Merge context coverage from all relevant contexts context_ids = test.context_ids || [] merge_context_coverages_into_test(coverage, context_ids) if coverage.empty? Telemetry.code_coverage_is_empty return end # cucumber's gherkin files are not covered by the code coverage collector - we add them here explicitly test_source_file = test.source_file ensure_test_source_covered(test_source_file, coverage) unless test_source_file.nil? # if we have static dependencies tracking enabled then we can make the coverage # more precise by fetching which files we depend on based on constants usage enrich_coverage_with_static_dependencies(coverage) Telemetry.code_coverage_files(coverage.size) coverage_event = Coverage::Event.new( test_id: test.id.to_s, test_suite_id: test.test_suite_id.to_s, test_session_id: test.test_session_id.to_s, coverage: coverage ) Datadog.logger.debug { "Writing coverage event \n #{coverage_event.pretty_inspect}" } write(coverage_event) coverage_event end |
#on_test_started(test) ⇒ void
This method returns an undefined value.
Called when a test starts within a context. This method:
-
Stops any in-progress context coverage collection and stores it
-
Starts coverage collection for the test itself
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 175 def on_test_started(test) return if !enabled? || !code_coverage? # Stop any in-progress context coverage and store it stop_context_coverage_and_store Telemetry.code_coverage_started(test) context_ids = test.context_ids || [] Datadog.logger.debug do "Starting test coverage for [#{test.name}] with context chain: #{context_ids.inspect}" end coverage_collector&.start end |
#restore_state(state) ⇒ Object
321 322 323 324 325 326 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 321 def restore_state(state) @mutex.synchronize do @correlation_id = state[:correlation_id] @skippable_tests = state[:skippable_tests] end end |
#restore_state_from_datadog_test_runner ⇒ Object
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 332 def restore_state_from_datadog_test_runner Datadog.logger.debug { "Restoring skippable tests from Test Optimization cache" } skippable_tests_data = load_cached_skippable_tests if skippable_tests_data.nil? Datadog.logger.debug { "Restoring skippable tests failed, will request again" } return false end Datadog.logger.debug { "Restored skippable tests from Test Optimization: #{skippable_tests_data}" } skippable_response = Skippable::Response.from_json(skippable_tests_data) @mutex.synchronize do @correlation_id = skippable_response.correlation_id @skippable_tests = skippable_response.tests end Datadog.logger.debug { "Found [#{@skippable_tests.size}] skippable tests from context" } Datadog.logger.debug { "ITR correlation ID from context: #{@correlation_id}" } true end |
#serialize_state ⇒ Object
Implementation of Stateful interface
314 315 316 317 318 319 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 314 def serialize_state { correlation_id: @correlation_id, skippable_tests: @skippable_tests } end |
#shutdown! ⇒ Object
309 310 311 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 309 def shutdown! @coverage_writer&.stop end |
#skippable?(datadog_test_id) ⇒ Boolean
277 278 279 280 281 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 277 def skippable?(datadog_test_id) return false if !enabled? || !skipping_tests? @mutex.synchronize { @skippable_tests.include?(datadog_test_id) } end |
#skippable_tests_count ⇒ Object
305 306 307 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 305 def skippable_tests_count skippable_tests.count end |
#skipping_tests? ⇒ Boolean
119 120 121 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 119 def skipping_tests? @test_skipping_enabled end |
#start_coverage ⇒ void
This method returns an undefined value.
Starts coverage collection. This is a low-level method that only starts the collector.
131 132 133 134 135 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 131 def start_coverage return if !enabled? || !code_coverage? coverage_collector&.start end |
#stop_coverage ⇒ Hash?
Stops coverage collection and returns raw coverage data. This is a low-level method that only stops the collector.
141 142 143 144 145 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 141 def stop_coverage return if !enabled? || !code_coverage? coverage_collector&.stop end |
#storage_key ⇒ Object
328 329 330 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 328 def storage_key FILE_STORAGE_KEY end |
#write_test_session_tags(test_session, skipped_tests_count) ⇒ Object
295 296 297 298 299 300 301 302 303 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 295 def (test_session, skipped_tests_count) return if !enabled? Datadog.logger.debug { "Finished optimised session with test skipping enabled: #{@test_skipping_enabled}" } Datadog.logger.debug { "#{skipped_tests_count} tests were skipped" } test_session.set_tag(Ext::Test::TAG_ITR_TESTS_SKIPPED, skipped_tests_count.positive?.to_s) test_session.set_tag(Ext::Test::TAG_ITR_TEST_SKIPPING_COUNT, skipped_tests_count) end |