Skip to content

Commit

Permalink
Merge pull request #6632 from melissa/ticket/master/PUP-8442-params-d…
Browse files Browse the repository at this point in the history
…efault-false

(PUP-8442) Params should allow default of `false`
  • Loading branch information
ericldelaney committed Feb 14, 2018
2 parents 3b3f4a8 + c98bb67 commit 1b2969e
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 9 deletions.
7 changes: 4 additions & 3 deletions lib/puppet/type.rb
Expand Up @@ -831,10 +831,11 @@ def set_default(attr)

return unless parameter = newattr(klass.name)

if value = parameter.default and ! value.nil?
parameter.value = value
else
value = parameter.default
if value.nil?
@parameters.delete(parameter.name)
else
parameter.value = value
end
end

Expand Down
1 change: 0 additions & 1 deletion lib/puppet/type/user.rb
Expand Up @@ -677,7 +677,6 @@ def generate

defaultto :false

# Use Symbols instead of booleans until PUP-1967 is resolved.
newvalues(:true, :false)

validate do |value|
Expand Down
3 changes: 1 addition & 2 deletions spec/unit/provider/group/windows_adsi_spec.rb
Expand Up @@ -215,8 +215,7 @@

create = sequence('create')
group.expects(:commit).in_sequence(create)
# due to PUP-1967, defaultto false will set the default to nil
group.expects(:set_members).with(['user1', 'user2'], nil).in_sequence(create)
group.expects(:set_members).with(['user1', 'user2'], false).in_sequence(create)

provider.create
end
Expand Down
4 changes: 4 additions & 0 deletions spec/unit/type/augeas_spec.rb
Expand Up @@ -82,6 +82,10 @@
it "should be false for type_check" do
expect(augeas.new(:name => :type_check)[:type_check]).to eq(:false)
end

it "should be false for :force" do
expect(augeas.new(:name => :type_check)[:force]).to eq(false)
end
end

describe "provider interaction" do
Expand Down
6 changes: 6 additions & 0 deletions spec/unit/type/file_spec.rb
Expand Up @@ -191,6 +191,12 @@
end
end

describe "the force parameter" do
it "should default to false" do
expect(file[:force]).to eq(false)
end
end

describe ".instances" do
it "should return an empty array" do
expect(described_class.instances).to eq([])
Expand Down
6 changes: 6 additions & 0 deletions spec/unit/type/group_spec.rb
Expand Up @@ -19,6 +19,12 @@
end

describe "when validating attributes" do
[:auth_membership, :allowdupe, :system].each do |default_param|
it "should set the default for #{default_param} to false" do
expect(@class.new(:name => "foo")[default_param]).to eq(false)
end
end

[:name, :allowdupe].each do |param|
it "should have a #{param} parameter" do
expect(@class.attrtype(param)).to eq(:param)
Expand Down
13 changes: 10 additions & 3 deletions spec/unit/type/user_spec.rb
Expand Up @@ -52,13 +52,20 @@ def self.instances; []; end
expect(described_class.provider_feature(:manages_shell)).not_to be_nil
end

context "managehome" do
context "defaults" do
let (:provider) { @provider_class.new(:name => 'foo', :ensure => :absent) }
let (:instance) { described_class.new(:name => 'foo', :provider => provider) }

it "defaults to false" do
expect(instance[:managehome]).to be_falsey
[:system, :allowdupe, :managehome].each do |default_param|
it "defaults #{default_param} to false" do
expect(instance[default_param]).to eq(false)
end
end
end

context "managehome" do
let (:provider) { @provider_class.new(:name => 'foo', :ensure => :absent) }
let (:instance) { described_class.new(:name => 'foo', :provider => provider) }

it "can be set to false" do
instance[:managehome] = 'false'
Expand Down
63 changes: 63 additions & 0 deletions spec/unit/type_spec.rb
Expand Up @@ -78,6 +78,12 @@
expect(params).not_to be_include(nil)
end

it "can return any `false` values when retrieving all set parameters" do
resource = Puppet::Type.type(:mount).new(:name => "foo", :fstype => "bar", :pass => 1, :ensure => :present, :tag => 'foo', :remounts => false)
params = resource.parameters_with_value
expect(params).to be_include(resource.parameter(:remounts))
end

it "can return an iterator for all set parameters" do
resource = Puppet::Type.type(:notify).new(:name=>'foo',:message=>'bar',:tag=>'baz',:require=> "File['foo']")
params = [:name, :message, :withpath, :loglevel, :tag, :require]
Expand All @@ -94,6 +100,10 @@
expect(Puppet::Type.type(:mount).new(:name => "foo").parameter(:noop)).to be_nil
end

it "should set attributes that have a default value of false and no user specified value" do
expect(Puppet::Type.type(:group).new(:name => "foo")[:system]).to eq(false)
end

it "should have a method for adding tags" do
expect(Puppet::Type.type(:mount).new(:name => "foo")).to respond_to(:tags)
end
Expand Down Expand Up @@ -736,6 +746,59 @@ class something {
end
end

describe "#set_default" do
let(:test_defaults_type) do
Puppet::Type.newtype(:default_test) do
newparam(:name) { isnamevar }
newparam(:default_to_false) { defaultto false }
newparam(:default_to_true) { defaultto true }
newparam(:default_to_string) { defaultto 'foo' }
newparam(:default_to_symbol) { defaultto :false }
newparam(:default_to_empty_array) { defaultto [] }
newparam(:default_to_array) { defaultto ['foo', 'baz'] }
newparam(:default_to_empty_hash) { defaultto ({}) }
newparam(:default_to_hash) { defaultto ({'foo' => 'baz'}) }
newparam(:no_default)
end
end

it "populates a parameter when the default is false" do
expect(test_defaults_type.new(:name => 'bar')[:default_to_false]).to be false
end

it "populates a parameter when the default is true" do
expect(test_defaults_type.new(:name => 'bar')[:default_to_true]).to be true
end

it "populates a parameter when the default is a string" do
expect(test_defaults_type.new(:name => 'bar')[:default_to_string]).to eq 'foo'
end

it "populates a parameter when the default is a symbol" do
expect(test_defaults_type.new(:name => 'bar')[:default_to_symbol]).to eq :false
end

it "populates a parameter when the default is an empty array" do
expect(test_defaults_type.new(:name => 'bar')[:default_to_empty_array]).to match_array([])
end

it "populates a parameter when the default is an array" do
expect(test_defaults_type.new(:name => 'bar')[:default_to_array]).to match_array(['foo', 'baz'])
end

it "populates a parameter when the default is an empty array" do
expect(test_defaults_type.new(:name => 'bar')[:default_to_empty_hash]).to eq ({})
end

it "populates a parameter when the default is a hash" do
expect(test_defaults_type.new(:name => 'bar')[:default_to_hash]).to eq ({'foo' => 'baz'})
end

it "does not populates a parameter when there is no default" do
expect(test_defaults_type.new(:name => 'bar')[:no_default]).to be_nil
end
end

describe "when #finish is called on a type" do
let(:post_hook_type) do
Puppet::Type.newtype(:finish_test) do
Expand Down

0 comments on commit 1b2969e

Please sign in to comment.