Skip to content

Commit c36f935

Browse files
committed
Address loadjson.rb issues with PSON
1 parent 6723dec commit c36f935

File tree

3 files changed

+76
-21
lines changed

3 files changed

+76
-21
lines changed

lib/puppet/parser/functions/load_module_metadata.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ module Puppet::Parser::Functions
2424

2525
metadata_exists = File.exists?(metadata_json) # rubocop:disable Lint/DeprecatedClassMethods : Changing to .exist? breaks the code
2626
if metadata_exists
27-
metadata = PSON.load(File.read(metadata_json))
27+
metadata = if Puppet::Util::Package.versioncmp(Puppet.version, '8.0.0').negative?
28+
PSON.load(File.read(metadata_json))
29+
else
30+
JSON.parse(File.read(metadata_json))
31+
end
2832
else
2933
metadata = {}
3034
raise(Puppet::ParseError, "load_module_metadata(): No metadata.json file for module #{mod}") unless allow_empty_metadata

lib/puppet/parser/functions/loadjson.rb

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,32 @@ module Puppet::Parser::Functions
4242
else
4343
url = args[0]
4444
end
45-
begin
46-
contents = OpenURI.open_uri(url, **http_options)
47-
rescue OpenURI::HTTPError => err
48-
res = err.io
49-
warning("Can't load '#{url}' HTTP Error Code: '#{res.status[0]}'")
50-
args[1]
45+
if Puppet::Util::Package.versioncmp(Puppet.version, '8.0.0').negative?
46+
begin
47+
contents = OpenURI.open_uri(url, **http_options)
48+
rescue OpenURI::HTTPError => err
49+
res = err.io
50+
warning("Can't load '#{url}' HTTP Error Code: '#{res.status[0]}'")
51+
args[1]
52+
end
53+
PSON.load(contents) || args[1]
54+
else
55+
begin
56+
contents = URI.open(url, **http_options) # rubocop:disable Security/Open : Temporarily disabling this cop. This is a security risk and must be addressed before release.
57+
rescue URI::Error => err
58+
res = err.io
59+
warning("Can't load '#{url}' HTTP Error Code: '#{res.status[0]}'")
60+
args[1]
61+
end
62+
JSON.parse(contents) || args[1]
5163
end
52-
PSON.load(contents) || args[1]
5364
elsif File.exists?(args[0]) # rubocop:disable Lint/DeprecatedClassMethods : Changing to .exist? breaks the code
5465
content = File.read(args[0])
55-
PSON.load(content) || args[1]
66+
if Puppet::Util::Package.versioncmp(Puppet.version, '8.0.0').negative?
67+
PSON.load(content) || args[1]
68+
else
69+
JSON.parse(content) || args[1]
70+
end
5671
else
5772
warning("Can't load '#{args[0]}' File does not exist!")
5873
args[1]

spec/functions/loadjson_spec.rb

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@
2727

