From ab9f91568ee3538019e8a4d7d862b97d4b9acb03 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 1 May 2014 09:46:40 -0700 Subject: [PATCH] core: more intuitive logic around default providers --- lib/vagrant/environment.rb | 8 ++++++-- test/unit/vagrant/environment_test.rb | 22 +++++++++++++++++++++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/lib/vagrant/environment.rb b/lib/vagrant/environment.rb index 19975b4ec..ea5d77b15 100644 --- a/lib/vagrant/environment.rb +++ b/lib/vagrant/environment.rb @@ -249,16 +249,20 @@ module Vagrant end # This returns the provider name for the default provider for this - # environment. The provider returned is currently hardcoded to "virtualbox" - # but one day should be a detected valid, best-case provider for this # environment. # # @return [Symbol] Name of the default provider. def default_provider(**opts) + opts[:force_default] = true if !opts.has_key?(:force_default) + default = ENV["VAGRANT_DEFAULT_PROVIDER"] default = nil if default == "" default = default.to_sym if default + # If we're forcing the default, just short-circuit and return + # that (the default behavior) + return default if default && opts[:force_default] + ordered = [] Vagrant.plugin("2").manager.providers.each do |key, data| impl = data[0] diff --git a/test/unit/vagrant/environment_test.rb b/test/unit/vagrant/environment_test.rb index 20255e5b7..ae3199bdf 100644 --- a/test/unit/vagrant/environment_test.rb +++ b/test/unit/vagrant/environment_test.rb @@ -717,7 +717,7 @@ VF end end - it "is the default provider if it is usable" do + it "is the default provider set if usable" do plugin_providers[:foo] = [provider_usable_class(true), { priority: 5 }] plugin_providers[:bar] = [provider_usable_class(true), { priority: 7 }] plugin_providers[:baz] = [provider_usable_class(true), { priority: 2 }] @@ -728,6 +728,26 @@ VF end end + it "is the default provider set even if unusable" do + plugin_providers[:baz] = [provider_usable_class(false), { priority: 5 }] + plugin_providers[:foo] = [provider_usable_class(true), { priority: 5 }] + plugin_providers[:bar] = [provider_usable_class(true), { priority: 7 }] + + with_temp_env("VAGRANT_DEFAULT_PROVIDER" => "baz") do + expect(subject.default_provider).to eq(:baz) + end + end + + it "is the usable despite default if not forced" do + plugin_providers[:baz] = [provider_usable_class(false), { priority: 5 }] + plugin_providers[:foo] = [provider_usable_class(true), { priority: 5 }] + plugin_providers[:bar] = [provider_usable_class(true), { priority: 7 }] + + with_temp_env("VAGRANT_DEFAULT_PROVIDER" => "baz") do + expect(subject.default_provider(force_default: false)).to eq(:bar) + end + end + it "is VirtualBox if nothing else is usable" do plugin_providers[:foo] = [provider_usable_class(false), { priority: 5 }] plugin_providers[:bar] = [provider_usable_class(false), { priority: 5 }]