Skip to content

Commit

Permalink
Merge pull request #12 from glarizza/fix_numeric
Browse files Browse the repository at this point in the history
Handle stringified numerics
  • Loading branch information
cprice404 committed May 1, 2015
2 parents 2133462 + 4f9784e commit d7ca17c
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 3 deletions.
8 changes: 8 additions & 0 deletions lib/puppet/provider/hocon_setting/ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ def destroy
@conf_file = nil
end

def type
@resource[:type]
end

def type=(value)
@resource[:type] = value
end

def value
val = conf_object.get_value(setting).unwrapped

Expand Down
16 changes: 15 additions & 1 deletion lib/puppet/type/hocon_setting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@
raise "Type specified as #{@resource[:type]} but was #{value.class}"
end
when 'number'
unless value.is_a?(Numeric)
# Puppet stringifies numerics in versions of Puppet < 4.0.0
# Account for this by first attempting to cast to an Integer.
# Failing that, attempt to cast to a Float or return false
numeric_as_string = Integer(value) rescue false
numeric_as_string = numeric_as_string ? numeric_as_string : Float(value) rescue false

unless (value.is_a?(Numeric) or numeric_as_string)
raise "Type specified as 'number' but was #{value.class}"
end
when 'array'
Expand All @@ -61,6 +67,14 @@
raise "Type was specified as #{@resource[:type]}, but should have been one of 'boolean', 'string', 'text', 'number', 'array', or 'hash'"
end
end

munge do |value|
if value.is_a?(String) and @resource[:type] == 'number'
munged_value = Integer(value) rescue false
value = munged_value ? munged_value : Float(value)
end
value
end
end

validate do
Expand Down
31 changes: 29 additions & 2 deletions spec/unit/puppet/provider/conf_setting/ruby_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -517,13 +517,40 @@ def validate_file(expected_content,tmpfile = tmpfile)
expect(provider.value[0]).to eql(true)
end

it "should be able to handle number value with number type specified" do
it "should be able to handle an integer value with number type specified" do
resource = Puppet::Type::Hocon_setting.new(common_params.merge(
:setting => 'test_key_1.master', :value => 12, :type => 'number'))
provider = described_class.new(resource)
provider.create
expect(provider.exists?).to be true
expect(provider.value[0]).to eql(12)
expect(provider.value[0].eql?(12)).to be(true)
end

it "should be able to handle a float value with number type specified" do
resource = Puppet::Type::Hocon_setting.new(common_params.merge(
:setting => 'test_key_1.master', :value => 13.37, :type => 'number'))
provider = described_class.new(resource)
provider.create
expect(provider.exists?).to be true
expect(provider.value[0].eql?(13.37)).to be(true)
end

it "should be able to handle an Integer string value with number type specified" do
resource = Puppet::Type::Hocon_setting.new(common_params.merge(
:setting => 'test_key_1.master', :value => '12', :type => 'number'))
provider = described_class.new(resource)
provider.create
expect(provider.exists?).to be true
expect(provider.value[0].eql?(12)).to be(true)
end

it "should be able to handle a Float string value with number type specified" do
resource = Puppet::Type::Hocon_setting.new(common_params.merge(
:setting => 'test_key_1.master', :value => '13.37', :type => 'number'))
provider = described_class.new(resource)
provider.create
expect(provider.exists?).to be true
expect(provider.value[0].eql?(13.37)).to be(true)
end

it "should be able to handle string value with string type specified" do
Expand Down
124 changes: 124 additions & 0 deletions spec/unit/puppet/type/hocon_setting_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
require 'puppet'
require 'puppet/type/hocon_setting'
describe Puppet::Type.type(:hocon_setting) do
let(:resource) {
Puppet::Type.type(:hocon_setting).new(
:name => 'hocon setting',
:path => '/tmp/hocon.setting',
:setting => 'test_key.master',
:value => 'value',
:type => 'text'
)
}

it 'is ensurable' do
resource[:ensure] = :present
expect(resource[:ensure]).to be(:present)
resource[:ensure] = :absent
expect(resource[:ensure]).to be(:absent)
end

it 'raises an error if an invalid ensure value is passed' do
expect { resource[:ensure] = 'file' }.to raise_error \
Puppet::Error, /Invalid value "file"/
end

it 'accepts a valid type value' do
valid_types = [
'boolean',
'string',
'text',
'number',
'array',
'hash'
]

valid_types.each do |t|
resource[:type] = t
expect(resource[:type]).to eq(t)
end
end

it 'raises an error with invalid type values when a value is specified' do
resource[:type] = 'blarg'
expect { resource[:value] = 4 }.to raise_error \
Puppet::Error, /Type was specified as blarg, but should have been one of 'boolean'/
end

it 'accepts valid boolean values' do
resource[:type] = 'boolean'
[true, false].each do |val|
resource[:value] = val
expect(resource[:value]).to eq([val])
end
end

it 'raises an error with invalid boolean values' do
resource[:type] = 'boolean'
expect { resource[:value] = 'not boolean' }.to raise_error \
Puppet::Error, /Type specified as 'boolean' but was String/
end

it 'accepts valid string and text values' do
['string', 'text'].each do |t|
resource[:type] = t
resource[:value] = 'string value'
expect(resource[:value]).to eq(['string value'])
end
end

it 'raises an error with invalid string and text values' do
['string', 'text'].each do |t|
resource[:type] = t
expect { resource[:value] = 4 }.to raise_error \
Puppet::Error, /Type specified as #{t} but was Fixnum/
end
end

it 'accepts valid number values' do
[13, 13.37].each do |t|
resource[:type] = 'number'
resource[:value] = t
expect(resource[:value]).to eq([t])
end
end

it 'accepts valid number values as a string' do
{
'13' => 13,
'13.37' => 13.37
}.each do |key, val|
resource[:type] = 'number'
resource[:value] = key
expect(resource[:value].eql?([val])).to be(true)
end
end

it 'raises an error with invalid number values' do
['string', '45g'].each do |t|
resource[:type] = 'number'
expect { resource[:value] = t }.to raise_error \
Puppet::Error, /Type specified as 'number' but was String/
end
end

it 'accepts valid array values' do
array = ['foo', 'bar']
resource[:type] = 'array'
resource[:value] = array
expect(resource[:value]).to eq(array)
end

it 'accepts valid hash values' do
hash = { 'key' => 'value' }
resource[:type] = 'hash'
resource[:value] = hash
expect(resource[:value]).to eq([hash])
end

it 'raises an error with invalid hash values' do
resource[:type] = 'hash'
expect { resource[:value] = 4 }.to raise_error \
Puppet::Error, /Type specified as 'hash' but was Fixnum/
end
end

0 comments on commit d7ca17c

Please sign in to comment.