diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2017-03-15 22:58:08 -0700 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2017-03-15 22:58:08 -0700 |
commit | 0d6c4462a6c88584ee8a2c8cedc0865c39880154 (patch) | |
tree | 5b385c479a8bef84f471f62d579fca99406dec85 /spec/unit/property/validation_spec.rb | |
parent | 645054783c587301c3c1226581df5cfcd0a9b947 (diff) | |
download | chef-0d6c4462a6c88584ee8a2c8cedc0865c39880154.tar.gz |
Chef-13: Nillable properties
remove deprecations and now properties are nillable and passing a nil is
now always a set, not a get.
Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
Diffstat (limited to 'spec/unit/property/validation_spec.rb')
-rw-r--r-- | spec/unit/property/validation_spec.rb | 120 |
1 files changed, 51 insertions, 69 deletions
diff --git a/spec/unit/property/validation_spec.rb b/spec/unit/property/validation_spec.rb index 4e1b252863..13afcdfbc2 100644 --- a/spec/unit/property/validation_spec.rb +++ b/spec/unit/property/validation_spec.rb @@ -100,13 +100,10 @@ describe "Chef::Resource.property validation" do expect(resource.x).to be_nil end unless tags.include?(:nillable) - it "changing x to nil warns that the get will change to a set in Chef 13 and does not change the value" do + it "changing x to nil does a set" do resource.instance_eval { @x = "default" } - expect { resource.x nil }.to raise_error Chef::Exceptions::DeprecatedFeatureError, - /An attempt was made to change x from "default" to nil by calling x\(nil\). In Chef 12, this does a get rather than a set. In Chef 13, this will change to set the value to nil./ - Chef::Config[:treat_deprecation_warnings_as_errors] = false - expect(resource.x nil).to eq "default" - expect(resource.x).to eq "default" + expect(resource.x nil).to eq nil + expect(resource.x).to eq nil end end end @@ -116,13 +113,10 @@ describe "Chef::Resource.property validation" do expect(resource.x nil).to be_nil expect(resource.x).to be_nil end - it "changing x to nil warns that the get will change to a set in Chef 13 and does not change the value" do + it "changing x to nil does a set" do resource.instance_eval { @x = "default" } - expect { resource.x nil }.to raise_error Chef::Exceptions::DeprecatedFeatureError, - /An attempt was made to change x from "default" to nil by calling x\(nil\). In Chef 12, this does a get rather than a set. In Chef 13, this will change to set the value to nil./ - Chef::Config[:treat_deprecation_warnings_as_errors] = false - expect(resource.x nil).to eq "default" - expect(resource.x).to eq "default" + expect(resource.x nil).to eq nil + expect(resource.x).to eq nil end end elsif tags.include?(:nillable) @@ -134,26 +128,17 @@ describe "Chef::Resource.property validation" do expect(resource.x).to eq nil end end - else + elsif tags.include?(:delayed_nil_default_failure) it "property :x, #{validation}, default: nil warns that the default is invalid" do - expect { resource_class.class_eval("property :x, #{validation}, default: nil", __FILE__, __LINE__) }.to raise_error Chef::Exceptions::DeprecatedFeatureError, - /Default value nil is invalid for property x of resource chef_resource_property_spec_(\d+). Possible fixes: 1. Remove 'default: nil' if nil means 'undefined'. 2. Set a valid default value if there is a reasonable one. 3. Allow nil as a valid value of your property \(for example, 'property :x, \[ String, nil \], default: nil'\)./ + expect { resource_class.class_eval("property :x, #{validation}, default: nil", __FILE__, __LINE__) }.not_to raise_error + expect { resource.x }.to raise_error Chef::Exceptions::ValidationFailed, /Property x must be one of: .* You passed nil./ end - context "With property :x, #{validation}, default: nil" do - before do - Chef::Config[:treat_deprecation_warnings_as_errors] = false - resource_class.class_eval("property :x, #{validation}, default: nil", __FILE__, __LINE__) - Chef::Config[:treat_deprecation_warnings_as_errors] = true - end - - it "changing x to nil emits a warning that the value is invalid and does not change the value" do - resource.instance_eval { @x = "default" } - expect { resource.x nil }.to raise_error Chef::Exceptions::DeprecatedFeatureError, - /nil is an invalid value for x of resource chef_resource_property_spec_(\d+). In Chef 13, this warning will change to an error./ - Chef::Config[:treat_deprecation_warnings_as_errors] = false - expect(resource.x nil).to eq "default" - expect(resource.x).to eq "default" - end + elsif tags.include?(:skip_nil_default) + # intentionally left blank + else + it "property :x, #{validation}, default: nil warns that the default is invalid" do + expect { resource_class.class_eval("property :x, #{validation}, default: nil", __FILE__, __LINE__) }.to raise_error Chef::Exceptions::ValidationFailed, + /Property x must be one of: .* You passed nil./ end end end @@ -174,12 +159,10 @@ describe "Chef::Resource.property validation" do it "set to invalid value raises ValidationFailed" do expect { resource.x 10 }.to raise_error Chef::Exceptions::ValidationFailed end - it "set to nil emits a deprecation warning and does a get" do - expect { resource.x nil }.to raise_error Chef::Exceptions::DeprecatedFeatureError - Chef::Config[:treat_deprecation_warnings_as_errors] = false + it "set to nil does a set" do resource.x "str" - expect(resource.x nil).to eq "str" - expect(resource.x).to eq "str" + expect(resource.x nil).to eq nil + expect(resource.x).to eq nil end end context "when the variable does not have an initial value" do @@ -206,12 +189,9 @@ describe "Chef::Resource.property validation" do it "get succeeds" do expect(resource.x).to eq "default" end - it "set(nil) emits a warning that the value will be set, but does not set the value" do - expect { resource.x nil }.to raise_error Chef::Exceptions::DeprecatedFeatureError, - /An attempt was made to change x from "default" to nil by calling x\(nil\). In Chef 12, this does a get rather than a set. In Chef 13, this will change to set the value to nil./ - Chef::Config[:treat_deprecation_warnings_as_errors] = false - expect(resource.x nil).to eq "default" - expect(resource.x).to eq "default" + it "set(nil) does a set" do + expect(resource.x nil).to eq nil + expect(resource.x).to eq nil end it "set to valid value succeeds" do expect(resource.x "str").to eq "str" @@ -328,11 +308,17 @@ describe "Chef::Resource.property validation" do # Proc validation_test "is: proc { |x| x }", [ true, 1 ], - [ false ] + [ false ], + # this is somewhat complicated, we test adding `default: nil` and that the default fails, but with a proc the + # validation is delayed until access, so we have to test after access not after declaring the default + :delayed_nil_default_failure validation_test "is: proc { |x| x > blah }", [ 10 ], - [ -1 ] + [ -1 ], + # here the test of adding `default: nil` just causes the proc to explode because nil gets passed to the proc + # which throws a NoMethodError on NilClass for the `>` method. + :skip_nil_default validation_test "is: nil", [ ], @@ -589,15 +575,21 @@ describe "Chef::Resource.property validation" do it "value nil emits a validation failed error because it must have a value" do expect { resource.x nil }.to raise_error Chef::Exceptions::ValidationFailed end - context "and value is set to something other than nil" do - before { resource.x 10 } - it "value nil emits a deprecation warning and does a get" do - expect { resource.x nil }.to raise_error Chef::Exceptions::DeprecatedFeatureError - Chef::Config[:treat_deprecation_warnings_as_errors] = false - resource.x 1 - expect(resource.x nil).to eq 1 - expect(resource.x).to eq 1 - end + end + + with_property ":x, String, required: true" do + it "if x is not specified, retrieval fails" do + expect { resource.x }.to raise_error Chef::Exceptions::ValidationFailed + end + it "value nil is not valid (required means 'not nil')" do + expect { resource.x nil }.to raise_error Chef::Exceptions::ValidationFailed + end + it "value '1' is valid" do + expect(resource.x "1").to eq "1" + expect(resource.x).to eq "1" + end + it "value 1 is invalid" do + expect { resource.x 1 }.to raise_error Chef::Exceptions::ValidationFailed end end @@ -625,12 +617,8 @@ describe "Chef::Resource.property validation" do expect(resource.x 1).to eq 1 expect(resource.x).to eq 1 end - it "value nil emits a deprecation warning and does a get" do - expect { resource.x nil }.to raise_error Chef::Exceptions::DeprecatedFeatureError - Chef::Config[:treat_deprecation_warnings_as_errors] = false - resource.x 1 - expect(resource.x nil).to eq 1 - expect(resource.x).to eq 1 + it "value nil is invalid" do + expect { resource.x nil }.to raise_error Chef::Exceptions::ValidationFailed end end @@ -643,11 +631,7 @@ describe "Chef::Resource.property validation" do expect(resource.x).to eq 1 end it "value nil is invalid" do - expect { resource.x nil }.to raise_error Chef::Exceptions::DeprecatedFeatureError - Chef::Config[:treat_deprecation_warnings_as_errors] = false - resource.x 1 - expect(resource.x nil).to eq 1 - expect(resource.x).to eq 1 + expect { resource.x nil }.to raise_error Chef::Exceptions::ValidationFailed end end end @@ -675,15 +659,13 @@ describe "Chef::Resource.property validation" do end it "value '1' is invalid" do - Chef::Config[:treat_deprecation_warnings_as_errors] = false expect { resource.x "1" }.to raise_error Chef::Exceptions::ValidationFailed end - it "value nil does a get" do - Chef::Config[:treat_deprecation_warnings_as_errors] = false + it "value nil does a set" do resource.x 1 resource.x nil - expect(resource.x).to eq 1 + expect(resource.x).to eq nil end end end @@ -709,10 +691,10 @@ describe "Chef::Resource.property validation" do expect { resource.x "1" }.to raise_error Chef::Exceptions::ValidationFailed end - it "value nil does a get" do + it "value nil does a set" do resource.x 1 resource.x nil - expect(resource.x).to eq 1 + expect(resource.x).to eq nil end end end |