Skip to content

Commit 64f1517

Browse files
committed
refactor RSpec::Core::Example instrumentation to support checking if Example is skippable by test impact analysis
1 parent e8a361d commit 64f1517

File tree

2 files changed

+56
-23
lines changed

2 files changed

+56
-23
lines changed

lib/datadog/ci/contrib/rspec/example.rb

+45-23
Original file line numberDiff line numberDiff line change
@@ -21,38 +21,21 @@ def run(*args)
2121
return super if ::RSpec.configuration.dry_run? && !datadog_configuration[:dry_run_enabled]
2222
return super unless datadog_configuration[:enabled]
2323

24-
test_name = full_description.strip
25-
if metadata[:description].empty?
26-
# for unnamed it blocks this appends something like "example at ./spec/some_spec.rb:10"
27-
test_name << " #{description}"
28-
end
29-
30-
test_suite_description = fetch_top_level_example_group[:description]
31-
suite_name = "#{test_suite_description} at #{metadata[:example_group][:rerun_file_path]}"
32-
33-
# remove example group description from test name to avoid duplication
34-
test_name = test_name.sub(test_suite_description, "").strip
35-
36-
if ci_queue?
37-
suite_name = "#{suite_name} (ci-queue running example [#{test_name}])"
38-
ci_queue_test_span = test_visibility_component.start_test_suite(suite_name)
39-
end
24+
test_suite_span = test_visibility_component.start_test_suite(datadog_test_suite_name) if ci_queue?
4025

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

4429
test_retries_component.with_retries do
4530
test_visibility_component.trace_test(
46-
test_name,
47-
suite_name,
31+
datadog_test_name,
32+
datadog_test_suite_name,
4833
tags: {
4934
CI::Ext::Test::TAG_FRAMEWORK => Ext::FRAMEWORK,
5035
CI::Ext::Test::TAG_FRAMEWORK_VERSION => datadog_integration.version.to_s,
5136
CI::Ext::Test::TAG_SOURCE_FILE => Git::LocalRepository.relative_to_root(metadata[:file_path]),
5237
CI::Ext::Test::TAG_SOURCE_START => metadata[:line_number].to_s,
53-
CI::Ext::Test::TAG_PARAMETERS => Utils::TestRun.test_parameters(
54-
metadata: {"scoped_id" => metadata[:scoped_id]}
55-
)
38+
CI::Ext::Test::TAG_PARAMETERS => datadog_test_parameters
5639
},
5740
service: datadog_configuration[:service_name]
5841
) do |test_span|
@@ -87,8 +70,8 @@ def run(*args)
8770
end
8871
end
8972

90-
# this is a special case for ci-queue, we need to finish the test suite span
91-
ci_queue_test_span&.finish
73+
# this is a special case for ci-queue, we need to finish the test suite span created for a single test
74+
test_suite_span&.finish
9275

9376
# after retries are done, we can finally report the test to RSpec
9477
@skip_reporting = false
@@ -129,6 +112,45 @@ def datadog_configuration
129112
Datadog.configuration.ci[:rspec]
130113
end
131114

115+
def datadog_test_suite_description
116+
@datadog_test_suite_description ||= fetch_top_level_example_group[:description]
117+
end
118+
119+
def datadog_test_name
120+
return @datadog_test_name if defined?(@datadog_test_name)
121+
122+
test_name = full_description.strip
123+
if metadata[:description].empty?
124+
# for unnamed it blocks this appends something like "example at ./spec/some_spec.rb:10"
125+
test_name << " #{description}"
126+
end
127+
128+
# remove example group description from test name to avoid duplication
129+
test_name = test_name.sub(datadog_test_suite_description, "").strip
130+
131+
@datadog_test_name = test_name
132+
end
133+
134+
def datadog_test_suite_name
135+
return @datadog_test_suite_name if defined?(@datadog_test_suite_name)
136+
137+
suite_name = "#{datadog_test_suite_description} at #{metadata[:example_group][:rerun_file_path]}"
138+
139+
if ci_queue?
140+
suite_name = "#{suite_name} (ci-queue running example [#{datadog_test_name}])"
141+
end
142+
143+
@datadog_test_suite_name = suite_name
144+
end
145+
146+
def datadog_test_parameters
147+
return @datadog_test_parameters if defined?(@datadog_test_parameters)
148+
149+
@datadog_test_parameters = Utils::TestRun.test_parameters(
150+
metadata: {"scoped_id" => metadata[:scoped_id]}
151+
)
152+
end
153+
132154
def test_visibility_component
133155
Datadog.send(:components).test_visibility
134156
end

sig/datadog/ci/contrib/rspec/example.rbs

+11
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ module Datadog
77
module InstanceMethods : ::RSpec::Core::Example
88
@skip_reporting: bool
99

10+
@datadog_test_suite_description: String
11+
12+
@datadog_test_name: String
13+
@datadog_test_suite_name: String
14+
@datadog_test_parameters: String
15+
1016
def run: (untyped example_group_instance, untyped reporter) -> untyped
1117

1218
private
@@ -17,6 +23,11 @@ module Datadog
1723
def test_visibility_component: () -> Datadog::CI::TestVisibility::Component
1824
def test_retries_component: () -> Datadog::CI::TestRetries::Component
1925
def ci_queue?: () -> bool
26+
27+
def datadog_test_suite_description: () -> String
28+
def datadog_test_name: () -> String
29+
def datadog_test_suite_name: () -> String
30+
def datadog_test_parameters: () -> String
2031
end
2132
end
2233
end

0 commit comments

Comments
 (0)