Skip to content

Commit

Permalink
change how minitest instrumentation works: instrument CompositeReport…
Browse files Browse the repository at this point in the history
…er as it does not get overwritten when using minitest-reporters gem
  • Loading branch information
anmarchenko committed Jan 22, 2024
1 parent e885530 commit 76a9b7e
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 68 deletions.
2 changes: 1 addition & 1 deletion Steepfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ target :lib do
library "rspec"
library "cucumber"
library "msgpack"
library "weakref"
library "ci_queue"
end
7 changes: 2 additions & 5 deletions lib/datadog/ci/contrib/minitest/patcher.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require_relative "reporter"
require_relative "hooks"
require_relative "runnable"

Expand All @@ -18,13 +19,9 @@ def target_version
end

def patch
require_relative "plugin"

::Minitest::CompositeReporter.include(Reporter)
::Minitest::Test.include(Hooks)
::Minitest.include(Plugin)
::Minitest::Runnable.include(Runnable)

::Minitest.extensions << "datadog_ci"
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,23 @@ module Datadog
module CI
module Contrib
module Minitest
module Plugin
module Reporter
def self.included(base)
base.extend(ClassMethods)
base.prepend(InstanceMethods)
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
module InstanceMethods
def report(*)
return super unless datadog_configuration[:enabled]

res = super

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?
return res if active_test_session.nil? || active_test_module.nil?

if @reporter.passed?
if passed?
active_test_module.passed!
active_test_session.passed!
else
Expand All @@ -39,13 +36,11 @@ def report
active_test_module.finish
active_test_session.finish

nil
res
end
end

module ClassMethods
def plugin_datadog_ci_init(*)
return unless datadog_configuration[:enabled]
def start(*)
return super unless datadog_configuration[:enabled]

test_session = CI.start_test_session(
tags: {
Expand All @@ -56,7 +51,7 @@ def plugin_datadog_ci_init(*)
)
CI.start_test_module(test_session.name) if test_session

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

private
Expand Down
31 changes: 0 additions & 31 deletions sig/datadog/ci/contrib/minitest/plugin.rbs

This file was deleted.

21 changes: 21 additions & 0 deletions sig/datadog/ci/contrib/minitest/reporter.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Datadog
module CI
module Contrib
module Minitest
module Reporter
def self.included: (untyped base) -> untyped

class InstanceMethods < ::Minitest::AbstractReporter
def report: (*untyped) -> untyped

def start: (*untyped) -> untyped

private

def datadog_configuration: () -> untyped
end
end
end
end
end
end
10 changes: 3 additions & 7 deletions spec/datadog/ci/contrib/minitest/patcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,10 @@
end
end

context "Minitest includes plugin" do
let(:minitest) { Minitest }
context "Minitest::CompositeReporter is patched" do
let(:reporter) { Minitest::CompositeReporter }
it "has a custom bases" do
expect(minitest.ancestors).to include(Datadog::CI::Contrib::Minitest::Plugin)
end

it "has datadog_ci extension" do
expect(minitest.extensions).to include("datadog_ci")
expect(reporter.ancestors).to include(Datadog::CI::Contrib::Minitest::Reporter)
end
end
end
Expand Down
5 changes: 5 additions & 0 deletions vendor/rbs/ci_queue/0/minitest.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Minitest
end

module Minitest::Queue
end
6 changes: 0 additions & 6 deletions vendor/rbs/weakref/0/weakref.rbs

This file was deleted.

0 comments on commit 76a9b7e

Please sign in to comment.