Extract ruby version checks into a lib/ library
This commit is contained in:
Родитель
dc8e6b9e1a
Коммит
ba8ea8d3c1
|
@ -2,12 +2,13 @@
|
|||
|
||||
ENV["BUNDLE_GEMFILE"] = File.expand_path("../Gemfile", File.dirname(__FILE__))
|
||||
require "bundler/setup"
|
||||
require "ruby-version-check"
|
||||
|
||||
# Note that contracts.ruby has two specific ruby-version specific libraries, which we have vendored into lib/
|
||||
if (Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("3.0.0")) # ruby3
|
||||
$LOAD_PATH.unshift(File.expand_path(File.join(__dir__, "../lib/contracts-ruby3/lib")))
|
||||
else # ruby2
|
||||
if RubyVersionCheck.ruby_version2?
|
||||
$LOAD_PATH.unshift(File.expand_path(File.join(__dir__, "../lib/contracts-ruby2/lib")))
|
||||
else
|
||||
$LOAD_PATH.unshift(File.expand_path(File.join(__dir__, "../lib/contracts-ruby3/lib")))
|
||||
end
|
||||
|
||||
require "contracts"
|
||||
|
|
|
@ -1,29 +1,13 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module Entitlements
|
||||
# Allows maintaining version compatibility with older versions of Ruby
|
||||
# :nocov:
|
||||
def self.ruby_version2?
|
||||
@ruby_version2 ||= (
|
||||
Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.0.0") &&
|
||||
Gem::Version.new(RUBY_VERSION) < Gem::Version.new("3.0.0")
|
||||
)
|
||||
end
|
||||
|
||||
def self.ruby_version3?
|
||||
@ruby_version3 ||= (Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("3.0.0"))
|
||||
end
|
||||
# :nocov:
|
||||
end
|
||||
|
||||
# Hey there! With our use of the "contracts" module, load order is important.
|
||||
|
||||
# Load third party dependencies first.
|
||||
require "concurrent"
|
||||
require "ruby-version-check"
|
||||
|
||||
# Note that contracts.ruby has two specific ruby-version specific libraries, which we have vendored into lib/
|
||||
# :nocov:
|
||||
if Entitlements.ruby_version2?
|
||||
if RubyVersionCheck.ruby_version2?
|
||||
$LOAD_PATH.unshift(File.expand_path(File.join(__dir__, "contracts-ruby2/lib")))
|
||||
else
|
||||
$LOAD_PATH.unshift(File.expand_path(File.join(__dir__, "contracts-ruby3/lib")))
|
||||
|
|
|
@ -163,7 +163,7 @@ module Entitlements
|
|||
# :nocov:
|
||||
Contract C::None => C::HashOf[String => C::Any]
|
||||
def parsed_data
|
||||
@parsed_data ||= if Entitlements.ruby_version2?
|
||||
@parsed_data ||= if RubyVersionCheck.ruby_version2?
|
||||
::YAML.load(File.read(filename)).to_h
|
||||
else
|
||||
::YAML.load(File.read(filename), permitted_classes: [Date]).to_h
|
||||
|
|
|
@ -79,7 +79,7 @@ module Entitlements
|
|||
Entitlements.logger.debug "Loading people from #{filename.inspect}"
|
||||
|
||||
# :nocov:
|
||||
raw_person_data = if Entitlements.ruby_version2?
|
||||
raw_person_data = if RubyVersionCheck.ruby_version2?
|
||||
::YAML.load(File.read(filename)).to_h
|
||||
else
|
||||
::YAML.load(File.read(filename), permitted_classes: [Date]).to_h
|
||||
|
|
|
@ -58,7 +58,7 @@ module Entitlements
|
|||
person_dn_format: config.fetch("person_dn_format")
|
||||
}
|
||||
opts[:disable_ssl_verification] = true if config.fetch("disable_ssl_verification", false)
|
||||
if Entitlements.ruby_version2?
|
||||
if RubyVersionCheck.ruby_version2?
|
||||
Entitlements::Service::LDAP.new_with_cache(opts)
|
||||
else
|
||||
Entitlements::Service::LDAP.new_with_cache(**opts)
|
||||
|
|
|
@ -33,7 +33,7 @@ module Entitlements
|
|||
end
|
||||
|
||||
# :nocov:
|
||||
if Entitlements.ruby_version2?
|
||||
if RubyVersionCheck.ruby_version2?
|
||||
::YAML.load(File.read(manager_map_file)).to_h
|
||||
else
|
||||
::YAML.load(File.read(manager_map_file), permitted_classes: [Date]).to_h
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module RubyVersionCheck
|
||||
# Allows maintaining version compatibility with older versions of Ruby
|
||||
# :nocov:
|
||||
def self.ruby_version2?
|
||||
@ruby_version2 ||= (
|
||||
Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.0.0") &&
|
||||
Gem::Version.new(RUBY_VERSION) < Gem::Version.new("3.0.0")
|
||||
)
|
||||
end
|
||||
|
||||
def self.ruby_version3?
|
||||
@ruby_version3 ||= (Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("3.0.0"))
|
||||
end
|
||||
# :nocov:
|
||||
end
|
|
@ -103,7 +103,7 @@ describe Entitlements::Data::Groups::Calculated::Ruby do
|
|||
|
||||
it "raises an error when an unexpected data structure is created" do
|
||||
filename = fixture("ldap-config/filters/filter-bad-data-structure.rb")
|
||||
if Entitlements.ruby_version2?
|
||||
if RubyVersionCheck.ruby_version2?
|
||||
expect { described_class.new(filename: filename) }.to raise_error(ParamContractError)
|
||||
else
|
||||
expect { described_class.new(filename: filename) }.to raise_error(ReturnContractError)
|
||||
|
|
|
@ -1,12 +1,13 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require "ruby-version-check"
|
||||
require "base64"
|
||||
|
||||
# Note that contracts.ruby has two specific ruby-version specific libraries, which we have vendored into lib/
|
||||
if (Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("3.0.0")) # ruby3
|
||||
$LOAD_PATH.unshift(File.expand_path(File.join(__dir__, "../../lib/contracts-ruby3/lib")))
|
||||
else # ruby2
|
||||
if RubyVersionCheck.ruby_version2?
|
||||
$LOAD_PATH.unshift(File.expand_path(File.join(__dir__, "../../lib/contracts-ruby2/lib")))
|
||||
else
|
||||
$LOAD_PATH.unshift(File.expand_path(File.join(__dir__, "../../lib/contracts-ruby3/lib")))
|
||||
end
|
||||
|
||||
require "contracts"
|
||||
|
|
Загрузка…
Ссылка в новой задаче