Skip to content

Commit

Permalink
refactor: Remove references to loaded specs
Browse files Browse the repository at this point in the history
  • Loading branch information
arielvalentin authored Mar 30, 2022
1 parent fdfbcbb commit 28c7cbb
Show file tree
Hide file tree
Showing 23 changed files with 32 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base
private

def gem_version
Gem.loaded_specs['actionpack']&.version || ::ActionPack.version
::ActionPack.version
end

def patch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base
private

def gem_version
Gem.loaded_specs['actionview'].version || ::ActionView.version
::ActionView.version
end

def require_dependencies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base
private

def gem_version
Gem.loaded_specs['activejob']&.version || ::ActiveJob.version
::ActiveJob.version
end

def require_dependencies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,13 @@

describe '#compatible' do
it 'returns false for unsupported gem versions' do
Gem.stub(:loaded_specs, 'activejob' => Gem::Specification.new { |s| s.version = '4.2.0' }) do
::ActiveJob.stub(:version, Gem::Version.new('4.2.0')) do
_(instrumentation.compatible?).must_equal false
end
end

it 'returns true for supported gem versions' do
Gem.stub(:loaded_specs, 'activejob' => Gem::Specification.new { |s| s.version = minimum_version }) do
_(instrumentation.compatible?).must_equal true
end
_(instrumentation.compatible?).must_equal true
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base
private

def gem_version
Gem.loaded_specs['active_model_serializers']&.version || Gem::Version.new(::ActiveModel::Serializer::VERSION)
Gem::Version.new(::ActiveModel::Serializer::VERSION)
end

def require_dependencies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,11 @@

describe 'compatible' do
it 'when older gem version installed' do
allow_any_instance_of(Bundler::StubSpecification).to receive(:version).and_return(Gem::Version.new('0.9.4'))
stub_const('::ActiveModel::Serializer::VERSION', '0.9.4')
_(instrumentation.compatible?).must_equal false
end

it 'when future gem version installed' do
allow_any_instance_of(Bundler::StubSpecification).to receive(:version).and_return(Gem::Version.new('0.11.0'))
_(instrumentation.compatible?).must_equal true
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def insert_class_methods_supported?
end

def gem_version
Gem.loaded_specs['activerecord']&.version || ::ActiveRecord.version
::ActiveRecord.version
end

def patch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,25 @@

describe 'compatible' do
it 'when a version below the minimum supported gem version is installed' do
Gem.stub(:loaded_specs, 'activerecord' => Gem::Specification.new { |s| s.version = '4.2.0' }) do
ActiveRecord.stub(:version, Gem::Version.new('4.2.0')) do
_(instrumentation.compatible?).must_equal false
end
end

it 'when a version above the maximum supported gem version is installed' do
Gem.stub(:loaded_specs, 'activerecord' => Gem::Specification.new { |s| s.version = '8.0.0' }) do
ActiveRecord.stub(:version, Gem::Version.new('8.0.0')) do
_(instrumentation.compatible?).must_equal false
end
end

it 'it treats pre releases as being equivalent to a full release' do
Gem.stub(:loaded_specs, 'activerecord' => Gem::Specification.new { |s| s.version = '8.0.0.alpha' }) do
ActiveRecord.stub(:version, Gem::Version.new('8.0.0.alpha')) do
_(instrumentation.compatible?).must_equal false
end
end

it 'when supported gem version installed' do
Gem.stub(:loaded_specs, 'activerecord' => Gem::Specification.new { |s| s.version = minimum_version }) do
_(instrumentation.compatible?).must_equal true
end
_(instrumentation.compatible?).must_equal true
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base
option :suppress_internal_instrumentation, default: false, validate: :boolean

def gem_version
Gem.loaded_specs['aws-sdk']&.version || Gem.loaded_specs['aws-sdk-core']&.version || Gem::Version.new(::Aws::CORE_GEM_VERSION)
Gem::Version.new(::Aws::CORE_GEM_VERSION)
end

private
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,13 @@

describe '#compatible' do
it 'returns false for unsupported gem versions' do
Gem.stub(:loaded_specs, 'aws-sdk' => Gem::Specification.new { |s| s.version = '1.0.0' }) do
stub_const('::Aws::CORE_GEM_VERSION', Gem::Version.new('1.0.0')) do
_(instrumentation.compatible?).must_equal false
end
end

it 'returns true for supported gem versions' do
Gem.stub(:loaded_specs, 'aws-sdk' => Gem::Specification.new { |s| s.version = minimum_version }) do
_(instrumentation.compatible?).must_equal true
end
_(instrumentation.compatible?).must_equal true
end
end

