Skip to content

Fix backwards compatibility from #511 #527

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

Merged
merged 1 commit into from
Sep 21, 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
10 changes: 7 additions & 3 deletions lib/puppet/parser/functions/parsejson.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#

module Puppet::Parser::Functions
newfunction(:parsejson, :type => :rvalue, :arity => -2, :doc => <<-EOS
newfunction(:parsejson, :type => :rvalue, :doc => <<-EOS
This function accepts JSON as a string and converts it into the correct
Puppet structure.

Expand All @@ -15,8 +15,12 @@ module Puppet::Parser::Functions

begin
PSON::load(arguments[0]) || arguments[1]
rescue Exception
arguments[1]
rescue Exception => e
if arguments[1]
arguments[1]
else
raise e
end
end

end
Expand Down
10 changes: 7 additions & 3 deletions lib/puppet/parser/functions/parseyaml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#

module Puppet::Parser::Functions
newfunction(:parseyaml, :type => :rvalue, :arity => -2, :doc => <<-EOS
newfunction(:parseyaml, :type => :rvalue, :doc => <<-EOS
This function accepts YAML as a string and converts it into the correct
Puppet structure.

Expand All @@ -16,8 +16,12 @@ module Puppet::Parser::Functions

begin
YAML::load(arguments[0]) || arguments[1]
rescue Exception
arguments[1]
rescue Exception => e
if arguments[1]
arguments[1]
else
raise e
end
end

end
Expand Down
16 changes: 14 additions & 2 deletions spec/acceptance/parsejson_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,24 @@
it 'raises error on incorrect json' do
pp = <<-EOS
$a = '{"hunter": "washere", "tests": "passing",}'
$ao = parsejson($a, {'tests' => 'using the default value'})
$ao = parsejson($a, 'tests are using the default value')
notice(inline_template('a is <%= @ao.inspect %>'))
EOS

apply_manifest(pp, :catch_failures => true) do |r|
expect(r.stdout).to match(/tests are "using the default value"/)
expect(r.stdout).to match(/tests are using the default value/)
end
end

it 'raises error on incorrect json' do
pp = <<-EOS
$a = '{"hunter": "washere", "tests": "passing",}'
$ao = parsejson($a)
notice(inline_template('a is <%= @ao.inspect %>'))
EOS

apply_manifest(pp, :expect_failures => true) do |r|
expect(r.stderr).to match(/expected next name/)
end
end

Expand Down
14 changes: 14 additions & 0 deletions spec/acceptance/parseyaml_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@
end
end

it 'raises error on incorrect yaml' do
pp = <<-EOS
$a = "---\nhunter: washere\ntests: passing\n:"
$o = parseyaml($a)
$tests = $o['tests']
notice(inline_template('tests are <%= @tests.inspect %>'))
EOS

apply_manifest(pp, :expect_failures => true) do |r|
expect(r.stderr).to match(/(syntax error|did not find expected key)/)
end
end


it 'raises error on incorrect number of arguments' do
pp = <<-EOS
$o = parseyaml()
Expand Down
6 changes: 3 additions & 3 deletions spec/functions/parsejson_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@

end

context 'with incorrect YAML data' do
it 'should return "nil" if a default value should be returned but is not provided' do
context 'with incorrect JSON data' do
it 'should raise an error with invalid JSON and no default' do
is_expected.to run.with_params('').
and_return(nil)
and_raise_error(PSON::ParserError)
end

it 'should support a structure for a default value' do
Expand Down
17 changes: 13 additions & 4 deletions spec/functions/parseyaml_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,21 @@

end

context 'with incorrect YAML data' do
it 'should return "nil" if a default value should be returned but is not provided' do
is_expected.to run.with_params('').
and_return(nil)
context 'on a modern ruby', :unless => RUBY_VERSION == '1.8.7' do
it 'should raise an error with invalid YAML and no default' do
is_expected.to run.with_params('["one"').
and_raise_error(Psych::SyntaxError)
end
end

context 'when running on ruby 1.8.7, which does not have Psych', :if => RUBY_VERSION == '1.8.7' do
it 'should raise an error with invalid YAML and no default' do
is_expected.to run.with_params('["one"').
and_raise_error(ArgumentError)
end
end

context 'with incorrect YAML data' do
it 'should support a structure for a default value' do
is_expected.to run.with_params('', {'a' => '1'}).
and_return({'a' => '1'})
Expand Down