Skip to content

Commit

Permalink
as ActiveSupport::TestCase overrides test_order, use presence of Mini…
Browse files Browse the repository at this point in the history
…test::Parallel::Test in ancestors chain to detect parallelized Runnable
  • Loading branch information
anmarchenko committed Jan 3, 2024
1 parent 1c278b7 commit 452b59d
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ module Datadog
module CI
module Contrib
module Minitest
# Minitest integration constants
# TODO: mark as `@public_api` when GA, to protect from resource and tag name changes.
module Suite
def self.name(klass, method_name)
module Helpers
def self.test_suite_name(klass, method_name)
source_location, = klass.instance_method(method_name).source_location
source_file_path = Pathname.new(source_location.to_s).relative_path_from(Pathname.pwd).to_s

"#{klass.name} at #{source_file_path}"
end

def self.parallel?(klass)
klass.ancestors.include?(::Minitest::Parallel::Test)
end
end
end
end
Expand Down
12 changes: 4 additions & 8 deletions lib/datadog/ci/contrib/minitest/hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require_relative "../../ext/test"
require_relative "ext"
require_relative "suite"
require_relative "helpers"

module Datadog
module CI
Expand All @@ -16,8 +16,8 @@ def before_setup

test_name = "#{class_name}##{name}"

test_suite_name = Suite.name(self.class, name)
if parallel?
test_suite_name = Helpers.test_suite_name(self.class, name)
if Helpers.parallel?(self.class)
test_suite_name = "#{test_suite_name} (#{name} concurrently)"

# for parallel execution we need to start a new test suite for each test
Expand All @@ -41,7 +41,7 @@ def after_teardown
return super unless test_span

finish_test(test_span, result_code)
if parallel?
if Helpers.parallel?(self.class)
finish_test_suite(test_span.test_suite, result_code)
end

Expand Down Expand Up @@ -78,10 +78,6 @@ def finish_with_result(span, result_code)
span.finish
end

def parallel?
self.class.test_order == :parallel
end

def datadog_configuration
Datadog.configuration.ci[:minitest]
end
Expand Down
10 changes: 3 additions & 7 deletions lib/datadog/ci/contrib/minitest/runnable.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require_relative "suite"
require_relative "helpers"

module Datadog
module CI
Expand All @@ -12,12 +12,12 @@ def self.included(base)
module ClassMethods
def run(*)
return super unless datadog_configuration[:enabled]
return super if parallel?
return super if Helpers.parallel?(self)

method = runnable_methods.first
return super if method.nil?

test_suite_name = Suite.name(self, method)
test_suite_name = Helpers.test_suite_name(self, method)

test_suite = Datadog::CI.start_test_suite(test_suite_name)
test_suite.passed! # will be overridden if any test fails
Expand All @@ -31,10 +31,6 @@ def run(*)

private

def parallel?
test_order == :parallel
end

def datadog_configuration
Datadog.configuration.ci[:minitest]
end
Expand Down
13 changes: 13 additions & 0 deletions sig/datadog/ci/contrib/minitest/helpers.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Datadog
module CI
module Contrib
module Minitest
module Helpers
def self.test_suite_name: (untyped klass, String? method_name) -> ::String

def self.parallel?: (untyped klass) -> bool
end
end
end
end
end
2 changes: 0 additions & 2 deletions sig/datadog/ci/contrib/minitest/hooks.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ module Datadog

private

def parallel?: () -> bool

def datadog_configuration: () -> untyped

def finish_test: (Datadog::CI::Test test_span, String result_code) -> void
Expand Down
2 changes: 0 additions & 2 deletions sig/datadog/ci/contrib/minitest/runnable.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ module Datadog

private

def parallel?: () -> bool

def datadog_configuration: () -> untyped

def test_order: () -> (nil | :parallel | :random | :sorted | :alpha)
Expand Down
11 changes: 0 additions & 11 deletions sig/datadog/ci/contrib/minitest/suite.rbs

This file was deleted.

0 comments on commit 452b59d

Please sign in to comment.