Skip to content

Commit 3163631

Browse files
committed
Replace PSON with JSON
1 parent 1e8f1e9 commit 3163631

File tree

2 files changed

+64
-19
lines changed

2 files changed

+64
-19
lines changed

lib/puppet/parser/functions/loadjson.rb

+14-7
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,23 @@ 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]
51-
end
5245
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
5353
PSON.load(contents) || args[1]
5454
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
5562
JSON.parse(contents) || args[1]
5663
end
5764
elsif File.exists?(args[0]) # rubocop:disable Lint/DeprecatedClassMethods : Changing to .exist? breaks the code

spec/functions/loadjson_spec.rb

+50-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# frozen_string_literal: true
22

33
require 'spec_helper'
4+
require 'json'
5+
require 'open-uri'
46

57
describe 'loadjson' do
68
it { is_expected.not_to eq(nil) }
@@ -27,7 +29,11 @@
2729

2830
before(:each) do
2931
allow(File).to receive(:exists?).with(filename).and_return(false).once
30-
allow(PSON).to receive(:load).never
32+
if Puppet::PUPPETVERSION[0].to_i < 8
33+
allow(PSON).to receive(:load).never
34+
else
35+
allow(JSON).to receive(:parse).never
36+
end
3137
end
3238
it { is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value') }
3339
it { is_expected.to run.with_params(filename, 'đẽƒằưļŧ' => '٧ẵłựέ').and_return('đẽƒằưļŧ' => '٧ẵłựέ') }
@@ -49,7 +55,11 @@
4955
allow(File).to receive(:exists?).with(filename).and_return(true).once
5056
allow(File).to receive(:read).with(filename).and_return(json).once
5157
allow(File).to receive(:read).with(filename).and_return(json).once
52-
allow(PSON).to receive(:load).with(json).and_return(data).once
58+
if Puppet::PUPPETVERSION[0].to_i < 8
59+
allow(PSON).to receive(:load).with(json).and_return(data).once
60+
else
61+
allow(JSON).to receive(:parse).with(json).and_return(data).once
62+
end
5363
end
5464
it { is_expected.to run.with_params(filename).and_return(data) }
5565
end
@@ -67,7 +77,11 @@
6777
before(:each) do
6878
allow(File).to receive(:exists?).with(filename).and_return(true).once
6979
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!'
80+
if Puppet::PUPPETVERSION[0].to_i < 8
81+
allow(PSON).to receive(:load).with(json).once.and_raise StandardError, 'Something terrible have happened!'
82+
else
83+
allow(JSON).to receive(:parse).with(json).once.and_raise StandardError, 'Something terrible have happened!'
84+
end
7185
end
7286
it { is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value') }
7387
end
@@ -80,8 +94,13 @@
8094
let(:json) { '{"key":"value", {"ķęŷ":"νậŀųề" }, {"キー":"値" }' }
8195

8296
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
97+
if Puppet::PUPPETVERSION[0].to_i < 8
98+
expect(OpenURI).to receive(:open_uri).with(filename, {}).and_return(json)
99+
expect(PSON).to receive(:load).with(json).and_return(data).once
100+
else
101+
expect(URI).to receive(:open).with(filename).and_return(json)
102+
expect(JSON).to receive(:parse).with(json).and_return(data).once
103+
end
85104
is_expected.to run.with_params(filename).and_return(data)
86105
}
87106
end
@@ -96,8 +115,13 @@
96115
let(:json) { '{"key":"value", {"ķęŷ":"νậŀųề" }, {"キー":"値" }' }
97116

98117
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
118+
if Puppet::PUPPETVERSION[0].to_i < 8
119+
expect(OpenURI).to receive(:open_uri).with(url_no_auth, basic_auth).and_return(json)
120+
expect(PSON).to receive(:load).with(json).and_return(data).once
121+
else
122+
expect(URI).to receive(:open).with(url_no_auth, basic_auth).and_return(json)
123+
expect(JSON).to receive(:parse).with(json).and_return(data).once
124+
end
101125
is_expected.to run.with_params(filename).and_return(data)
102126
}
103127
end
@@ -112,8 +136,13 @@
112136
let(:json) { '{"key":"value", {"ķęŷ":"νậŀųề" }, {"キー":"値" }' }
113137

114138
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
139+
if Puppet::PUPPETVERSION[0].to_i < 8
140+
expect(OpenURI).to receive(:open_uri).with(url_no_auth, basic_auth).and_return(json)
141+
expect(PSON).to receive(:load).with(json).and_return(data).once
142+
else
143+
expect(URI).to receive(:open).with(url_no_auth, basic_auth).and_return(json)
144+
expect(JSON).to receive(:parse).with(json).and_return(data).once
145+
end
117146
is_expected.to run.with_params(filename).and_return(data)
118147
}
119148
end
@@ -125,8 +154,13 @@
125154
let(:json) { ',;{"key":"value"}' }
126155

127156
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!'
157+
if Puppet::PUPPETVERSION[0].to_i < 8
158+
expect(OpenURI).to receive(:open_uri).with(filename, {}).and_return(json)
159+
expect(PSON).to receive(:load).with(json).once.and_raise StandardError, 'Something terrible have happened!'
160+
else
161+
expect(URI).to receive(:open).with(filename).and_return(json)
162+
expect(JSON).to receive(:parse).with(json).once.and_raise StandardError, 'Something terrible have happened!'
163+
end
130164
is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value')
131165
}
132166
end
@@ -137,7 +171,11 @@
137171
end
138172

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

0 commit comments

Comments
 (0)