diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2018-03-22 19:48:53 -0700 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2018-03-22 19:49:10 -0700 |
commit | 7eb06ba4180c2acd0821fc127e8f4149c0f88bca (patch) | |
tree | d6aaa21cf99f3b709396c42fd53d5818e7b6600d /spec/unit/property/validation_spec.rb | |
parent | 2fd65bdcbd8d7201e404ca752e437dfbe52b914d (diff) | |
download | chef-7eb06ba4180c2acd0821fc127e8f4149c0f88bca.tar.gz |
Setting nil to properties with implicit nil sets default value
- This makes converting core resources to properties safer
- This makes it easier to apply wrapping properties to subresources
property :foo, String, default: "foo"
This is where the change lies, and writing a nil here will now actually
write a "foo" to the variable.
property :foo, [ String, nil ], default: "foo"
This is unchanged. Writing nil writes nil.
property :foo, String
Technically this is changed, since it writes the default value, but
since nil.equal?(nil) in a very deep way no behavior changes.
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 | 55 |
1 files changed, 51 insertions, 4 deletions
diff --git a/spec/unit/property/validation_spec.rb b/spec/unit/property/validation_spec.rb index 882ea3353b..b05d8c4e17 100644 --- a/spec/unit/property/validation_spec.rb +++ b/spec/unit/property/validation_spec.rb @@ -617,8 +617,9 @@ describe "Chef::Resource.property validation" do expect(resource.x 1).to eq 1 expect(resource.x).to eq 1 end - it "value nil is invalid" do - expect { resource.x nil }.to raise_error Chef::Exceptions::ValidationFailed + it "value nil sets to the default" do + # this mildly complicated because the default of a name property is a lazy evaluator to the actual resource.name + expect(resource.x nil).to be_a(Chef::DelayedEvaluator) end end @@ -630,8 +631,54 @@ describe "Chef::Resource.property validation" do expect(resource.x 1).to eq 1 expect(resource.x).to eq 1 end - it "value nil is invalid" do - expect { resource.x nil }.to raise_error Chef::Exceptions::ValidationFailed + it "value nil sets the default" do + expect(resource.x nil).to eq 10 + expect(resource.x).to eq 10 + end + end + end + + context "nil setting default" do + with_property ":x, String" do + it "if x is not specified, the default is returned" do + expect(resource.x).to eq nil + end + it "value '2' is valid" do + expect(resource.x "2").to eq "2" + expect(resource.x).to eq "2" + end + it "value nil sets the default" do + resource.x "2" + expect(resource.x nil).to eq nil + expect(resource.x).to eq nil + end + end + with_property ":x, String, default: '1'" do + it "if x is not specified, the default is returned" do + expect(resource.x).to eq "1" + end + it "value '2' is valid" do + expect(resource.x "2").to eq "2" + expect(resource.x).to eq "2" + end + it "value nil sets the default" do + resource.x "2" + expect(resource.x nil).to eq "1" + expect(resource.x).to eq "1" + end + end + with_property ":x, [ String, nil ] , default: '1'" do + it "if x is not specified, the default is returned" do + expect(resource.x).to eq "1" + end + it "value '2' is valid" do + expect(resource.x "2").to eq "2" + expect(resource.x).to eq "2" + end + it "value nil sets to nil" do + resource.x "2" + expect(resource.x nil).to eq nil + expect(resource.x).to eq nil end end end |