Skip to content

Commit 97bd656

Browse files
committed
Merge pull request #527 from mhaskel/511_compatibility
Fix backwards compatibility from #511
2 parents 9b1932c + 799c38e commit 97bd656

File tree

6 files changed

+58
-15
lines changed

6 files changed

+58
-15
lines changed

lib/puppet/parser/functions/parsejson.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#
44

55
module Puppet::Parser::Functions
6-
newfunction(:parsejson, :type => :rvalue, :arity => -2, :doc => <<-EOS
6+
newfunction(:parsejson, :type => :rvalue, :doc => <<-EOS
77
This function accepts JSON as a string and converts it into the correct
88
Puppet structure.
99
@@ -15,8 +15,12 @@ module Puppet::Parser::Functions
1515

1616
begin
1717
PSON::load(arguments[0]) || arguments[1]
18-
rescue Exception
19-
arguments[1]
18+
rescue Exception => e
19+
if arguments[1]
20+
arguments[1]
21+
else
22+
raise e
23+
end
2024
end
2125

2226
end

lib/puppet/parser/functions/parseyaml.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#
44

55
module Puppet::Parser::Functions
6-
newfunction(:parseyaml, :type => :rvalue, :arity => -2, :doc => <<-EOS
6+
newfunction(:parseyaml, :type => :rvalue, :doc => <<-EOS
77
This function accepts YAML as a string and converts it into the correct
88
Puppet structure.
99
@@ -16,8 +16,12 @@ module Puppet::Parser::Functions
1616

1717
begin
1818
YAML::load(arguments[0]) || arguments[1]
19-
rescue Exception
20-
arguments[1]
19+
rescue Exception => e
20+
if arguments[1]
21+
arguments[1]
22+
else
23+
raise e
24+
end
2125
end
2226

2327
end

spec/acceptance/parsejson_spec.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,24 @@
2121
it 'raises error on incorrect json' do
2222
pp = <<-EOS
2323
$a = '{"hunter": "washere", "tests": "passing",}'
24-
$ao = parsejson($a, {'tests' => 'using the default value'})
24+
$ao = parsejson($a, 'tests are using the default value')
2525
notice(inline_template('a is <%= @ao.inspect %>'))
2626
EOS
2727

2828
apply_manifest(pp, :catch_failures => true) do |r|
29-
expect(r.stdout).to match(/tests are "using the default value"/)
29+
expect(r.stdout).to match(/tests are using the default value/)
30+
end
31+
end
32+
33+
it 'raises error on incorrect json' do
34+
pp = <<-EOS
35+
$a = '{"hunter": "washere", "tests": "passing",}'
36+
$ao = parsejson($a)
37+
notice(inline_template('a is <%= @ao.inspect %>'))
38+
EOS
39+
40+
apply_manifest(pp, :expect_failures => true) do |r|
41+
expect(r.stderr).to match(/expected next name/)
3042
end
3143
end
3244

spec/acceptance/parseyaml_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,20 @@
3131
end
3232
end
3333

34+
it 'raises error on incorrect yaml' do
35+
pp = <<-EOS
36+
$a = "---\nhunter: washere\ntests: passing\n:"
37+
$o = parseyaml($a)
38+
$tests = $o['tests']
39+
notice(inline_template('tests are <%= @tests.inspect %>'))
40+
EOS
41+
42+
apply_manifest(pp, :expect_failures => true) do |r|
43+
expect(r.stderr).to match(/(syntax error|did not find expected key)/)
44+
end
45+
end
46+
47+
3448
it 'raises error on incorrect number of arguments' do
3549
pp = <<-EOS
3650
$o = parseyaml()

spec/functions/parsejson_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@
4141

4242
end
4343

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

5050
it 'should support a structure for a default value' do

spec/functions/parseyaml_spec.rb

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,21 @@
4040

4141
end
4242

43-
context 'with incorrect YAML data' do
44-
it 'should return "nil" if a default value should be returned but is not provided' do
45-
is_expected.to run.with_params('').
46-
and_return(nil)
43+
context 'on a modern ruby', :unless => RUBY_VERSION == '1.8.7' do
44+
it 'should raise an error with invalid YAML and no default' do
45+
is_expected.to run.with_params('["one"').
46+
and_raise_error(Psych::SyntaxError)
47+
end
48+
end
49+
50+
context 'when running on ruby 1.8.7, which does not have Psych', :if => RUBY_VERSION == '1.8.7' do
51+
it 'should raise an error with invalid YAML and no default' do
52+
is_expected.to run.with_params('["one"').
53+
and_raise_error(ArgumentError)
54+
end
4755
end
4856

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

0 commit comments

Comments
 (0)