From 2f506b3c70a872e676662d863a8576333112e4c1 Mon Sep 17 00:00:00 2001 From: Dominik Richter Date: Thu, 21 Dec 2017 14:20:59 +0100 Subject: [PATCH] bugfix: default attributes for nil and false (#2410) Traditionally those would translated DEFAULT_ATTRIBUTE. but that was wrong, it should have been nil or false or whatever the user supplied. Signed-off-by: Dominik Richter --- lib/inspec/objects/attribute.rb | 2 +- test/unit/objects/attribute_test.rb | 13 +++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/inspec/objects/attribute.rb b/lib/inspec/objects/attribute.rb index 33e7cef45..32f43d6e3 100644 --- a/lib/inspec/objects/attribute.rb +++ b/lib/inspec/objects/attribute.rb @@ -27,7 +27,7 @@ module Inspec end def default - @opts[:default] || DEFAULT_ATTRIBUTE.new + @opts.key?(:default) ? @opts[:default] : DEFAULT_ATTRIBUTE.new end def title diff --git a/test/unit/objects/attribute_test.rb b/test/unit/objects/attribute_test.rb index e971cb61d..61f432d9e 100644 --- a/test/unit/objects/attribute_test.rb +++ b/test/unit/objects/attribute_test.rb @@ -29,10 +29,19 @@ describe Inspec::Attribute do end describe 'attribute with a default value set' do - let(:attribute) { Inspec::Attribute.new('test_attribute', default: 'default_value') } - it 'returns the user-configured default value if no value is assigned' do + attribute = Inspec::Attribute.new('test_attribute', default: 'default_value') attribute.value.must_equal 'default_value' end + + it 'returns the user-configured default value if no value is assigned (nil)' do + attribute = Inspec::Attribute.new('test_attribute', default: nil) + attribute.value.must_equal nil + end + + it 'returns the user-configured default value if no value is assigned (false)' do + attribute = Inspec::Attribute.new('test_attribute', default: false) + attribute.value.must_equal false + end end end