Introduce a local and global check for enabled experimental features

This commit is contained in:
Brian Cain 2018-12-07 13:30:50 -08:00
parent c07f99fe7d
commit 01ec72cac2
No known key found for this signature in database
GPG Key ID: 9FC4639B2E4510A0
3 changed files with 54 additions and 11 deletions

View File

@ -166,13 +166,11 @@ begin
# 0 - Disables experimental features # 0 - Disables experimental features
# 1 - Enables all features # 1 - Enables all features
# String - Enables one or more features, separated by commas # 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 experimental = Vagrant::Util::Experimental.features_requested
ui = Vagrant::UI::Prefixed.new(env.ui, "vagrant") ui = Vagrant::UI::Prefixed.new(env.ui, "vagrant")
logger.debug("Experimental flag is enabled") logger.debug("Experimental flag is enabled")
if experimental.size >= 1 && experimental.first != "1" if Vagrant::Util::Experimental.global_enabled?
ui.warn(I18n.t("vagrant.general.experimental.features", features: experimental.join(", ")), bold: true, prefix: true, channel: :error)
else
ui.warn(I18n.t("vagrant.general.experimental.all"), bold: true, prefix: true, channel: :error) ui.warn(I18n.t("vagrant.general.experimental.all"), bold: true, prefix: true, channel: :error)
end end
end end

View File

@ -4,10 +4,11 @@ module Vagrant
VALID_FEATURES = [] VALID_FEATURES = []
class << self 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] # @return [Boolean]
def global_enabled? def enabled?
if !defined?(@_experimental) if !defined?(@_experimental)
experimental = features_requested experimental = features_requested
if experimental.size >= 1 && experimental.first != "0" if experimental.size >= 1 && experimental.first != "0"
@ -19,8 +20,24 @@ module Vagrant
@_experimental @_experimental
end 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 # 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 # @param [String] feature
# @return [Boolean] - A hash containing the original array and if it is valid # @return [Boolean] - A hash containing the original array and if it is valid

View File

@ -7,13 +7,41 @@ describe Vagrant::Util::Experimental do
before(:each) { described_class.reset! } before(:each) { described_class.reset! }
subject { described_class } 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 describe "#global_enabled?" do
it "returns true if enabled with '1'" do it "returns true if enabled with '1'" do
allow(ENV).to receive(:[]).with("VAGRANT_EXPERIMENTAL").and_return("1") allow(ENV).to receive(:[]).with("VAGRANT_EXPERIMENTAL").and_return("1")
expect(subject.global_enabled?).to eq(true) expect(subject.global_enabled?).to eq(true)
end 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") allow(ENV).to receive(:[]).with("VAGRANT_EXPERIMENTAL").and_return("list,of,features")
expect(subject.global_enabled?).to eq(true) expect(subject.global_enabled?).to eq(true)
end end
@ -54,9 +82,9 @@ describe Vagrant::Util::Experimental do
expect(subject.feature_enabled?("secret_feature")).to eq(true) expect(subject.feature_enabled?("secret_feature")).to eq(true)
end end
it "returns false if flag does not contain feature requested" do it "returns false if flag is set but does not contain feature requested" do
allow(ENV).to receive(:[]).with("VAGRANT_EXPERIMENTAL").and_return("secret_feature") allow(ENV).to receive(:[]).with("VAGRANT_EXPERIMENTAL").and_return("fake_feature")
expect(subject.feature_enabled?("anything")).to eq(false) expect(subject.feature_enabled?("secret_feature")).to eq(false)
end end
it "returns false if flag set to 0" do it "returns false if flag set to 0" do