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

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

Instance Method Summary collapse

Instance Method Details

#datadog_test_idObject



92
93
94
95
96
97
98
# File 'lib/datadog/ci/contrib/rspec/example.rb', line 92

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

#datadog_unskippable?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/datadog/ci/contrib/rspec/example.rb', line 100

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

#finish(reporter) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/datadog/ci/contrib/rspec/example.rb', line 81

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



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
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
# File 'lib/datadog/ci/contrib/rspec/example.rb', line 20

def run(*args)
  return super if ::RSpec.configuration.dry_run? && !datadog_configuration[:dry_run_enabled]
  return super unless datadog_configuration[: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

  test_retries_component.with_retries do
    test_visibility_component.trace_test(
      datadog_test_name,
      datadog_test_suite_name,
      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
      },
      service: datadog_configuration[:service_name]
    ) do |test_span|
      test_span&.itr_unskippable! if datadog_unskippable?

      [:skip] = CI::Ext::Test::ITR_TEST_SKIP_REASON if test_span&.skipped_by_itr?

      # 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, we don't fail the test run
        @exception = nil if test_span&.any_retry_passed?
      else
        # :pending or nil
        test_span&.skipped!(
          reason: execution_result.pending_message,
          exception: execution_result.pending_exception
        )
      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