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_suites ⇒ Object
readonly
Returns the value of attribute skippable_suites.
-
#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.
-
#test_skipping_mode ⇒ Object
readonly
Returns the value of attribute test_skipping_mode.
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, test_skipping_mode: Ext::Test::TIATestSkippingMode::TEST, 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
- #mark_if_suite_skippable(test_suite) ⇒ 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.
- #on_test_suite_finished(test_suite, context) ⇒ Object
- #on_test_suite_started(test_suite) ⇒ Object
- #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_suite?(test_suite_name) ⇒ Boolean
- #skippables_count ⇒ Object
- #skipping_suites? ⇒ Boolean
- #skipping_tests? ⇒ Boolean
-
#start_coverage ⇒ void
Starts coverage collection.
-
#stop_coverage ⇒ Hash?
Stops coverage collection and returns raw coverage data.
- #storage_key ⇒ Object
- #suite_skipping_mode? ⇒ Boolean
- #test_skipping_mode? ⇒ Boolean
- #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, test_skipping_mode: Ext::Test::TIATestSkippingMode::TEST, bundle_location: nil, use_single_threaded_coverage: false, use_allocation_tracing: true, static_dependencies_tracking_enabled: false) ⇒ Component
Returns a new instance of Component.
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 83 84 85 86 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 37 def initialize( dd_env:, config_tags: {}, api: nil, coverage_writer: nil, enabled: false, test_skipping_mode: Ext::Test::TIATestSkippingMode::TEST, 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 = || {} @test_skipping_mode = test_skipping_mode @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 @skippable_suites = 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.
34 35 36 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 34 def code_coverage_enabled @code_coverage_enabled end |
#correlation_id ⇒ Object (readonly)
Returns the value of attribute correlation_id.
34 35 36 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 34 def correlation_id @correlation_id end |
#enabled ⇒ Object (readonly)
Returns the value of attribute enabled.
34 35 36 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 34 def enabled @enabled end |
#skippable_suites ⇒ Object (readonly)
Returns the value of attribute skippable_suites.
34 35 36 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 34 def skippable_suites @skippable_suites end |
#skippable_tests ⇒ Object (readonly)
Returns the value of attribute skippable_tests.
34 35 36 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 34 def skippable_tests @skippable_tests end |
#skippable_tests_fetch_error ⇒ Object (readonly)
Returns the value of attribute skippable_tests_fetch_error.
34 35 36 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 34 def skippable_tests_fetch_error @skippable_tests_fetch_error end |
#test_skipping_enabled ⇒ Object (readonly)
Returns the value of attribute test_skipping_enabled.
34 35 36 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 34 def test_skipping_enabled @test_skipping_enabled end |
#test_skipping_mode ⇒ Object (readonly)
Returns the value of attribute test_skipping_mode.
34 35 36 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 34 def test_skipping_mode @test_skipping_mode 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).
263 264 265 266 267 268 269 270 271 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 263 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
138 139 140 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 138 def code_coverage? @code_coverage_enabled end |
#configure(remote_configuration, test_session) ⇒ Object
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 114 115 116 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 88 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) test_session.set_tag(Ext::Test::TAG_ITR_TEST_SKIPPING_TYPE, @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? || skipping_suites? return if load_component_state fetch_skippables(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.
277 278 279 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 277 def context_coverage_enabled? enabled? && code_coverage? && !suite_skipping_mode? && !@use_single_threaded_coverage end |
#enabled? ⇒ Boolean
118 119 120 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 118 def enabled? @enabled end |
#mark_if_skippable(test) ⇒ Object
293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 293 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 |
#mark_if_suite_skippable(test_suite) ⇒ Object
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 305 def mark_if_suite_skippable(test_suite) return if !enabled? || !skipping_suites? unskippable = test_suite.itr_unskippable? Telemetry.itr_unskippable if unskippable unless skippable_suite?(test_suite.name) Datadog.logger.debug { "Test suite is not skippable: #{test_suite.name}" } return end if unskippable Telemetry.itr_forced_run test_suite.set_tag(Ext::Test::TAG_ITR_FORCED_RUN, "true") Datadog.logger.debug { "Forced run of skippable test suite: #{test_suite.name}" } return end test_suite.set_tag(Ext::Test::TAG_ITR_SKIPPED_BY_ITR, "true") test_suite.skipped!(reason: Ext::Test::SkipReason::TEST_IMPACT_ANALYSIS) Datadog.logger.debug { "Marked test suite as skippable: #{test_suite.name}" } 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.
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 167 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
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 253 254 255 256 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 219 def on_test_finished(test, context) return unless enabled? if suite_skipping_mode? test.test_suite&.add_impacted_files(test.lock_custom_impacted_files) return end # 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) write_coverage_event( test_id: test.id.to_s, test_suite_id: test.test_suite_id.to_s, test_session_id: test.test_session_id.to_s, source_file: test.source_file, coverage: coverage, custom_impacted_files: test.lock_custom_impacted_files ) 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
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 190 def on_test_started(test) inherit_suite_impacted_files(test) if test_skipping_mode? return if !enabled? || !code_coverage? return if suite_skipping_mode? # 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 |
#on_test_suite_finished(test_suite, context) ⇒ Object
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 341 def on_test_suite_finished(test_suite, context) return unless enabled? && suite_skipping_mode? if test_suite.skipped_by_test_impact_analysis? Telemetry.itr_skipped context.incr_tests_skipped_by_tia_count return end return unless code_coverage? Telemetry.code_coverage_finished(test_suite) coverage = coverage_collector&.stop write_coverage_event( test_id: nil, test_suite_id: test_suite.id.to_s, test_session_id: test_suite.get_tag(Ext::Test::TAG_TEST_SESSION_ID).to_s, source_file: test_suite.source_file, coverage: coverage, custom_impacted_files: test_suite.lock_custom_impacted_files ) end |
#on_test_suite_started(test_suite) ⇒ Object
330 331 332 333 334 335 336 337 338 339 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 330 def on_test_suite_started(test_suite) return unless enabled? && suite_skipping_mode? mark_if_suite_skippable(test_suite) return if test_suite.should_skip? return unless code_coverage? Telemetry.code_coverage_started(test_suite) coverage_collector&.start end |
#restore_state(state) ⇒ Object
393 394 395 396 397 398 399 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 393 def restore_state(state) set_skippables( correlation_id: state[:correlation_id], tests: state[:skippable_tests] || Set.new, suites: state[:skippable_suites] || Set.new ) end |
#restore_state_from_datadog_test_runner ⇒ Object
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 405 def restore_state_from_datadog_test_runner Datadog.logger.debug { "Restoring skippables from Test Optimization cache" } skippables_data = load_cached_skippable_tests if skippables_data.nil? Datadog.logger.debug { "Restoring skippables failed, will request again" } return false end Datadog.logger.debug { "Restored skippables from Test Optimization: #{skippables_data}" } skippable_response = Skippable::Response.from_json(skippables_data) apply_skippable_response(skippable_response) Datadog.logger.debug { "Found [#{skippables_count}] skippable #{@test_skipping_mode}s from context" } Datadog.logger.debug { "ITR correlation ID from context: #{@correlation_id}" } true end |
#serialize_state ⇒ Object
Implementation of Stateful interface
385 386 387 388 389 390 391 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 385 def serialize_state { correlation_id: @correlation_id, skippable_tests: @skippable_tests, skippable_suites: @skippable_suites } end |
#shutdown! ⇒ Object
380 381 382 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 380 def shutdown! @coverage_writer&.stop end |
#skippable?(datadog_test_id) ⇒ Boolean
281 282 283 284 285 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 281 def skippable?(datadog_test_id) return false if !enabled? || !skipping_tests? @mutex.synchronize { @skippable_tests.include?(datadog_test_id) } end |
#skippable_suite?(test_suite_name) ⇒ Boolean
287 288 289 290 291 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 287 def skippable_suite?(test_suite_name) return false if !enabled? || !skipping_suites? @mutex.synchronize { @skippable_suites.include?(test_suite_name) } end |
#skippables_count ⇒ Object
376 377 378 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 376 def skippables_count current_skippables.count end |
#skipping_suites? ⇒ Boolean
126 127 128 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 126 def skipping_suites? @test_skipping_enabled && suite_skipping_mode? end |
#skipping_tests? ⇒ Boolean
122 123 124 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 122 def skipping_tests? @test_skipping_enabled && test_skipping_mode? end |
#start_coverage ⇒ void
This method returns an undefined value.
Starts coverage collection. This is a low-level method that only starts the collector.
146 147 148 149 150 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 146 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.
156 157 158 159 160 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 156 def stop_coverage return if !enabled? || !code_coverage? coverage_collector&.stop end |
#storage_key ⇒ Object
401 402 403 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 401 def storage_key FILE_STORAGE_KEY end |
#suite_skipping_mode? ⇒ Boolean
134 135 136 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 134 def suite_skipping_mode? @test_skipping_mode == Ext::Test::TIATestSkippingMode::SUITE end |
#test_skipping_mode? ⇒ Boolean
130 131 132 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 130 def test_skipping_mode? @test_skipping_mode == Ext::Test::TIATestSkippingMode::TEST end |
#write_test_session_tags(test_session, skipped_tests_count) ⇒ Object
366 367 368 369 370 371 372 373 374 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 366 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 |