Skip to content

Commit

Permalink
Replace PSON with JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasAud committed May 16, 2023
1 parent 1e8f1e9 commit 3163631
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 19 deletions.
21 changes: 14 additions & 7 deletions lib/puppet/parser/functions/loadjson.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,23 @@ module Puppet::Parser::Functions
else
url = args[0]
end
begin
contents = OpenURI.open_uri(url, **http_options)
rescue OpenURI::HTTPError => err
res = err.io
warning("Can't load '#{url}' HTTP Error Code: '#{res.status[0]}'")
args[1]
end
if Puppet::Util::Package.versioncmp(Puppet.version, '8.0.0').negative?
begin
contents = OpenURI.open_uri(url, **http_options)
rescue OpenURI::HTTPError => err
res = err.io
warning("Can't load '#{url}' HTTP Error Code: '#{res.status[0]}'")
args[1]
end
PSON.load(contents) || args[1]
else
begin
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.
rescue URI::Error => err
res = err.io
warning("Can't load '#{url}' HTTP Error Code: '#{res.status[0]}'")
args[1]
end
JSON.parse(contents) || args[1]
end
elsif File.exists?(args[0]) # rubocop:disable Lint/DeprecatedClassMethods : Changing to .exist? breaks the code
Expand Down
62 changes: 50 additions & 12 deletions spec/functions/loadjson_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

require 'spec_helper'
require 'json'
require 'open-uri'

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

before(:each) do
allow(File).to receive(:exists?).with(filename).and_return(false).once
allow(PSON).to receive(:load).never
if Puppet::PUPPETVERSION[0].to_i < 8
allow(PSON).to receive(:load).never
else
allow(JSON).to receive(:parse).never
end
end
it { is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value') }
it { is_expected.to run.with_params(filename, 'đẽƒằưļŧ' => '٧ẵłựέ').and_return('đẽƒằưļŧ' => '٧ẵłựέ') }
Expand All @@ -49,7 +55,11 @@
allow(File).to receive(:exists?).with(filename).and_return(true).once
allow(File).to receive(:read).with(filename).and_return(json).once
allow(File).to receive(:read).with(filename).and_return(json).once
allow(PSON).to receive(:load).with(json).and_return(data).once
if Puppet::PUPPETVERSION[0].to_i < 8
allow(PSON).to receive(:load).with(json).and_return(data).once
else
allow(JSON).to receive(:parse).with(json).and_return(data).once
end
end
it { is_expected.to run.with_params(filename).and_return(data) }
end
Expand All @@ -67,7 +77,11 @@
before(:each) do
allow(File).to receive(:exists?).with(filename).and_return(true).once
allow(File).to receive(:read).with(filename).and_return(json).once
allow(PSON).to receive(:load).with(json).once.and_raise StandardError, 'Something terrible have happened!'
if Puppet::PUPPETVERSION[0].to_i < 8
allow(PSON).to receive(:load).with(json).once.and_raise StandardError, 'Something terrible have happened!'
else
allow(JSON).to receive(:parse).with(json).once.and_raise StandardError, 'Something terrible have happened!'
end
end
it { is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value') }
end
Expand All @@ -80,8 +94,13 @@
let(:json) { '{"key":"value", {"ķęŷ":"νậŀųề" }, {"キー":"値" }' }

it {
expect(OpenURI).to receive(:open_uri).with(filename, {}).and_return(json)
expect(PSON).to receive(:load).with(json).and_return(data).once
if Puppet::PUPPETVERSION[0].to_i < 8
expect(OpenURI).to receive(:open_uri).with(filename, {}).and_return(json)
expect(PSON).to receive(:load).with(json).and_return(data).once
else
expect(URI).to receive(:open).with(filename).and_return(json)
expect(JSON).to receive(:parse).with(json).and_return(data).once
end
is_expected.to run.with_params(filename).and_return(data)
}
end
Expand All @@ -96,8 +115,13 @@
let(:json) { '{"key":"value", {"ķęŷ":"νậŀųề" }, {"キー":"値" }' }

it {
expect(OpenURI).to receive(:open_uri).with(url_no_auth, basic_auth).and_return(json)
expect(PSON).to receive(:load).with(json).and_return(data).once
if Puppet::PUPPETVERSION[0].to_i < 8
expect(OpenURI).to receive(:open_uri).with(url_no_auth, basic_auth).and_return(json)
expect(PSON).to receive(:load).with(json).and_return(data).once
else
expect(URI).to receive(:open).with(url_no_auth, basic_auth).and_return(json)
expect(JSON).to receive(:parse).with(json).and_return(data).once
end
is_expected.to run.with_params(filename).and_return(data)
}
end
Expand All @@ -112,8 +136,13 @@
let(:json) { '{"key":"value", {"ķęŷ":"νậŀųề" }, {"キー":"値" }' }

it {
expect(OpenURI).to receive(:open_uri).with(url_no_auth, basic_auth).and_return(json)
expect(PSON).to receive(:load).with(json).and_return(data).once
if Puppet::PUPPETVERSION[0].to_i < 8
expect(OpenURI).to receive(:open_uri).with(url_no_auth, basic_auth).and_return(json)
expect(PSON).to receive(:load).with(json).and_return(data).once
else
expect(URI).to receive(:open).with(url_no_auth, basic_auth).and_return(json)
expect(JSON).to receive(:parse).with(json).and_return(data).once
end
is_expected.to run.with_params(filename).and_return(data)
}
end
Expand All @@ -125,8 +154,13 @@
let(:json) { ',;{"key":"value"}' }

it {
expect(OpenURI).to receive(:open_uri).with(filename, {}).and_return(json)
expect(PSON).to receive(:load).with(json).once.and_raise StandardError, 'Something terrible have happened!'
if Puppet::PUPPETVERSION[0].to_i < 8
expect(OpenURI).to receive(:open_uri).with(filename, {}).and_return(json)
expect(PSON).to receive(:load).with(json).once.and_raise StandardError, 'Something terrible have happened!'
else
expect(URI).to receive(:open).with(filename).and_return(json)
expect(JSON).to receive(:parse).with(json).once.and_raise StandardError, 'Something terrible have happened!'
end
is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value')
}
end
Expand All @@ -137,7 +171,11 @@
end

it {
expect(OpenURI).to receive(:open_uri).with(filename, {}).and_raise OpenURI::HTTPError, '404 File not Found'
if Puppet::PUPPETVERSION[0].to_i < 8
expect(OpenURI).to receive(:open_uri).with(filename, {}).and_raise OpenURI::HTTPError, '404 File not Found'
else
expect(URI).to receive(:open).with(filename).and_raise URI::Error, '404 File not Found'
end
is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value')
}
end
Expand Down

0 comments on commit 3163631

Please sign in to comment.