Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mitigate .to_json segfaults on Ruby 1.8.7 #205

Merged
merged 4 commits into from
Dec 22, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ before_install: rm Gemfile.lock || true
sudo: false
cache: bundler
rvm:
- 1.8.7
- 1.9.3
- 2.0.0
- 2.1.0
Expand All @@ -13,6 +14,8 @@ env:
- PUPPET_VERSION="~> 3.1.0"
- PUPPET_VERSION="~> 3.3.0"
- PUPPET_VERSION="~> 3.7.4" FUTURE_PARSER=yes STRICT_VARIABLES=yes
- PUPPET_VERSION="~> 3.8.4"
- PUPPET_VERSION="~> 3.8.4" FUTURE_PARSER=yes STRICT_VARIABLES=yes
- PUPPET_VERSION="~> 4.0.0"
- PUPPET_VERSION="~> 4.1.0"
matrix:
Expand All @@ -35,8 +38,12 @@ matrix:
env: PUPPET_VERSION="~> 3.4.0"
- rvm: 1.9.3
env: PUPPET_VERSION="~> 4.0.0"
- rvm: 1.8.7
env: PUPPET_VERSION="~> 4.0.0"
- rvm: 2.0.0
env: PUPPET_VERSION="~> 4.0.0"
- rvm: 1.8.7
env: PUPPET_VERSION="~> 4.1.0"
- rvm: 1.9.3
env: PUPPET_VERSION="~> 4.1.0"
- rvm: 2.0.0
Expand Down
31 changes: 18 additions & 13 deletions lib/puppet/parser/functions/consul_sorted_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@ class << self

def sorted_generate(obj)
case obj
when Fixnum, Float, TrueClass, FalseClass, NilClass
return obj.to_json
when String
# Convert quoted integers (string) to int
return (obj.match(/\A[-]?[0-9]+\z/) ? obj.to_i : obj).to_json
when NilClass,Fixnum, Float, TrueClass, FalseClass,String
return simple_generate(obj)
when Array
arrayRet = []
obj.each do |a|
Expand All @@ -34,14 +31,8 @@ def sorted_pretty_generate(obj, indent_len=4)
indent = " " * indent_len

case obj

when Fixnum, Float, TrueClass, FalseClass, NilClass
return obj.to_json

when String
# Convert quoted integers (string) to int
return (obj.match(/\A[-]?[0-9]+\z/) ? obj.to_i : obj).to_json

when NilClass,Fixnum, Float, TrueClass, FalseClass,String
return simple_generate(obj)
when Array
arrayRet = []

Expand Down Expand Up @@ -80,6 +71,20 @@ def sorted_pretty_generate(obj, indent_len=4)
end

end # end def
private
# simplify jsonification of standard types
def simple_generate(obj)
case obj
when NilClass
'null'
when Fixnum, Float, TrueClass, FalseClass
"#{obj}"
else
# Should be a string
# keep string integers unquoted
(obj =~ /\A[-]?\d+\z/) ? obj : obj.to_json
end
end

end # end class

Expand Down
4 changes: 2 additions & 2 deletions spec/classes/init_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@
it { should contain_file('consul config.json').with_content(/"server": true/) }
it { should contain_file('consul config.json').with_content(/"http": -1,/) }
it { should contain_file('consul config.json').with_content(/"https": 8500/) }
it { should contain_file('consul config.json').with_content(/"ports": {/) }
it { should contain_file('consul config.json').with_content(/"ports": \{/) }
end

context "When asked not to manage the user" do
Expand Down Expand Up @@ -360,7 +360,7 @@
it { should contain_file('consul config.json').with(
:owner => 'custom_consul_user',
:group => 'custom_consul_group',
:mode => '0600',
:mode => '0600'
)}
end

Expand Down
40 changes: 40 additions & 0 deletions spec/functions/consul_sorted_json_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,37 @@
require 'spec_helper'

RSpec.shared_examples 'handling_simple_types' do |pretty|
it 'handles nil' do
expect(subject.call([ {'key' => nil }],pretty)).to eql('{"key":null}')
end
it 'handles true' do
expect(subject.call([{'key' => true }],pretty)).to eql('{"key":true}')
end
it 'handles nil' do
expect(subject.call([{'key' => false }],pretty)).to eql('{"key":false}')
end
it 'handles positive integer' do
expect(subject.call([{'key' => 1 }],pretty)).to eql('{"key":1}')
end
it 'handles negative integer' do
expect(subject.call([{'key' => -1 }],pretty)).to eql('{"key":-1}')
end
it 'handles positive float' do
expect(subject.call([{'key' => 1.1 }],pretty)).to eql('{"key":1.1}')
end
it 'handles negative float' do
expect(subject.call([{'key' => -1.1 }],pretty)).to eql('{"key":-1.1}')
end
it 'handles integer in a string' do
expect(subject.call([{'key' => '1' }],pretty)).to eql('{"key":1}')
end
it 'handles negative integer in a string' do
expect(subject.call([{'key' => '-1' }],pretty)).to eql('{"key":-1}')
end
it 'handles simple string' do
expect(subject.call([{'key' => 'aString' }],pretty)).to eql("{\"key\":\"aString\"}")
end
end
describe 'consul_sorted_json', :type => :puppet_function do

let(:test_hash){ { 'z' => 3, 'a' => '1', 'p' => '2', 's' => '-7' } }
Expand Down Expand Up @@ -41,4 +73,12 @@
end

end
context 'test simple behavior' do
context 'sorted' do
include_examples 'handling_simple_types', false
end
context 'sorted pretty' do
include_examples 'handling_simple_types', true
end
end
end
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def hiera_stub
RSpec.configure do |c|
c.mock_framework = :rspec
c.before(:each) do
Puppet::Indirector::Hiera.stub(:hiera => hiera_stub)
allow(Puppet::Indirector::Hiera).to receive(:hiera) { hiera_stub }
end

end