2828
before(:each) do
2929
allow(File).to receive(:exists?).with(filename).and_return(false).once
30-
allow(PSON).to receive(:load).never
30+
if Puppet::PUPPETVERSION[0].to_i < 8
31+
allow(PSON).to receive(:load).never
32+
else
33+
allow(JSON).to receive(:parse).never
34+
end
3135
end
3236
it { is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value') }
3337
it { is_expected.to run.with_params(filename, 'đẽƒằưļŧ' => '٧ẵłựέ').and_return('đẽƒằưļŧ' => '٧ẵłựέ') }
@@ -49,7 +53,11 @@
4953
allow(File).to receive(:exists?).with(filename).and_return(true).once
5054
allow(File).to receive(:read).with(filename).and_return(json).once
5155
allow(File).to receive(:read).with(filename).and_return(json).once
52-
allow(PSON).to receive(:load).with(json).and_return(data).once
56+
if Puppet::PUPPETVERSION[0].to_i < 8
57+
allow(PSON).to receive(:load).with(json).and_return(data).once
58+
else
59+
allow(JSON).to receive(:parse).with(json).and_return(data).once
60+
end
5361
end
5462
it { is_expected.to run.with_params(filename).and_return(data) }
5563
end
@@ -67,7 +75,11 @@
6775
before(:each) do
6876
allow(File).to receive(:exists?).with(filename).and_return(true).once
6977
allow(File).to receive(:read).with(filename).and_return(json).once
70-
allow(PSON).to receive(:load).with(json).once.and_raise StandardError, 'Something terrible have happened!'
78+
if Puppet::PUPPETVERSION[0].to_i < 8
79+
allow(PSON).to receive(:load).with(json).once.and_raise StandardError, 'Something terrible have happened!'
80+
else
81+
allow(JSON).to receive(:parse).with(json).once.and_raise StandardError, 'Something terrible have happened!'
82+
end
7183
end
7284
it { is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value') }
7385
end
@@ -80,8 +92,13 @@
8092
let(:json) { '{"key":"value", {"ķęŷ":"νậŀųề" }, {"キー":"値" }' }
8193

8294
it {
83-
expect(OpenURI).to receive(:open_uri).with(filename, {}).and_return(json)
84-
expect(PSON).to receive(:load).with(json).and_return(data).once
95+
if Puppet::PUPPETVERSION[0].to_i < 8
96+
expect(OpenURI).to receive(:open_uri).with(filename, {}).and_return(json)
97+
expect(PSON).to receive(:load).with(json).and_return(data).once
98+
else
99+
expect(URI).to receive(:open).with(filename).and_return(json)
100+
expect(JSON).to receive(:parse).with(json).and_return(data).once
101+
end
85102
is_expected.to run.with_params(filename).and_return(data)
86103
}
87104
end
@@ -96,8 +113,13 @@
96113
let(:json) { '{"key":"value", {"ķęŷ":"νậŀųề" }, {"キー":"値" }' }
97114

98115
it {
99-
expect(OpenURI).to receive(:open_uri).with(url_no_auth, basic_auth).and_return(json)
100-
expect(PSON).to receive(:load).with(json).and_return(data).once
116+
if Puppet::PUPPETVERSION[0].to_i < 8
117+
expect(OpenURI).to receive(:open_uri).with(url_no_auth, basic_auth).and_return(json)
118+
expect(PSON).to receive(:load).with(json).and_return(data).once
119+
else
120+
expect(URI).to receive(:open).with(url_no_auth, basic_auth).and_return(json)
121+
expect(JSON).to receive(:parse).with(json).and_return(data).once
122+
end
101123
is_expected.to run.with_params(filename).and_return(data)
102124
}
103125
end
@@ -112,8 +134,13 @@
112134
let(:json) { '{"key":"value", {"ķęŷ":"νậŀųề" }, {"キー":"値" }' }
113135

114136
it {
115-
expect(OpenURI).to receive(:open_uri).with(url_no_auth, basic_auth).and_return(json)
116-
expect(PSON).to receive(:load).with(json).and_return(data).once
137+
if Puppet::PUPPETVERSION[0].to_i < 8
138+
expect(OpenURI).to receive(:open_uri).with(url_no_auth, basic_auth).and_return(json)
139+
expect(PSON).to receive(:load).with(json).and_return(data).once
140+
else
141+
expect(URI).to receive(:open).with(url_no_auth, basic_auth).and_return(json)
142+
expect(JSON).to receive(:parse).with(json).and_return(data).once
143+
end
117144
is_expected.to run.with_params(filename).and_return(data)
118145
}
119146
end
@@ -125,8 +152,13 @@
125152
let(:json) { ',;{"key":"value"}' }
126153

127154
it {
128-
expect(OpenURI).to receive(:open_uri).with(filename, {}).and_return(json)
129-
expect(PSON).to receive(:load).with(json).once.and_raise StandardError, 'Something terrible have happened!'
155+
if Puppet::PUPPETVERSION[0].to_i < 8
156+
expect(OpenURI).to receive(:open_uri).with(filename, {}).and_return(json)
157+
expect(PSON).to receive(:load).with(json).once.and_raise StandardError, 'Something terrible have happened!'
158+
else
159+
expect(URI).to receive(:open).with(filename).and_return(json)
160+
expect(JSON).to receive(:parse).with(json).once.and_raise StandardError, 'Something terrible have happened!'
161+
end
130162
is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value')
131163
}
132164
end
@@ -137,7 +169,11 @@
137169
end
138170

139171
it {
140-
expect(OpenURI).to receive(:open_uri).with(filename, {}).and_raise OpenURI::HTTPError, '404 File not Found'
172+
if Puppet::PUPPETVERSION[0].to_i < 8
173+
expect(OpenURI).to receive(:open_uri).with(filename, {}).and_raise OpenURI::HTTPError, '404 File not Found'
174+
else
175+
expect(URI).to receive(:open).with(filename).and_raise URI::Error, '404 File not Found'
176+
end
141177
is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value')
142178
}
143179
end

0 commit comments

Comments
 (0)