Introduce a local and global check for enabled experimental features
This commit is contained in:
parent
c07f99fe7d
commit
01ec72cac2
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue