diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e484cad2..71641293e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ BUG FIXES: - core: Can add boxes with spaces in their path. [GH-3306] - core: Prerelease plugins installed are locked to that prerelease version. [GH-3301] + - core: Better error message when adding a box with a malformed version. [GH-3332] - commands/box: Show versions when listing. [GH-3316] - commands/status: Machine readable output contains the target. [GH-3218] - guests/arch: Reload udev rules after network change. [GH-3322] diff --git a/lib/vagrant/box_metadata.rb b/lib/vagrant/box_metadata.rb index 81c040e4b..fe8e62cea 100644 --- a/lib/vagrant/box_metadata.rb +++ b/lib/vagrant/box_metadata.rb @@ -31,12 +31,14 @@ module Vagrant @name = @raw["name"] @description = @raw["description"] @version_map = (@raw["versions"] || []).map do |v| - [Gem::Version.new(v["version"]), v] + begin + [Gem::Version.new(v["version"]), v] + rescue ArgumentError + raise Errors::BoxMetadataMalformedVersion, + version: v["version"].to_s + end end @version_map = Hash[@version_map] - - # TODO: check for corruption: - # - malformed version end # Returns data about a single version that is included in this diff --git a/lib/vagrant/errors.rb b/lib/vagrant/errors.rb index 1aa277b05..03c6578fa 100644 --- a/lib/vagrant/errors.rb +++ b/lib/vagrant/errors.rb @@ -172,6 +172,10 @@ module Vagrant error_key(:box_metadata_malformed) end + class BoxMetadataMalformedVersion < VagrantError + error_key(:box_metadata_malformed_version) + end + class BoxNotFound < VagrantError error_key(:box_not_found) end diff --git a/templates/locales/en.yml b/templates/locales/en.yml index f84bb7580..bcf2b24d1 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -392,6 +392,11 @@ en: that this issue can be fixed. %{error} + box_metadata_malformed_version: |- + A version of the box you're loading is formatted in a way that + Vagrant cannot parse: '%{version}'. Please reformat the version + to be properly formatted. It should be in the format of + X.Y.Z. box_not_found: |- The box '%{name}' does not exist. Please double check and try again. You can see the boxes that are installed with diff --git a/test/unit/vagrant/box_metadata_test.rb b/test/unit/vagrant/box_metadata_test.rb index 0acb0ad48..de3950c3b 100644 --- a/test/unit/vagrant/box_metadata_test.rb +++ b/test/unit/vagrant/box_metadata_test.rb @@ -61,6 +61,25 @@ describe Vagrant::BoxMetadata do end end + context "with poorly formatted version" do + let(:raw) { + <<-RAW + { "name": "foo", + "versions": [ + { + "version": "I AM NOT VALID" + } + ] + } + RAW + } + + it "raises an exception" do + expect { subject }. + to raise_error(Vagrant::Errors::BoxMetadataMalformedVersion) + end + end + describe "#version" do it "matches an exact version" do result = subject.version("1.0.0")