summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith@chef.io>2018-03-17 19:00:14 -0700
committerTim Smith <tsmith@chef.io>2018-06-11 13:48:35 -0700
commitbbcd583eb4e7cbee84bfcd05b72183b03a3a4ec0 (patch)
tree80ffaf467f5e4cfbf8508a6c36d49ff72b3c19ca
parent283292519a2bc8f2d4e62abc98d987caac3d2b92 (diff)
downloadchef-bbcd583eb4e7cbee84bfcd05b72183b03a3a4ec0.tar.gz
Improve comma separated list splitting in group resourcegroup2
This is the same thing I did in sudo. It just makes the string parsing a bit more user spacing proof. accepted before: - 1,2,3 accepted now: - 1,2,3 - 1, 2, 3 - 1 ,2 ,3 - any combo of the above 3 Signed-off-by: Tim Smith <tsmith@chef.io>
-rw-r--r--lib/chef/resource/group.rb33
-rw-r--r--spec/unit/resource/group_spec.rb22
2 files changed, 15 insertions, 40 deletions
diff --git a/lib/chef/resource/group.rb b/lib/chef/resource/group.rb
index 9298fbc150..5229a4bb04 100644
--- a/lib/chef/resource/group.rb
+++ b/lib/chef/resource/group.rb
@@ -20,45 +20,20 @@
class Chef
class Resource
class Group < Chef::Resource
- state_attrs :members
-
description "Use the group resource to manage a local group."
allowed_actions :create, :remove, :modify, :manage
default_action :create
- def initialize(name, run_context = nil)
- super
- @members = []
- @excluded_members = []
- end
-
property :group_name, String, name_property: true, identity: true
property :gid, [ String, Integer ]
-
- def members(arg = nil)
- converted_members = arg.is_a?(String) ? arg.split(",") : arg
- set_or_return(
- :members,
- converted_members,
- :kind_of => [ Array ]
- )
- end
-
- alias_method :users, :members
-
- def excluded_members(arg = nil)
- converted_members = arg.is_a?(String) ? arg.split(",") : arg
- set_or_return(
- :excluded_members,
- converted_members,
- :kind_of => [ Array ]
- )
- end
-
+ property :members, [Array, String], default: lazy { [] }, coerce: proc { |arg| arg.is_a?(String) ? arg.split(/\s*,\s*/) : arg }
+ property :excluded_members, [Array, String], default: lazy { [] }, coerce: proc { |arg| arg.is_a?(String) ? arg.split(/\s*,\s*/) : arg }
property :append, [ TrueClass, FalseClass ], default: false
property :system, [ TrueClass, FalseClass ], default: false
property :non_unique, [ TrueClass, FalseClass ], default: false
+
+ alias_method :users, :members
end
end
end
diff --git a/spec/unit/resource/group_spec.rb b/spec/unit/resource/group_spec.rb
index 638d8718c7..6d4ff36af9 100644
--- a/spec/unit/resource/group_spec.rb
+++ b/spec/unit/resource/group_spec.rb
@@ -93,23 +93,23 @@ describe Chef::Resource::Group, "members" do
let(:resource) { Chef::Resource::Group.new("fakey_fakerton") }
[ :users, :members].each do |method|
- it "(#{method}) allows and convert a string" do
- resource.send(method, "aj")
- expect(resource.send(method)).to eql(["aj"])
+ it "(#{method}) allows a String and coerces it to an Array" do
+ resource.send(method, "some_user")
+ expect(resource.send(method)).to eql(["some_user"])
end
- it "(#{method}) should split a string on commas" do
- resource.send(method, "aj,adam")
- expect(resource.send(method)).to eql( %w{aj adam} )
+ it "(#{method}) coerces a comma separated list of users to an Array" do
+ resource.send(method, "some_user, other_user ,another_user,just_one_more_user")
+ expect(resource.send(method)).to eql( %w{some_user other_user another_user just_one_more_user} )
end
- it "(#{method}) allows an array" do
- resource.send(method, %w{aj adam})
- expect(resource.send(method)).to eql( %w{aj adam} )
+ it "(#{method}) allows an Array" do
+ resource.send(method, %w{some_user other_user})
+ expect(resource.send(method)).to eql( %w{some_user other_user} )
end
- it "(#{method}) does not allow a hash" do
- expect { resource.send(method, { :aj => "is freakin awesome" }) }.to raise_error(ArgumentError)
+ it "(#{method}) does not allow a Hash" do
+ expect { resource.send(method, { :some_user => "is freakin awesome" }) }.to raise_error(ArgumentError)
end
end
end