Class: Datadog::CI::TestImpactAnalysis::Component

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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.



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
83
84
85
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 36

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 = 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_enabledObject (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_idObject (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

#enabledObject (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_suitesObject (readonly)

Returns the value of attribute skippable_suites.



33
34
35
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 33

def skippable_suites
  @skippable_suites
end

#skippable_testsObject (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_errorObject (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_enabledObject (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

#test_skipping_modeObject (readonly)

Returns the value of attribute test_skipping_mode.



33
34
35
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 33

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).

Parameters:

  • context_id (String)

    The context ID to clear



255
256
257
258
259
260
261
262
263
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 255

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

Returns:

  • (Boolean)


137
138
139
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 137

def code_coverage?
  @code_coverage_enabled
end

#configure(remote_configuration, test_session) ⇒ Object



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
114
115
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 87

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.

Returns:

  • (Boolean)


269
270
271
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 269

def context_coverage_enabled?
  enabled? && code_coverage? && !suite_skipping_mode? && !@use_single_threaded_coverage
end

#enabled?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 117

def enabled?
  @enabled
end

#mark_if_skippable(test) ⇒ Object



285
286
287
288
289
290
291
292
293
294
295
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 285

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



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 297

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.

Parameters:

  • context_id (String)

    A stable identifier for the context (e.g., RSpec scoped_id)



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 166

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:

  1. Stops test coverage collection

  2. Merges context coverage from all relevant contexts

  3. Writes the combined coverage event

  4. Records ITR statistics if test was skipped by TIA

Parameters:

Returns:



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
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 216

def on_test_finished(test, context)
  return unless enabled?
  return if suite_skipping_mode?

  # 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
  )
end

#on_test_started(test) ⇒ void

This method returns an undefined value.

Called when a test starts within a context. This method:

  1. Stops any in-progress context coverage collection and stores it

  2. Starts coverage collection for the test itself

Parameters:



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 189

def on_test_started(test)
  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



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 333

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
  )
end

#on_test_suite_started(test_suite) ⇒ Object



322
323
324
325
326
327
328
329
330
331
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 322

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



384
385
386
387
388
389
390
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 384

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_runnerObject



396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 396

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_stateObject

Implementation of Stateful interface



376
377
378
379
380
381
382
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 376

def serialize_state
  {
    correlation_id: @correlation_id,
    skippable_tests: @skippable_tests,
    skippable_suites: @skippable_suites
  }
end

#shutdown!Object



371
372
373
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 371

def shutdown!
  @coverage_writer&.stop
end

#skippable?(datadog_test_id) ⇒ Boolean

Returns:

  • (Boolean)


273
274
275
276
277
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 273

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

Returns:

  • (Boolean)


279
280
281
282
283
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 279

def skippable_suite?(test_suite_name)
  return false if !enabled? || !skipping_suites?

  @mutex.synchronize { @skippable_suites.include?(test_suite_name) }
end

#skippables_countObject



367
368
369
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 367

def skippables_count
  current_skippables.count
end

#skipping_suites?Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 125

def skipping_suites?
  @test_skipping_enabled && suite_skipping_mode?
end

#skipping_tests?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 121

def skipping_tests?
  @test_skipping_enabled && test_skipping_mode?
end

#start_coveragevoid

This method returns an undefined value.

Starts coverage collection. This is a low-level method that only starts the collector.



145
146
147
148
149
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 145

def start_coverage
  return if !enabled? || !code_coverage?

  coverage_collector&.start
end

#stop_coverageHash?

Stops coverage collection and returns raw coverage data. This is a low-level method that only stops the collector.

Returns:

  • (Hash, nil)

    Raw coverage data or nil



155
156
157
158
159
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 155

def stop_coverage
  return if !enabled? || !code_coverage?

  coverage_collector&.stop
end

#storage_keyObject



392
393
394
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 392

def storage_key
  FILE_STORAGE_KEY
end

#suite_skipping_mode?Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 133

def suite_skipping_mode?
  @test_skipping_mode == Ext::Test::TIATestSkippingMode::SUITE
end

#test_skipping_mode?Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 129

def test_skipping_mode?
  @test_skipping_mode == Ext::Test::TIATestSkippingMode::TEST
end

#write_test_session_tags(test_session, skipped_tests_count) ⇒ Object



357
358
359
360
361
362
363
364
365
# File 'lib/datadog/ci/test_impact_analysis/component.rb', line 357

def write_test_session_tags(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