|  | 
| 1 | 1 | # frozen_string_literal: true | 
| 2 | 2 | 
 | 
| 3 | 3 | require 'spec_helper' | 
|  | 4 | +require 'json' | 
|  | 5 | +require 'open-uri' | 
| 4 | 6 | 
 | 
| 5 | 7 | describe 'loadjson' do | 
| 6 | 8 |   it { is_expected.not_to eq(nil) } | 
|  | 
| 27 | 29 | 
 | 
| 28 | 30 |       before(:each) do | 
| 29 | 31 |         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 | 
| 31 | 37 |       end | 
| 32 | 38 |       it { is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value') } | 
| 33 | 39 |       it { is_expected.to run.with_params(filename, 'đẽƒằưļŧ' => '٧ẵłựέ').and_return('đẽƒằưļŧ' => '٧ẵłựέ') } | 
|  | 
| 49 | 55 |         allow(File).to receive(:exists?).with(filename).and_return(true).once | 
| 50 | 56 |         allow(File).to receive(:read).with(filename).and_return(json).once | 
| 51 | 57 |         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 | 
| 53 | 63 |       end | 
| 54 | 64 |       it { is_expected.to run.with_params(filename).and_return(data) } | 
| 55 | 65 |     end | 
|  | 
| 67 | 77 |       before(:each) do | 
| 68 | 78 |         allow(File).to receive(:exists?).with(filename).and_return(true).once | 
| 69 | 79 |         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 | 
| 71 | 85 |       end | 
| 72 | 86 |       it { is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value') } | 
| 73 | 87 |     end | 
|  | 
| 80 | 94 |       let(:json) { '{"key":"value", {"ķęŷ":"νậŀųề" }, {"キー":"値" }' } | 
| 81 | 95 | 
 | 
| 82 | 96 |       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 | 
| 85 | 104 |         is_expected.to run.with_params(filename).and_return(data) | 
| 86 | 105 |       } | 
| 87 | 106 |     end | 
|  | 
| 96 | 115 |       let(:json) { '{"key":"value", {"ķęŷ":"νậŀųề" }, {"キー":"値" }' } | 
| 97 | 116 | 
 | 
| 98 | 117 |       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 | 
| 101 | 125 |         is_expected.to run.with_params(filename).and_return(data) | 
| 102 | 126 |       } | 
| 103 | 127 |     end | 
|  | 
| 112 | 136 |       let(:json) { '{"key":"value", {"ķęŷ":"νậŀųề" }, {"キー":"値" }' } | 
| 113 | 137 | 
 | 
| 114 | 138 |       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 | 
| 117 | 146 |         is_expected.to run.with_params(filename).and_return(data) | 
| 118 | 147 |       } | 
| 119 | 148 |     end | 
|  | 
| 125 | 154 |       let(:json) { ',;{"key":"value"}' } | 
| 126 | 155 | 
 | 
| 127 | 156 |       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 | 
| 130 | 164 |         is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value') | 
| 131 | 165 |       } | 
| 132 | 166 |     end | 
|  | 
| 137 | 171 |       end | 
| 138 | 172 | 
 | 
| 139 | 173 |       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 | 
| 141 | 179 |         is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value') | 
| 142 | 180 |       } | 
| 143 | 181 |     end | 
|  | 
0 commit comments