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

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

Instance Method Summary collapse

Instance Method Details

#runObject



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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/datadog/ci/contrib/rspec/example.rb', line 18

def run(*)
  return super unless datadog_configuration[:enabled]

  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

  test_suite_description = fetch_top_level_example_group[:description]
  suite_name = "#{test_suite_description} at #{[:example_group][:rerun_file_path]}"

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

  if ci_queue?
    suite_name += " (ci-queue running example [#{test_name}])"
    test_suite_span = CI.start_test_suite(suite_name)
  end

  CI.trace_test(
    test_name,
    suite_name,
    tags: {
      CI::Ext::Test::TAG_FRAMEWORK => Ext::FRAMEWORK,
      CI::Ext::Test::TAG_FRAMEWORK_VERSION => CI::Contrib::RSpec::Integration.version.to_s,
      CI::Ext::Test::TAG_SOURCE_FILE => Utils::Git.relative_to_root([:file_path]),
      CI::Ext::Test::TAG_SOURCE_START => [:line_number].to_s
    },
    service: datadog_configuration[:service_name]
  ) do |test_span|
    result = super

    if test_span
      test_span.set_parameters({}, {"scoped_id" => [:scoped_id]})

      case execution_result.status
      when :passed
        test_span.passed!
        test_suite_span.passed! if test_suite_span
      when :failed
        test_span.failed!(exception: execution_result.exception)
        test_suite_span.failed! if test_suite_span
      else
        # :pending or nil
        test_span.skipped!(
          reason: execution_result.pending_message,
          exception: execution_result.pending_exception
        )

        test_suite_span.skipped! if test_suite_span
      end
    end

    test_suite_span.finish if test_suite_span

    result
  end
end