diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2020-04-17 12:51:13 -0700 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2020-04-17 16:23:51 -0700 |
commit | 49aa17ad9805b6949baaa37cfe66f8cf2b8083a8 (patch) | |
tree | 0c1d22d58c8c679c048aac2b400a1b5703c0a70a /spec | |
parent | 8feb3f054185d01d70e42e917429c692dfd8dc81 (diff) | |
download | chef-49aa17ad9805b6949baaa37cfe66f8cf2b8083a8.tar.gz |
Force requiring properties
All required properties are now required for all actions by default
even if the action does not reference the property.
In order to only make the property required for a subset of the
actions, specify them as an array of symbols to the required
options on the property.
```
property :whatever, String, required: %i{start stop}
action :start do
end
action :stop do
end
action :enable do
end
action :disable do
end
```
That property will be required for start+stop but not for
enable+disable.
There's an unaddressed edge case here where if you reference the
property in an action which was not specified that it will also
fail validation. That is correct behavior. We should probably
dig into how to warn the user that they must either remove the
reference to the property from that action or else to add the
action to the list of required actions on the property.
Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
Diffstat (limited to 'spec')
-rw-r--r-- | spec/unit/property/validation_spec.rb | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/spec/unit/property/validation_spec.rb b/spec/unit/property/validation_spec.rb index dab2be8000..c8daee1580 100644 --- a/spec/unit/property/validation_spec.rb +++ b/spec/unit/property/validation_spec.rb @@ -45,11 +45,22 @@ describe "Chef::Resource.property validation" do def self.blah "class#{Namer.next_index}" end + + action :doit do + # this needs to not reference any properties + end + + action :doit2 do + # this needs to not reference any properties + end end end + let(:node) { Chef::Node.new } + let(:events) { Chef::EventDispatch::Dispatcher.new } + let(:run_context) { Chef::RunContext.new(node, {}, events) } let(:resource) do - resource_class.new("blah") + resource_class.new("blah", run_context) end def self.english_join(values) @@ -576,6 +587,19 @@ 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 + it "fails if it is not specified, on running the action, even if it is not referenced" do + expect { resource.run_action(:doit) }.to raise_error Chef::Exceptions::ValidationFailed + end + end + + with_property ":x, required: %i{doit}" do + it "fails if it is not specified, on running the doit action, even if it is not referenced" do + expect { resource.run_action(:doit) }.to raise_error Chef::Exceptions::ValidationFailed + end + + it "does not fail if it is not specified, on running the doit2 action" do + expect { resource.run_action(:doit2) }.not_to raise_error + end end with_property ":x, String, required: true" do |