Skip to content

Commit

Permalink
extract Knapsack integration from RSpec
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Nov 7, 2024
1 parent acc48cb commit a0b41cc
Show file tree
Hide file tree
Showing 15 changed files with 253 additions and 189 deletions.
27 changes: 27 additions & 0 deletions lib/datadog/ci/contrib/knapsack/extension.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

require "knapsack_pro/extensions/rspec_extension"

require_relative "runner"

module Datadog
module CI
module Contrib
module Knapsack
module Extension
def self.included(base)
base.singleton_class.prepend(ClassMethods)
end

module ClassMethods
def setup!
super

::RSpec::Core::Runner.include(Datadog::CI::Contrib::Knapsack::Runner)
end
end
end
end
end
end
end
51 changes: 51 additions & 0 deletions lib/datadog/ci/contrib/knapsack/integration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# frozen_string_literal: true

require_relative "../integration"
require_relative "patcher"

module Datadog
module CI
module Contrib
module Knapsack
# Knapsack Pro test runner instrumentation
# https://github.com/KnapsackPro/knapsack_pro-ruby
class Integration
include Datadog::CI::Contrib::Integration

Configuration = Struct.new(:enabled)

MINIMUM_VERSION = Gem::Version.new("7.0.0")

register_as :knapsack

def self.version
Gem.loaded_specs["knapsack_pro"]&.version
end

def self.loaded?
!defined?(::KnapsackPro).nil? && !defined?(::KnapsackPro::Extensions::RSpecExtension).nil? &&
!defined?(::KnapsackPro::Extensions::RSpecExtension::Runner).nil?
end

def self.compatible?
super && version >= MINIMUM_VERSION
end

# test environments should not auto instrument test libraries
def auto_instrument?
false
end

# TODO: not every integration needs a configuration
def new_configuration
Integration::Configuration.new(true)
end

def patcher
Patcher
end
end
end
end
end
end
33 changes: 33 additions & 0 deletions lib/datadog/ci/contrib/knapsack/patcher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

require_relative "../patcher"

module Datadog
module CI
module Contrib
module Knapsack
module Patcher
include Datadog::CI::Contrib::Patcher

module_function

def target_version
Integration.version
end

def patch
if ::RSpec::Core::Runner.ancestors.include?(::KnapsackPro::Extensions::RSpecExtension::Runner)
# knapsack already patched rspec runner
require_relative "runner"
::RSpec::Core::Runner.include(Datadog::CI::Contrib::Knapsack::Runner)
else
# knapsack didn't patch rspec runner yet
require_relative "extension"
::KnapsackPro::Extensions::RSpecExtension.include(Datadog::CI::Contrib::Knapsack::Extension)
end
end
end
end
end
end
end
61 changes: 61 additions & 0 deletions lib/datadog/ci/contrib/knapsack/runner.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# frozen_string_literal: true

require_relative "../../../ext/test"
require_relative "../ext"

module Datadog
module CI
module Contrib
module Knapsack
module Runner
def self.included(base)
base.prepend(InstanceMethods)
end

module InstanceMethods
# TODO: this is coupled to RSpec integration being present
def knapsack__run_specs(*args)
return super if ::RSpec.configuration.dry_run? && !datadog_configuration[:dry_run_enabled]
return super unless datadog_configuration[:enabled]

test_session = test_visibility_component.start_test_session(
tags: {
CI::Ext::Test::TAG_FRAMEWORK => CI::Contrib::RSpec::Ext::FRAMEWORK,
CI::Ext::Test::TAG_FRAMEWORK_VERSION => CI::Contrib::RSpec::Integration.version.to_s
},
service: datadog_configuration[:service_name]
)

test_module = test_visibility_component.start_test_module(CI::Contrib::RSpec::Ext::FRAMEWORK)

result = super
return result unless test_module && test_session

if result != 0
test_module.failed!
test_session.failed!
else
test_module.passed!
test_session.passed!
end
test_module.finish
test_session.finish

result
end

private

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

def test_visibility_component
Datadog.send(:components).test_visibility
end
end
end
end
end
end
end
29 changes: 0 additions & 29 deletions lib/datadog/ci/contrib/rspec/knapsack_pro/extension.rb

This file was deleted.

26 changes: 0 additions & 26 deletions lib/datadog/ci/contrib/rspec/knapsack_pro/patcher.rb

This file was deleted.

62 changes: 0 additions & 62 deletions lib/datadog/ci/contrib/rspec/knapsack_pro/runner.rb

This file was deleted.

15 changes: 0 additions & 15 deletions lib/datadog/ci/contrib/rspec/patcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,6 @@ def patch
::RSpec::Queue::Runner.include(Runner)
end

if knapsack_pro?
# Knapsack Pro test runner instrumentation
# https://github.com/KnapsackPro/knapsack_pro-ruby
require_relative "knapsack_pro/patcher"
Datadog::CI::Contrib::RSpec::KnapsackPro::Patcher.patch
end

# default rspec test runner instrumentation
::RSpec::Core::Runner.include(Runner)

Expand All @@ -44,14 +37,6 @@ def patch
def ci_queue?
!!defined?(::RSpec::Queue::Runner)
end

def knapsack_pro?
knapsack_version = Gem.loaded_specs["knapsack_pro"]&.version

# additional instrumentation is needed for KnapsackPro version 7 and later
!!defined?(::KnapsackPro) &&
!knapsack_version.nil? && knapsack_version >= Gem::Version.new("7")
end
end
end
end
Expand Down
17 changes: 17 additions & 0 deletions sig/datadog/ci/contrib/knapsack/extension.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Datadog
module CI
module Contrib
module Knapsack
module Extension
def self.included: (untyped base) -> untyped

module ClassMethods
include ::KnapsackPro::Extensions::RSpecExtension

def setup!: () -> void
end
end
end
end
end
end
30 changes: 30 additions & 0 deletions sig/datadog/ci/contrib/knapsack/integration.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module Datadog
module CI
module Contrib
module Knapsack
class Integration
extend Datadog::CI::Contrib::Integration::ClassMethods
include Datadog::CI::Contrib::Integration::InstanceMethods

class Configuration
def initialize: (bool enabled) -> void
end

MINIMUM_VERSION: Gem::Version

def self.version: () -> untyped

def self.loaded?: () -> bool

def self.compatible?: () -> bool

def auto_instrument?: () -> bool

def new_configuration: () -> Configuration

def patcher: () -> singleton(Patcher)
end
end
end
end
end
11 changes: 11 additions & 0 deletions sig/datadog/ci/contrib/knapsack/patcher.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Datadog
module CI
module Contrib
module Knapsack
module Patcher
def self.patch: () -> void
end
end
end
end
end
Loading

0 comments on commit a0b41cc

Please sign in to comment.