Class: Datadog::CI::ImpactedTestsDetection::Component

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/ci/impacted_tests_detection/component.rb

Instance Method Summary collapse

Constructor Details

#initialize(enabled:) ⇒ Component

Returns a new instance of Component.



10
11
12
13
# File 'lib/datadog/ci/impacted_tests_detection/component.rb', line 10

def initialize(enabled:)
  @enabled = enabled
  @git_diff = Git::Diff.new
end

Instance Method Details

#configure(library_settings, test_session) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/datadog/ci/impacted_tests_detection/component.rb', line 15

def configure(library_settings, test_session)
  @enabled &&= library_settings.impacted_tests_enabled?

  return unless @enabled

  # we must unshallow the repository before trying to find base_commit_sha or executing `git diff` command
  git_tree_upload_worker.wait_until_done

  base_commit_sha = test_session.base_commit_sha || Git::LocalRepository.base_commit_sha
  if base_commit_sha.nil?
    Datadog.logger.debug { "Impacted tests detection disabled: base commit not found" }
    @enabled = false
    return
  end

  git_diff = Git::LocalRepository.get_changes_since(base_commit_sha)
  if git_diff.empty?
    Datadog.logger.debug { "Impacted tests detection disabled: could not get changed files" }
    @enabled = false
    return
  end

  Datadog.logger.debug do
    "Impacted tests detection: found #{git_diff.size} changed files"
  end
  Datadog.logger.debug do
    "Impacted tests detection: changed files: #{git_diff.inspect}"
  end

  @git_diff = git_diff
  @enabled = true
end

#enabled?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/datadog/ci/impacted_tests_detection/component.rb', line 48

def enabled?
  @enabled
end

#modified?(test_span) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/datadog/ci/impacted_tests_detection/component.rb', line 52

def modified?(test_span)
  return false unless enabled?

  source_file = test_span.source_file
  return false if source_file.nil?

  # convert to relative path without leading slash
  # @type var source_file: String
  source_file = source_file[1..] if source_file.start_with?("/")

  result = @git_diff.lines_changed?(source_file, start_line: test_span.start_line, end_line: test_span.end_line)
  Datadog.logger.debug do
    "Impacted tests detection: test #{test_span.name} with source file #{source_file} is modified: #{result}"
  end
  result
end

#tag_modified_test(test_span) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/datadog/ci/impacted_tests_detection/component.rb', line 69

def tag_modified_test(test_span)
  return unless modified?(test_span)

  Datadog.logger.debug do
    "Impacted tests detection: test #{test_span.name} with source file #{test_span.source_file} is modified"
  end

  test_span.set_tag(Ext::Test::TAG_TEST_IS_MODIFIED, "true")
end