add `Vagrant.version?` helper method

to check Vagrant for a version match without raising an error.
This commit is contained in:
Robert Schulze 2018-09-07 11:37:30 +02:00
parent 3ec55d2a51
commit 28771674d6
2 changed files with 22 additions and 2 deletions

View File

@ -195,6 +195,17 @@ module Vagrant
puts "be removed in the next version of Vagrant."
end
# This checks if Vagrant is installed in a specific version.
#
# Example:
#
# Vagrant.version?(">= 2.1.0")
#
def self.version?(*requirements)
req = Gem::Requirement.new(*requirements)
req.satisfied_by?(Gem::Version.new(VERSION))
end
# This allows a Vagrantfile to specify the version of Vagrant that is
# required. You can specify a list of requirements which will all be checked
# against the running Vagrant version.
@ -211,8 +222,7 @@ module Vagrant
logger = Log4r::Logger.new("vagrant::root")
logger.info("Version requirements from Vagrantfile: #{requirements.inspect}")
req = Gem::Requirement.new(*requirements)
if req.satisfied_by?(Gem::Version.new(VERSION))
if version?(*requirements)
logger.info(" - Version requirements satisfied!")
return
end

View File

@ -101,6 +101,16 @@ describe Vagrant do
end
end
describe "version?" do
it "should succeed if valid range" do
expect(described_class.version?(Vagrant::VERSION)).to be(true)
end
it "should not succeed if bad range" do
expect(described_class.version?("> #{Vagrant::VERSION}")).to be(false)
end
end
describe "original_env" do
before do
ENV["VAGRANT_OLD_ENV_foo"] = "test"