core: BoxMetadata#version can constrain by providers

This commit is contained in:
Mitchell Hashimoto 2014-01-23 11:52:42 -08:00
parent 7d44cd61c9
commit 80194cde35
2 changed files with 29 additions and 7 deletions

View File

@ -45,15 +45,19 @@ module Vagrant
# be a constraint. # be a constraint.
# @return [Version] The matching version or nil if a matching # @return [Version] The matching version or nil if a matching
# version was not found. # version was not found.
def version(version) def version(version, **opts)
requirements = version.split(",").map do |v| requirements = version.split(",").map do |v|
Gem::Requirement.new(v.strip) Gem::Requirement.new(v.strip)
end end
providers = nil
providers = Array(opts[:provider]) if opts[:provider]
@version_map.keys.sort.reverse.each do |v| @version_map.keys.sort.reverse.each do |v|
if requirements.all? { |r| r.satisfied_by?(v) } next if !requirements.all? { |r| r.satisfied_by?(v) }
return Version.new(@version_map[v]) version = Version.new(@version_map[v])
end next if (providers & version.providers).empty? if providers
return version
end end
nil nil

View File

@ -12,13 +12,24 @@ describe Vagrant::BoxMetadata do
"description": "bar", "description": "bar",
"versions": [ "versions": [
{ {
"version": "1.0.0" "version": "1.0.0",
"providers": [
{ "name": "virtualbox" },
{ "name": "vmware" }
]
}, },
{ {
"version": "1.1.5" "version": "1.1.5",
"providers": [
{ "name": "virtualbox" }
]
}, },
{ {
"version": "1.1.0" "version": "1.1.0",
"providers": [
{ "name": "virtualbox" },
{ "name": "vmware" }
]
} }
] ]
} }
@ -64,6 +75,13 @@ describe Vagrant::BoxMetadata do
expect(result).to be_kind_of(Vagrant::BoxMetadata::Version) expect(result).to be_kind_of(Vagrant::BoxMetadata::Version)
expect(result.version).to eq("1.0.0") expect(result.version).to eq("1.0.0")
end end
it "matches the constraint that has the given provider" do
result = subject.version(">= 0", provider: "vmware")
expect(result).to_not be_nil
expect(result).to be_kind_of(Vagrant::BoxMetadata::Version)
expect(result.version).to eq("1.1.0")
end
end end
describe "#versions" do describe "#versions" do