diff options
author | John Keiser <john@johnkeiser.com> | 2015-06-03 15:03:18 -0700 |
---|---|---|
committer | John Keiser <john@johnkeiser.com> | 2015-06-23 15:23:01 -0700 |
commit | 2278a61bc0fafe7fdd208c30b12cc11db9547d04 (patch) | |
tree | c3553d800581e515a238cf5b7a161fcae7ff5c33 | |
parent | 4e34d816167af65b36c7a57ad33dca3b1f39c1e4 (diff) | |
download | chef-2278a61bc0fafe7fdd208c30b12cc11db9547d04.tar.gz |
Add property Type as an alias to is
-rw-r--r-- | lib/chef/resource.rb | 23 | ||||
-rw-r--r-- | spec/unit/property/state_spec.rb | 3 | ||||
-rw-r--r-- | spec/unit/property/validation_spec.rb | 118 | ||||
-rw-r--r-- | spec/unit/property_spec.rb | 17 |
4 files changed, 106 insertions, 55 deletions
diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb index 712b7b36cb..39435d557d 100644 --- a/lib/chef/resource.rb +++ b/lib/chef/resource.rb @@ -719,6 +719,8 @@ class Chef # defined by this resource, this will *override* the previous value. # # @param name [Symbol] The name of the property. + # @param type [Object,Array<Object>] The type(s) of this property. + # If present, this is prepended to the `is` validation option. # @param options [Hash<Symbol,Object>] Validation options. # @option options [Object,Array] :is An object, or list of # objects, that must match the value using Ruby's `===` operator @@ -759,15 +761,26 @@ class Chef # be run in the context of the instance (and able to access other # properties). # - # @return [Chef::Resource::PropertyType] The property type. - # - # @example With nothing + # @example Bare property # property :x # - # @example With options + # @example With just a type + # property :x, String + # + # @example With just options # property :x, default: 'hi' # - def self.property(name, **options) + # @example With type and options + # property :x, String, default: 'hi' + # + def self.property(name, type=NULL_ARG, **options) + if type != NULL_ARG + if options[:is] + options[:is] = ([ type ] + [ options[:is] ]).flatten(1) + else + options[:is] = type + end + end define_method(name) do |arg=nil| set_or_return(name.to_sym, arg, options) end diff --git a/spec/unit/property/state_spec.rb b/spec/unit/property/state_spec.rb index 5a7d19af11..d5c0893cfd 100644 --- a/spec/unit/property/state_spec.rb +++ b/spec/unit/property/state_spec.rb @@ -143,8 +143,7 @@ describe "Chef::Resource#identity and #state" do # end # end - # with_property ":y, String" do - with_property ":y, kind_of: String" do + with_property ":y, String" do context "With identity_attr :y on the subclass" do before do subresource_class.class_eval do diff --git a/spec/unit/property/validation_spec.rb b/spec/unit/property/validation_spec.rb index 4aa8a5474e..586d94ee18 100644 --- a/spec/unit/property/validation_spec.rb +++ b/spec/unit/property/validation_spec.rb @@ -97,43 +97,43 @@ describe "Chef::Resource.property validation" do end # Bare types - # context "bare types" do - # validation_test 'String', - # [ 'hi' ], - # [ 10, nil ] - # - # validation_test ':a', - # [ :a ], - # [ :b, nil ] - # - # validation_test ':a, is: :b', - # [ :a, :b ], - # [ :c, nil ] - # - # validation_test ':a, is: [ :b, :c ]', - # [ :a, :b, :c ], - # [ :d, nil ] - # - # validation_test '[ :a, :b ], is: :c', - # [ :a, :b, :c ], - # [ :d, nil ] - # - # validation_test '[ :a, :b ], is: [ :c, :d ]', - # [ :a, :b, :c, :d ], - # [ :e, nil ] - # - # validation_test 'nil', - # [ nil ], - # [ :a ] - # - # validation_test '[ nil ]', - # [ nil ], - # [ :a ] - # - # validation_test '[]', - # [ :a ], - # [] - # end + context "bare types" do + validation_test 'String', + [ 'hi' ], + [ 10, nil ] + + validation_test ':a', + [ :a ], + [ :b, nil ] + + validation_test ':a, is: :b', + [ :a, :b ], + [ :c, nil ] + + validation_test ':a, is: [ :b, :c ]', + [ :a, :b, :c ], + [ :d, nil ] + + validation_test '[ :a, :b ], is: :c', + [ :a, :b, :c ], + [ :d, nil ] + + validation_test '[ :a, :b ], is: [ :c, :d ]', + [ :a, :b, :c, :d ], + [ :e, nil ] + + validation_test 'nil', + [ nil ], + [ :a ] + + validation_test '[ nil ]', + [ nil ], + [ :a ] + + validation_test '[]', + [], + [ :a ] + end # is context "is" do @@ -190,6 +190,10 @@ describe "Chef::Resource.property validation" do validation_test 'is: [ String, nil ]', [ 'a', nil ], [ :b ] + + validation_test 'is: []', + [], + [ :a ] end # Combination @@ -225,6 +229,10 @@ describe "Chef::Resource.property validation" do validation_test 'equal_to: [ nil, "a" ]', [ 'a', nil ], [ 'b' ] + + validation_test 'equal_to: []', + [], + [ :a ] end # kind_of @@ -248,6 +256,14 @@ describe "Chef::Resource.property validation" do validation_test 'kind_of: [ NilClass, String ]', [ nil, 'a' ], [ :a ] + + validation_test 'kind_of: []', + [], + [ :a ] + + validation_test 'kind_of: nil', + [], + [ :a ] end # regex @@ -263,6 +279,14 @@ describe "Chef::Resource.property validation" do validation_test 'regex: [ /z/, /abc/ ]', [ 'xabcy', 'aza' ], [ 'gbh', 123, nil ] + + validation_test 'regex: []', + [], + [ :a ] + + validation_test 'regex: nil', + [], + [ :a ] end # callbacks @@ -278,6 +302,10 @@ describe "Chef::Resource.property validation" do validation_test 'callbacks: { "a" => proc { |x| x.nil? } }', [ nil ], [ 'a' ] + + validation_test 'callbacks: {}', + [ :a ], + [] end # respond_to @@ -301,6 +329,14 @@ describe "Chef::Resource.property validation" do validation_test 'respond_to: [ :to_s, :split ]', [ 'hi' ], [ 1, nil ] + + validation_test 'respond_to: []', + [ :a ], + [] + + validation_test 'respond_to: nil', + [ :a ], + [] end context "cannot_be" do @@ -327,6 +363,14 @@ describe "Chef::Resource.property validation" do validation_test 'cannot_be: [ :empty, :nil, :blahblah ]', [ 1, [1,2], { a: 10 } ], [ [], nil ] + + validation_test 'cannot_be: []', + [ :a ], + [] + + validation_test 'cannot_be: nil', + [ :a ], + [] end context "required" do diff --git a/spec/unit/property_spec.rb b/spec/unit/property_spec.rb index d948ad20e0..17857b8a57 100644 --- a/spec/unit/property_spec.rb +++ b/spec/unit/property_spec.rb @@ -103,8 +103,7 @@ describe "Chef::Resource.property" do # end end - # with_property ":x, Integer" - with_property ':x, kind_of: Integer' do + with_property ":x, Integer" do context "and subclass" do let(:subresource_class) do new_resource_name = self.class.new_resource_name @@ -181,12 +180,10 @@ describe "Chef::Resource.property" do end end - # context "with property :x, String on the subclass" do - context "with property :x, kind_of: String on the subclass" do + context "with property :x, String on the subclass" do before do subresource_class.class_eval do - # property :x, String - property :x, kind_of: String + property :x, String end end @@ -428,8 +425,7 @@ describe "Chef::Resource.property" do end context "validation of defaults" do - #with_property ':x, String, default: 10' do - with_property ':x, default: 10, kind_of: String' do + with_property ':x, String, default: 10' do it "when the resource is created, no error is raised" do resource end @@ -443,7 +439,7 @@ describe "Chef::Resource.property" do end # with_property ":x, String, default: lazy { Namer.next_index }" do - with_property ":x, default: Chef::DelayedEvaluator.new { Namer.next_index }, kind_of: String" do + with_property ":x, String, default: Chef::DelayedEvaluator.new { Namer.next_index }" do it "when the resource is created, no error is raised" do resource end @@ -639,8 +635,7 @@ describe "Chef::Resource.property" do # end # end - # with_property ':x, String' do - with_property ':x, kind_of: String' do + with_property ':x, String' do it "lazy values are not validated on set" do resource.x lazy { Namer.next_index } expect(Namer.current_index).to eq 0 |