Module: Datadog::CI::Contrib::RSpec::ExampleGroup::ClassMethods

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

Overview

Instance methods for configuration

Instance Method Summary collapse

Instance Method Details

#run(reporter = ::RSpec::Core::NullReporter) ⇒ Object



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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/datadog/ci/contrib/rspec/example_group.rb', line 18

def run(reporter = ::RSpec::Core::NullReporter)
  return super if ::RSpec.configuration.dry_run? && !datadog_configuration[:dry_run_enabled]
  return super unless datadog_configuration[:enabled]

  # skip the context hooks if all descendant example are going to be skipped
  # IMPORTANT: must happen before top_level? check because skipping hooks must happen
  # even if it is a nested context
  [:skip] = true if all_examples_skipped_by_datadog?

  # Start context coverage for this example group (for TIA suite-level coverage).
  # This captures code executed in before(:context)/before(:all) hooks.
  # The context_id uses scoped_id which is a stable identifier for RSpec example groups.
  context_id = datadog_context_id
  start_context_coverage(context_id)

  begin
    # return early because we create Datadog::CI::TestSuite only for top-level example groups
    return super unless top_level?

    suite_name = "#{description} at #{file_path}"
    suite_tags = {
      CI::Ext::Test::TAG_SOURCE_FILE => Git::LocalRepository.relative_to_root([:file_path]),
      CI::Ext::Test::TAG_SOURCE_START => [:line_number].to_s
    }

    test_suite =
      test_visibility_component&.start_test_suite(
        suite_name,
        tags: suite_tags,
        service: datadog_configuration[:service_name]
      )

    success = super

    return success unless test_suite

    test_suite.finish

    success
  ensure
    clear_context_coverage(context_id)
  end
end