From 01ec72cac2c51e0f52dd33aab45506086e2de226 Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Fri, 7 Dec 2018 13:30:50 -0800 Subject: [PATCH] Introduce a local and global check for enabled experimental features --- bin/vagrant | 6 ++-- lib/vagrant/util/experimental.rb | 23 +++++++++++-- test/unit/vagrant/util/experimental_test.rb | 36 ++++++++++++++++++--- 3 files changed, 54 insertions(+), 11 deletions(-) diff --git a/bin/vagrant b/bin/vagrant index 9acbcb024..efe5eab63 100755 --- a/bin/vagrant +++ b/bin/vagrant @@ -166,13 +166,11 @@ begin # 0 - Disables experimental features # 1 - Enables all features # String - Enables one or more features, separated by commas - if Vagrant::Util::Experimental.global_enabled? + if Vagrant::Util::Experimental.enabled? experimental = Vagrant::Util::Experimental.features_requested ui = Vagrant::UI::Prefixed.new(env.ui, "vagrant") logger.debug("Experimental flag is enabled") - if experimental.size >= 1 && experimental.first != "1" - ui.warn(I18n.t("vagrant.general.experimental.features", features: experimental.join(", ")), bold: true, prefix: true, channel: :error) - else + if Vagrant::Util::Experimental.global_enabled? ui.warn(I18n.t("vagrant.general.experimental.all"), bold: true, prefix: true, channel: :error) end end diff --git a/lib/vagrant/util/experimental.rb b/lib/vagrant/util/experimental.rb index e1200678e..9a2c6fb48 100644 --- a/lib/vagrant/util/experimental.rb +++ b/lib/vagrant/util/experimental.rb @@ -4,10 +4,11 @@ module Vagrant VALID_FEATURES = [] class << self - # A method for determining if the experimental flag has been enabled + # A method for determining if the experimental flag has been enabled with + # any features # # @return [Boolean] - def global_enabled? + def enabled? if !defined?(@_experimental) experimental = features_requested if experimental.size >= 1 && experimental.first != "0" @@ -19,8 +20,24 @@ module Vagrant @_experimental end + # A method for determining if all experimental features have been enabled + # by either a global enabled value "1" or all features explicitly enabled. + # + # @return [Boolean] + def global_enabled? + if !defined?(@_global_enabled) + experimental = features_requested + if experimental.size == 1 && experimental.first == "1" || (experimental.size > 0 && experimental.sort == VALID_FEATURES.sort) + @_global_enabled = true + else + @_global_enabled = false + end + end + @_global_enabled + end + # A method for Vagrant internals to determine if a given feature - # has been abled and can be used. + # has been abled by the user, is a valid feature flag and can be used. # # @param [String] feature # @return [Boolean] - A hash containing the original array and if it is valid diff --git a/test/unit/vagrant/util/experimental_test.rb b/test/unit/vagrant/util/experimental_test.rb index 5a0f17ad4..20b197b48 100644 --- a/test/unit/vagrant/util/experimental_test.rb +++ b/test/unit/vagrant/util/experimental_test.rb @@ -7,13 +7,41 @@ describe Vagrant::Util::Experimental do before(:each) { described_class.reset! } subject { described_class } + describe "#enabled?" do + it "returns true if enabled with '1'" do + allow(ENV).to receive(:[]).with("VAGRANT_EXPERIMENTAL").and_return("1") + expect(subject.enabled?).to eq(true) + end + + it "returns true if enabled with a list of features" do + allow(ENV).to receive(:[]).with("VAGRANT_EXPERIMENTAL").and_return("list,of,features") + expect(subject.enabled?).to eq(true) + end + + it "returns false if disabled" do + allow(ENV).to receive(:[]).with("VAGRANT_EXPERIMENTAL").and_return("0") + expect(subject.enabled?).to eq(false) + end + + it "returns false if not set" do + allow(ENV).to receive(:[]).with("VAGRANT_EXPERIMENTAL").and_return(nil) + expect(subject.enabled?).to eq(false) + end + end + describe "#global_enabled?" do it "returns true if enabled with '1'" do allow(ENV).to receive(:[]).with("VAGRANT_EXPERIMENTAL").and_return("1") expect(subject.global_enabled?).to eq(true) end - it "returns true if enabled with a list of features" do + it "returns false if enabled with a partial list of features" do + allow(ENV).to receive(:[]).with("VAGRANT_EXPERIMENTAL").and_return("list,of,features") + expect(subject.global_enabled?).to eq(false) + end + + it "returns true if enabled with a complete list of features" do + stub_const("Vagrant::Util::Experimental::VALID_FEATURES", ["list","of","features"]) allow(ENV).to receive(:[]).with("VAGRANT_EXPERIMENTAL").and_return("list,of,features") expect(subject.global_enabled?).to eq(true) end @@ -54,9 +82,9 @@ describe Vagrant::Util::Experimental do expect(subject.feature_enabled?("secret_feature")).to eq(true) end - it "returns false if flag does not contain feature requested" do - allow(ENV).to receive(:[]).with("VAGRANT_EXPERIMENTAL").and_return("secret_feature") - expect(subject.feature_enabled?("anything")).to eq(false) + it "returns false if flag is set but does not contain feature requested" do + allow(ENV).to receive(:[]).with("VAGRANT_EXPERIMENTAL").and_return("fake_feature") + expect(subject.feature_enabled?("secret_feature")).to eq(false) end it "returns false if flag set to 0" do