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.
-
#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.
Class Method Summary collapse
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
- #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.
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 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 47 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.
36 37 38 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 36 def code_coverage_enabled @code_coverage_enabled end |
#correlation_id ⇒ Object (readonly)
Returns the value of attribute correlation_id.
36 37 38 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 36 def correlation_id @correlation_id end |
#enabled ⇒ Object (readonly)
Returns the value of attribute enabled.
36 37 38 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 36 def enabled @enabled end |
#skippable_suites ⇒ Object (readonly)
Returns the value of attribute skippable_suites.
36 37 38 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 36 def skippable_suites @skippable_suites end |
#skippable_tests ⇒ Object (readonly)
Returns the value of attribute skippable_tests.
36 37 38 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 36 def skippable_tests @skippable_tests end |
#test_skipping_enabled ⇒ Object (readonly)
Returns the value of attribute test_skipping_enabled.
36 37 38 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 36 def test_skipping_enabled @test_skipping_enabled end |
#test_skipping_mode ⇒ Object (readonly)
Returns the value of attribute test_skipping_mode.
36 37 38 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 36 def test_skipping_mode @test_skipping_mode end |
Class Method Details
.build(api:, discard_traces:, dd_env:, **options) ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 39 def self.build(api:, discard_traces:, dd_env:, **) coverage_writer = unless api.nil? || discard_traces AsyncWriter.new(transport: Coverage::Transport.new(api: api)) end new(api: api, coverage_writer: coverage_writer, dd_env: dd_env, **) 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).
273 274 275 276 277 278 279 280 281 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 273 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
148 149 150 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 148 def code_coverage? @code_coverage_enabled end |
#configure(remote_configuration, test_session) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 98 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.
287 288 289 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 287 def context_coverage_enabled? enabled? && code_coverage? && !suite_skipping_mode? && !@use_single_threaded_coverage end |
#enabled? ⇒ Boolean
128 129 130 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 128 def enabled? @enabled end |
#mark_if_skippable(test) ⇒ Object
303 304 305 306 307 308 309 310 311 312 313 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 303 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
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 315 def mark_if_suite_skippable(test_suite) return if !enabled? || !skipping_suites? unskippable = test_suite.itr_unskippable? Utils::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 Utils::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.
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 177 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
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 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 229 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
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 200 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
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 351 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
340 341 342 343 344 345 346 347 348 349 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 340 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
399 400 401 402 403 404 405 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 399 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
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 411 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
391 392 393 394 395 396 397 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 391 def serialize_state { correlation_id: @correlation_id, skippable_tests: @skippable_tests, skippable_suites: @skippable_suites } end |
#shutdown! ⇒ Object
386 387 388 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 386 def shutdown! @coverage_writer&.stop end |
#skippable?(datadog_test_id) ⇒ Boolean
291 292 293 294 295 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 291 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
297 298 299 300 301 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 297 def skippable_suite?(test_suite_name) return false if !enabled? || !skipping_suites? @mutex.synchronize { @skippable_suites.include?(test_suite_name) } end |
#skipping_suites? ⇒ Boolean
136 137 138 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 136 def skipping_suites? @test_skipping_enabled && suite_skipping_mode? end |
#skipping_tests? ⇒ Boolean
132 133 134 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 132 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.
156 157 158 159 160 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 156 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.
166 167 168 169 170 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 166 def stop_coverage return if !enabled? || !code_coverage? coverage_collector&.stop end |
#storage_key ⇒ Object
407 408 409 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 407 def storage_key FILE_STORAGE_KEY end |
#suite_skipping_mode? ⇒ Boolean
144 145 146 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 144 def suite_skipping_mode? @test_skipping_mode == Ext::Test::TIATestSkippingMode::SUITE end |
#test_skipping_mode? ⇒ Boolean
140 141 142 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 140 def test_skipping_mode? @test_skipping_mode == Ext::Test::TIATestSkippingMode::TEST end |
#write_test_session_tags(test_session, skipped_tests_count) ⇒ Object
376 377 378 379 380 381 382 383 384 |
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 376 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 |