Module: Datadog::CI::Contrib::RSpec::Example::InstanceMethods

Defined in:
lib/datadog/ci/contrib/rspec/example.rb

Instance Method Summary collapse

Instance Method Details

#datadog_context_idsObject

Returns list of context IDs for this example, from outermost to innermost. Used for merging context-level coverage into test coverage.



191
192
193
194
# File 'lib/datadog/ci/contrib/rspec/example.rb', line 191

def datadog_context_ids
  traverse_example_group_hierarchy unless defined?(@datadog_context_ids)
  @datadog_context_ids
end

#datadog_fqn_test_idObject



139
140
141
142
143
144
# File 'lib/datadog/ci/contrib/rspec/example.rb', line 139

def datadog_fqn_test_id
  @datadog_fqn_test_id ||= Utils::TestRun.datadog_test_id(
    datadog_test_name,
    datadog_test_suite_name
  )
end

#datadog_test_idObject



131
132
133
134
135
136
137
# File 'lib/datadog/ci/contrib/rspec/example.rb', line 131

def datadog_test_id
  @datadog_test_id ||= Utils::TestRun.datadog_test_id(
    datadog_test_name,
    datadog_test_suite_name,
    datadog_test_parameters
  )
end

#datadog_test_nameObject



146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/datadog/ci/contrib/rspec/example.rb', line 146

def datadog_test_name
  return @datadog_test_name if defined?(@datadog_test_name)

  test_name = full_description.strip
  if [:description].empty?
    # for unnamed it blocks this appends something like "example at ./spec/some_spec.rb:10"
    test_name << " #{description}"
  end

  # remove example group description from test name to avoid duplication
  test_name = test_name.sub(datadog_test_suite_description, "").strip

  @datadog_test_name = test_name
end

#datadog_test_parametersObject



173
174
175
176
177
178
179
# File 'lib/datadog/ci/contrib/rspec/example.rb', line 173

def datadog_test_parameters
  return @datadog_test_parameters if defined?(@datadog_test_parameters)

  @datadog_test_parameters = Utils::TestRun.test_parameters(
    metadata: {"scoped_id" => [:scoped_id]}
  )
end

#datadog_test_suite_nameObject



161
162
163
164
165
166
167
168
169
170
171
# File 'lib/datadog/ci/contrib/rspec/example.rb', line 161

def datadog_test_suite_name
  return @datadog_test_suite_name if defined?(@datadog_test_suite_name)

  suite_name = "#{datadog_test_suite_description} at #{metadata[:example_group][:rerun_file_path]}"

  if ci_queue?
    suite_name = "#{suite_name} (ci-queue running example [#{datadog_test_name}])"
  end

  @datadog_test_suite_name = suite_name
end

#datadog_test_suite_source_file_pathObject



185
186
187
# File 'lib/datadog/ci/contrib/rspec/example.rb', line 185

def datadog_test_suite_source_file_path
  Git::LocalRepository.relative_to_root([:rerun_file_path])
end

#datadog_unskippable?Boolean

Returns:

  • (Boolean)


181
182
183
# File 'lib/datadog/ci/contrib/rspec/example.rb', line 181

def datadog_unskippable?
  !![CI::Ext::Test::ITR_UNSKIPPABLE_OPTION]
end

#finish(reporter) ⇒ Object



120
121
122
123
124
125
126
127
128
129
# File 'lib/datadog/ci/contrib/rspec/example.rb', line 120

def finish(reporter)
  # By default finish test but do not report it to RSpec::Core::Reporter
  # it is going to be reported once after retries are done.
  #
  # We need to do this because RSpec breaks when we try to report the same example multiple times with different
  # results.
  return super unless @skip_reporting

  super(::RSpec::Core::NullReporter)
end

#run(*args) ⇒ Object



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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/datadog/ci/contrib/rspec/example.rb', line 21

def run(*args)
  return super unless datadog_configuration[:enabled]
  return super if ::RSpec.configuration.dry_run? && !datadog_configuration[:dry_run_enabled]

  test_suite_span = test_visibility_component.start_test_suite(datadog_test_suite_name) if ci_queue?

  # don't report test to RSpec::Core::Reporter until retries are done
  @skip_reporting = true

  # @type var tags : Hash[String, String]
  tags = {
    CI::Ext::Test::TAG_FRAMEWORK => Ext::FRAMEWORK,
    CI::Ext::Test::TAG_FRAMEWORK_VERSION => datadog_integration.version.to_s,
    CI::Ext::Test::TAG_SOURCE_FILE => Git::LocalRepository.relative_to_root([:file_path]),
    CI::Ext::Test::TAG_SOURCE_START => [:line_number].to_s,
    CI::Ext::Test::TAG_PARAMETERS => datadog_test_parameters
  }

  end_line = SourceCode::MethodInspect.last_line(@example_block)
  tags[CI::Ext::Test::TAG_SOURCE_END] = end_line.to_s if end_line

  # we keep track of the last test failure if we encounter any
  test_failure = nil

  test_retries_component.with_retries do
    test_visibility_component.trace_test(
      datadog_test_name,
      datadog_test_suite_name,
      tags: tags,
      service: datadog_configuration[:service_name]
    ) do |test_span|
      # Set context IDs on the test span for TIA context coverage merging
      test_span&.context_ids = datadog_context_ids

      test_span&.itr_unskippable! if datadog_unskippable?

      [:skip] = test_span&.datadog_skip_reason if test_span&.should_skip?

      # before each run remove any previous exception
      @exception = nil

      result = super

      # When test job is canceled and RSpec is quitting we don't want to report the last test
      # before RSpec context unwinds. This test might have some unrelated errors that we don't want to
      # see in Datadog.
      return result if ::RSpec.world.wants_to_quit

      case execution_result.status
      when :passed
        test_span&.passed!
      when :failed
        test_span&.failed!(exception: execution_result.exception)

        # if any of the retries passed or test is quarantined, we don't fail the test run
        @exception = nil if test_span&.should_ignore_failures?
        test_failure = @exception
      else
        # :pending or nil
        test_span&.skipped!(
          reason: execution_result.pending_message,
          exception: execution_result.pending_exception
        )
      end

      if datadog_configuration[:datadog_formatter_enabled]
        if test_span&.is_retry?
          [Ext::] ||= 0
          [Ext::] ||= Hash.new(0)

          [Ext::] += 1
          [Ext::] = test_span&.retry_reason
          [Ext::][test_span&.status] += 1
        end

        [Ext::] = true if test_span&.quarantined?
        [Ext::] = true if test_span&.disabled?

        if test_span&.skipped_by_test_impact_analysis?
          [Ext::] = true
        end
      end

      # at this point if we have encountered any test failure in any of the previous retries
      # we restore the @exception internal state if we should not skip failures for this run
      if test_failure && !test_span&.should_ignore_failures?
        @exception = test_failure
      end
    end
  end

  # this is a special case for ci-queue, we need to finish the test suite span created for a single test
  test_suite_span&.finish

  # after retries are done, we can finally report the test to RSpec
  @skip_reporting = false
  finish(reporter)
end