Expand Down
1 change: 1 addition & 0 deletions instrumentation/aws_sdk/test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
require 'opentelemetry/sdk'

require 'minitest/autorun'
require 'rspec/mocks/minitest_integration'

# global opentelemetry-sdk setup:
EXPORTER = OpenTelemetry::SDK::Trace::Export::InMemorySpanExporter.new
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module Instrumentation
#
# # if the target library is present, is it compatible?
# compatible do
# (Gem.loaded_specs['sinatra']&.version || Gem::Version.new(Sinatra::VERSION)) > MIN_VERSION
# Gem::Version.new(Sinatra::VERSION)) > MIN_VERSION
# end
# end
# end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base
private

def gem_version
Gem.loaded_specs['mongo']&.version || Gem::Version.new(::Mongo::VERSION)
Gem::Version.new(::Mongo::VERSION)
end

def require_dependencies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,11 @@

describe 'compatible' do
it 'when older gem version installed' do
allow_any_instance_of(Bundler::StubSpecification).to receive(:version).and_return(Gem::Version.new('2.4.3'))
stub_const('::Mongo::VERSION', '2.4.3')
_(instrumentation.compatible?).must_equal false
end

it 'when future gem version installed' do
allow_any_instance_of(Bundler::StubSpecification).to receive(:version).and_return(Gem::Version.new('3.0.0'))
_(instrumentation.compatible?).must_equal true
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base
end

compatible do
Gem.loaded_specs['pg'].version > Gem::Version.new(MINIMUM_VERSION)
gem_version > Gem::Version.new(MINIMUM_VERSION)
end

option :peer_service, default: nil, validate: :string
Expand All @@ -48,7 +48,7 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base
private

def gem_version
Gem.loaded_specs['pg']&.version || Gem::Version.new(::PG::VERSION)
Gem::Version.new(::PG::VERSION)
end

def require_dependencies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def require_dependencies
end

def gem_version
Gem.loaded_specs['que']&.version || Gem::Version.new(::Que::VERSION)
Gem::Version.new(::Que::VERSION)
end

def patch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base
private

def gem_version
Gem.loaded_specs['actionpack'].version
::ActionPack.version
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base
private

def gem_version
Gem.loaded_specs['ruby-kafka']&.version || Gem::Version.new(Kafka::VERSION)
Gem::Version.new(Kafka::VERSION)
end

def require_patches
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,12 @@

describe 'compatible' do
it 'when older gem version installed' do
Gem.stub(:loaded_specs, 'ruby-kafka' => Gem::Specification.new { |s| s.version = '0.6.8' }) do
_(instrumentation.compatible?).must_equal false
end
stub_const('Kafka::VERSION', '0.6.8')
_(instrumentation.compatible?).must_equal false
end

it 'when future gem version installed' do
Gem.stub(:loaded_specs, 'ruby-kafka' => Gem::Specification.new { |s| s.version = '1.7.0' }) do
_(instrumentation.compatible?).must_equal true
end
_(instrumentation.compatible?).must_equal true
end

describe 'when the installing application bypasses RubyGems' do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base
private

def gem_version
Gem.loaded_specs['sidekiq']&.version || Gem::Version.new(::Sidekiq::VERSION)
Gem::Version.new(::Sidekiq::VERSION)
end

def require_dependencies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'bundler', '>= 1.17'
spec.add_development_dependency 'minitest', '~> 5.0'
spec.add_development_dependency 'opentelemetry-sdk', '~> 1.0'
spec.add_development_dependency 'rspec-mocks'
spec.add_development_dependency 'rubocop', '~> 0.73.0'
spec.add_development_dependency 'sidekiq', '~> 5.2.0'
spec.add_development_dependency 'simplecov', '~> 0.17.1'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@

describe 'compatible' do
it 'when older gem version installed' do
Gem.stub(:loaded_specs, 'sidekiq' => Gem::Specification.new { |s| s.version = '4.2.8' }) do
_(instrumentation.compatible?).must_equal false
end
stub_const('::Sidekiq::VERSION', '4.2.8')
_(instrumentation.compatible?).must_equal false
end

it 'when future gem version installed' do
Expand Down
1 change: 1 addition & 0 deletions instrumentation/sidekiq/test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
require 'opentelemetry/sdk'

require 'minitest/autorun'
require 'rspec/mocks/minitest_integration'
require 'helpers/mock_loader'
require 'active_job'
require 'pry'
Expand Down

0 comments on commit 28c7cbb

Please sign in to comment.