Skip to content

Commit

Permalink
rspec skips context hooks if all descendant examples are skipped by d…
Browse files Browse the repository at this point in the history
…atadog
  • Loading branch information
anmarchenko committed Dec 4, 2024
1 parent ee52609 commit b2fe2bb
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 12 deletions.
16 changes: 12 additions & 4 deletions lib/datadog/ci/contrib/rspec/example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,18 @@ def finish(reporter)
super(::RSpec::Core::NullReporter)
end

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

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

private

def fetch_top_level_example_group
Expand Down Expand Up @@ -151,10 +163,6 @@ def datadog_test_parameters
)
end

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

def test_visibility_component
Datadog.send(:components).test_visibility
end
Expand Down
19 changes: 13 additions & 6 deletions lib/datadog/ci/contrib/rspec/example_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,16 @@ def run(reporter = ::RSpec::Core::NullReporter)
return super unless top_level?

suite_name = "#{description} at #{file_path}"
test_suite = test_visibility_component.start_test_suite(
test_suite = test_visibility_component&.start_test_suite(
suite_name,
tags: {
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
}
)

# try skipping the whole example group
# all_skipped = descendant_filtered_examples.all? do |example|
# end
all_skipped = false
metadata[:skip] = true if all_skipped
# skip the context hooks if all descendant example are going to be skipped
metadata[:skip] = true if all_examples_skipped_by_datadog?

success = super
return success unless test_suite
Expand All @@ -53,13 +50,23 @@ def run(reporter = ::RSpec::Core::NullReporter)

private

def all_examples_skipped_by_datadog?
descendant_filtered_examples.all? do |example|
!example.datadog_unskippable? && test_optimisation_component&.skippable?(example)
end
end

def datadog_configuration
Datadog.configuration.ci[:rspec]
end

def test_visibility_component
Datadog.send(:components).test_visibility
end

def test_optimisation_component
Datadog.send(:components).test_optimisation
end
end
end
end
Expand Down
5 changes: 4 additions & 1 deletion sig/datadog/ci/contrib/rspec/example.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ module Datadog

@datadog_test_suite_description: String

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

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

def datadog_test_id: () -> String
def datadog_unskippable?: () -> bool

private

def fetch_top_level_example_group: () -> Hash[Symbol, untyped]
Expand All @@ -28,7 +32,6 @@ module Datadog
def datadog_test_name: () -> String
def datadog_test_suite_name: () -> String
def datadog_test_parameters: () -> String
def datadog_unskippable?: () -> bool
end
end
end
Expand Down
6 changes: 5 additions & 1 deletion sig/datadog/ci/contrib/rspec/example_group.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ module Datadog

private

def all_examples_skipped_by_datadog?: () -> bool

def datadog_configuration: () -> untyped

def test_visibility_component: () -> Datadog::CI::TestVisibility::Component
def test_visibility_component: () -> Datadog::CI::TestVisibility::Component?

def test_optimisation_component: () -> Datadog::CI::TestOptimisation::Component?
end
end
end
Expand Down
1 change: 1 addition & 0 deletions vendor/rbs/rspec/0/rspec.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ module RSpec::Core::ExampleGroup
def file_path: () -> String
def description: () -> String
def metadata: () -> untyped
def descendant_filtered_examples: () -> Array[untyped]
end
end

Expand Down

0 comments on commit b2fe2bb

Please sign in to comment.