Skip to content

Commit

Permalink
refactor RSpec::Core::Example instrumentation to support checking if …
Browse files Browse the repository at this point in the history
…Example is skippable by test impact analysis
  • Loading branch information
anmarchenko committed Dec 4, 2024
1 parent e8a361d commit 64f1517
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 23 deletions.
68 changes: 45 additions & 23 deletions lib/datadog/ci/contrib/rspec/example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,38 +21,21 @@ def run(*args)
return super if ::RSpec.configuration.dry_run? && !datadog_configuration[:dry_run_enabled]
return super unless datadog_configuration[:enabled]

test_name = full_description.strip
if metadata[: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 #{metadata[: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 = "#{suite_name} (ci-queue running example [#{test_name}])"
ci_queue_test_span = test_visibility_component.start_test_suite(suite_name)
end
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(
test_name,
suite_name,
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(metadata[:file_path]),
CI::Ext::Test::TAG_SOURCE_START => metadata[:line_number].to_s,
CI::Ext::Test::TAG_PARAMETERS => Utils::TestRun.test_parameters(
metadata: {"scoped_id" => metadata[:scoped_id]}
)
CI::Ext::Test::TAG_PARAMETERS => datadog_test_parameters
},
service: datadog_configuration[:service_name]
) do |test_span|
Expand Down Expand Up @@ -87,8 +70,8 @@ def run(*args)
end
end

# this is a special case for ci-queue, we need to finish the test suite span
ci_queue_test_span&.finish
# 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
Expand Down Expand Up @@ -129,6 +112,45 @@ def datadog_configuration
Datadog.configuration.ci[:rspec]
end

def datadog_test_suite_description
@datadog_test_suite_description ||= fetch_top_level_example_group[:description]
end

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

test_name = full_description.strip
if metadata[: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

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

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

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

def test_visibility_component
Datadog.send(:components).test_visibility
end
Expand Down
11 changes: 11 additions & 0 deletions sig/datadog/ci/contrib/rspec/example.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ module Datadog
module InstanceMethods : ::RSpec::Core::Example
@skip_reporting: bool

@datadog_test_suite_description: String

@datadog_test_name: String
@datadog_test_suite_name: String
@datadog_test_parameters: String

def run: (untyped example_group_instance, untyped reporter) -> untyped

private
Expand All @@ -17,6 +23,11 @@ module Datadog
def test_visibility_component: () -> Datadog::CI::TestVisibility::Component
def test_retries_component: () -> Datadog::CI::TestRetries::Component
def ci_queue?: () -> bool

def datadog_test_suite_description: () -> String
def datadog_test_name: () -> String
def datadog_test_suite_name: () -> String
def datadog_test_parameters: () -> String
end
end
end
Expand Down

0 comments on commit 64f1517

Please sign in to comment.