Skip to content

Commit

Permalink
require minitest plugin only when patching and avoid issues with refe…
Browse files Browse the repository at this point in the history
…rencing Minitest
  • Loading branch information
anmarchenko committed Dec 27, 2023
1 parent 5afea37 commit 59c0ebd
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 37 deletions.
1 change: 0 additions & 1 deletion Steepfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ target :lib do
check "lib"

ignore "lib/datadog/ci/configuration/settings.rb"
ignore "lib/datadog/ci/contrib/minitest/plugin.rb"

library "pathname"
library "json"
Expand Down
3 changes: 2 additions & 1 deletion lib/datadog/ci/contrib/minitest/patcher.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

require_relative "hooks"
require_relative "plugin"

module Datadog
module CI
Expand All @@ -18,6 +17,8 @@ def target_version
end

def patch
require_relative "plugin"

::Minitest::Test.include(Hooks)
::Minitest.include(Plugin)

Expand Down
60 changes: 30 additions & 30 deletions lib/datadog/ci/contrib/minitest/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,35 @@ def self.included(base)
base.extend(ClassMethods)
end

class DatadogReporter < ::Minitest::AbstractReporter
def initialize(minitest_reporter)
# This creates circular reference as minitest_reporter also holds reference to DatadogReporter.
# To make sure that minitest_reporter can be garbage collected, we use WeakRef.
@reporter = WeakRef.new(minitest_reporter)
end

def report
active_test_session = CI.active_test_session
active_test_module = CI.active_test_module

return unless @reporter.weakref_alive?
return if active_test_session.nil? || active_test_module.nil?

if @reporter.passed?
active_test_module.passed!
active_test_session.passed!
else
active_test_module.failed!
active_test_session.failed!
end

active_test_module.finish
active_test_session.finish

nil
end
end

module ClassMethods
def plugin_datadog_ci_init(*)
return unless datadog_configuration[:enabled]
Expand All @@ -28,36 +57,7 @@ def plugin_datadog_ci_init(*)
)
CI.start_test_module(test_session.name)

# we create dynamic class here to avoid referencing ::Minitest constant
# in datadog-ci class definitions because Minitest is not always available
datadog_reporter_klass = Class.new(::Minitest::AbstractReporter) do
def initialize(reporter)
# This creates circular reference as reporter holds reference to this reporter.
# To make sure that reporter can be garbage collected, we use WeakRef.
@reporter = WeakRef.new(reporter)
end

def report
active_test_session = CI.active_test_session
active_test_module = CI.active_test_module

return unless @reporter.weakref_alive?
return if active_test_session.nil? || active_test_module.nil?

if @reporter.passed?
active_test_module.passed!
active_test_session.passed!
else
active_test_module.failed!
active_test_session.failed!
end

active_test_module.finish
active_test_session.finish
end
end

reporter.reporters << datadog_reporter_klass.new(reporter)
reporter.reporters << DatadogReporter.new(reporter)
end

private
Expand Down
2 changes: 1 addition & 1 deletion lib/datadog/ci/contrib/rspec/integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def self.version
end

def self.loaded?
!defined?(::RSpec).nil? && !defined?(::RSpec::Core).nil? && \
!defined?(::RSpec).nil? && !defined?(::RSpec::Core).nil? &&
!defined?(::RSpec::Core::Example).nil?
end

Expand Down
2 changes: 1 addition & 1 deletion lib/datadog/ci/contrib/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def [](name)
end

def []=(name, value)
respond_to?("#{name}=") ? send("#{name}=", value) : set_option(name, value)
respond_to?(:"#{name}=") ? send(:"#{name}=", value) : set_option(name, value)
end
end
end
Expand Down
9 changes: 7 additions & 2 deletions sig/datadog/ci/contrib/minitest/plugin.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,21 @@ module Datadog
module Plugin
def self.included: (untyped base) -> untyped

module ClassMethods
attr_reader reporter: WeakRef
class DatadogReporter < ::Minitest::AbstractReporter
@reporter: WeakRef

def initialize: (Minitest::AbstractReporter reporter) -> void
end

module ClassMethods
def plugin_datadog_ci_init: (*untyped) -> (nil | untyped)

def initialize: (untyped reporter) -> void

def report: () -> (nil | untyped)

def reporter: () -> Minitest::CompositeReporter

private

def datadog_configuration: () -> untyped
Expand Down
2 changes: 1 addition & 1 deletion spec/datadog/ci/contrib/minitest/instrumentation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def self.name
end

num_tests.times do |i|
define_method("test_#{i}") {}
define_method(:"test_#{i}") {}
end
end

Expand Down

0 comments on commit 59c0ebd

Please sign in to comment.