|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | 3 | require 'spec_helper' |
| 4 | +require 'open-uri' |
4 | 5 |
|
5 | 6 | describe 'loadjson' do |
6 | 7 | it { is_expected.not_to be_nil } |
7 | | - it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{wrong number of arguments}i) } |
| 8 | + it { is_expected.to run.with_params.and_raise_error(ArgumentError, "'loadjson' expects between 1 and 2 arguments, got none") } |
8 | 9 |
|
9 | 10 | describe 'when calling with valid arguments' do |
10 | | - before :each do |
11 | | - # In Puppet 7, there are two prior calls to File.read prior to the responses we want to mock |
12 | | - allow(File).to receive(:read).with(anything, anything).and_call_original |
13 | | - allow(File).to receive(:read).with(%r{/(stdlib|test)/metadata.json}, encoding: 'utf-8').and_return('{"name": "puppetlabs-stdlib"}') |
14 | | - allow(File).to receive(:read).with(%r{/(stdlib|test)/metadata.json}).and_return('{"name": "puppetlabs-stdlib"}') |
15 | | - # Additional modules used by litmus which are identified while running these dues to being in fixtures |
16 | | - allow(File).to receive(:read).with(%r{/(provision|puppet_agent|facts)/metadata.json}, encoding: 'utf-8') |
17 | | - end |
18 | | - |
19 | 11 | context 'when a non-existing file is specified' do |
20 | 12 | let(:filename) do |
21 | | - if Puppet::Util::Platform.windows? |
22 | | - 'C:/tmp/doesnotexist' |
23 | | - else |
24 | | - '/tmp/doesnotexist' |
25 | | - end |
| 13 | + file = Tempfile.create |
| 14 | + file.close |
| 15 | + File.unlink(file.path) |
| 16 | + file.path |
26 | 17 | end |
27 | 18 |
|
28 | 19 | before(:each) do |
29 | | - allow(File).to receive(:exist?).and_call_original |
30 | | - allow(File).to receive(:exist?).with(filename).and_return(false).once |
31 | 20 | if Puppet::PUPPETVERSION[0].to_i < 8 |
32 | 21 | allow(PSON).to receive(:load).never # rubocop:disable RSpec/ReceiveNever Switching to not_to receive breaks testing in this case |
33 | 22 | else |
34 | 23 | allow(JSON).to receive(:parse).never # rubocop:disable RSpec/ReceiveNever |
35 | 24 | end |
36 | 25 | end |
37 | 26 |
|
38 | | - it { is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value') } |
39 | | - it { is_expected.to run.with_params(filename, 'đẽƒằưļŧ' => '٧ẵłựέ').and_return('đẽƒằưļŧ' => '٧ẵłựέ') } |
40 | | - it { is_expected.to run.with_params(filename, 'デフォルト' => '値').and_return('デフォルト' => '値') } |
| 27 | + it { is_expected.to run.with_params(filename, {'default' => 'value'}).and_return({'default' => 'value'}) } |
| 28 | + it { is_expected.to run.with_params(filename, {'đẽƒằưļŧ' => '٧ẵłựέ'}).and_return({'đẽƒằưļŧ' => '٧ẵłựέ'}) } |
| 29 | + it { is_expected.to run.with_params(filename, {'デフォルト' => '値'}).and_return({'デフォルト' => '値'}) } |
41 | 30 | end |
42 | 31 |
|
43 | 32 | context 'when an existing file is specified' do |
44 | | - let(:filename) do |
45 | | - if Puppet::Util::Platform.windows? |
46 | | - 'C:/tmp/doesexist' |
47 | | - else |
48 | | - '/tmp/doesexist' |
49 | | - end |
50 | | - end |
51 | 33 | let(:data) { { 'key' => 'value', 'ķęŷ' => 'νậŀųề', 'キー' => '値' } } |
52 | 34 | let(:json) { '{"key":"value", {"ķęŷ":"νậŀųề" }, {"キー":"値" }' } |
53 | 35 |
|
54 | | - before(:each) do |
55 | | - allow(File).to receive(:exist?).and_call_original |
56 | | - allow(File).to receive(:exist?).with(filename).and_return(true).once |
57 | | - allow(File).to receive(:read).with(filename).and_return(json).once |
58 | | - allow(File).to receive(:read).with(filename).and_return(json).once |
59 | | - if Puppet::PUPPETVERSION[0].to_i < 8 |
60 | | - allow(PSON).to receive(:load).with(json).and_return(data).once |
61 | | - else |
62 | | - allow(JSON).to receive(:parse).with(json).and_return(data).once |
| 36 | + it do |
| 37 | + Tempfile.new do |file| |
| 38 | + file.write(json) |
| 39 | + file.flush |
| 40 | + |
| 41 | + is_expected.to run.with_params(file.path).and_return(data) |
63 | 42 | end |
64 | 43 | end |
65 | | - |
66 | | - it { is_expected.to run.with_params(filename).and_return(data) } |
67 | 44 | end |
68 | 45 |
|
69 | 46 | context 'when the file could not be parsed' do |
70 | | - let(:filename) do |
71 | | - if Puppet::Util::Platform.windows? |
72 | | - 'C:/tmp/doesexist' |
73 | | - else |
74 | | - '/tmp/doesexist' |
75 | | - end |
76 | | - end |
77 | 47 | let(:json) { '{"key":"value"}' } |
78 | 48 |
|
79 | | - before(:each) do |
80 | | - allow(File).to receive(:exist?).and_call_original |
81 | | - allow(File).to receive(:exist?).with(filename).and_return(true).once |
82 | | - allow(File).to receive(:read).with(filename).and_return(json).once |
83 | | - if Puppet::PUPPETVERSION[0].to_i < 8 |
84 | | - allow(PSON).to receive(:load).with(json).once.and_raise StandardError, 'Something terrible have happened!' |
85 | | - else |
86 | | - allow(JSON).to receive(:parse).with(json).once.and_raise StandardError, 'Something terrible have happened!' |
87 | | - end |
88 | | - end |
89 | | - |
90 | | - it { is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value') } |
91 | | - end |
92 | | - |
93 | | - context 'when an existing URL is specified' do |
94 | | - let(:filename) do |
95 | | - 'https://example.local/myhash.json' |
96 | | - end |
97 | | - let(:data) { { 'key' => 'value', 'ķęŷ' => 'νậŀųề', 'キー' => '値' } } |
98 | | - let(:json) { '{"key":"value", {"ķęŷ":"νậŀųề" }, {"キー":"値" }' } |
| 49 | + it do |
| 50 | + Tempfile.new do |file| |
| 51 | + file.write(json) |
| 52 | + file.flush |
99 | 53 |
|
100 | | - it { |
101 | | - expect(OpenURI).to receive(:open_uri).with(filename, {}).and_return(json) |
102 | | - if Puppet::PUPPETVERSION[0].to_i < 8 |
103 | | - expect(PSON).to receive(:load).with(json).and_return(data).once |
104 | | - else |
105 | | - expect(JSON).to receive(:parse).with(json).and_return(data).once |
| 54 | + is_expected.to run.with_params(filename, {'default' => 'value'}).and_return({'default' => 'value'}) |
106 | 55 | end |
107 | | - expect(subject).to run.with_params(filename).and_return(data) |
108 | | - } |
109 | | - end |
110 | | - |
111 | | - context 'when an existing URL (with username and password) is specified' do |
112 | | - let(:filename) do |
113 | | - 'https://user1:pass1@example.local/myhash.json' |
114 | 56 | end |
115 | | - let(:url_no_auth) { 'https://example.local/myhash.json' } |
116 | | - let(:basic_auth) { { http_basic_authentication: ['user1', 'pass1'] } } |
117 | | - let(:data) { { 'key' => 'value', 'ķęŷ' => 'νậŀųề', 'キー' => '値' } } |
118 | | - let(:json) { '{"key":"value", {"ķęŷ":"νậŀųề" }, {"キー":"値" }' } |
119 | | - |
120 | | - it { |
121 | | - expect(OpenURI).to receive(:open_uri).with(url_no_auth, basic_auth).and_return(json) |
122 | | - if Puppet::PUPPETVERSION[0].to_i < 8 |
123 | | - expect(PSON).to receive(:load).with(json).and_return(data).once |
124 | | - else |
125 | | - expect(JSON).to receive(:parse).with(json).and_return(data).once |
126 | | - end |
127 | | - expect(subject).to run.with_params(filename).and_return(data) |
128 | | - } |
129 | 57 | end |
130 | 58 |
|
131 | | - context 'when an existing URL (with username) is specified' do |
| 59 | + context 'when an existing URL is specified' do |
132 | 60 | let(:filename) do |
133 | | - 'https://user1@example.local/myhash.json' |
| 61 | + 'https://example.com/myhash.json' |
134 | 62 | end |
135 | | - let(:url_no_auth) { 'https://example.local/myhash.json' } |
136 | | - let(:basic_auth) { { http_basic_authentication: ['user1', ''] } } |
137 | 63 | let(:data) { { 'key' => 'value', 'ķęŷ' => 'νậŀųề', 'キー' => '値' } } |
138 | | - let(:json) { '{"key":"value", {"ķęŷ":"νậŀųề" }, {"キー":"値" }' } |
| 64 | + let(:json) { '{"key":"value", "ķęŷ":"νậŀųề", "キー":"値" }' } |
139 | 65 |
|
140 | 66 | it { |
141 | | - expect(OpenURI).to receive(:open_uri).with(url_no_auth, basic_auth).and_return(json) |
142 | | - if Puppet::PUPPETVERSION[0].to_i < 8 |
143 | | - expect(PSON).to receive(:load).with(json).and_return(data).once |
144 | | - else |
145 | | - expect(JSON).to receive(:parse).with(json).and_return(data).once |
146 | | - end |
| 67 | + expect(URI).to receive(:open).with(filename).and_yield(json) |
147 | 68 | expect(subject).to run.with_params(filename).and_return(data) |
148 | 69 | } |
149 | 70 | end |
150 | 71 |
|
151 | 72 | context 'when the URL output could not be parsed, with default specified' do |
152 | 73 | let(:filename) do |
153 | | - 'https://example.local/myhash.json' |
| 74 | + 'https://example.com/myhash.json' |
154 | 75 | end |
155 | 76 | let(:json) { ',;{"key":"value"}' } |
156 | 77 |
|
157 | 78 | it { |
158 | | - expect(OpenURI).to receive(:open_uri).with(filename, {}).and_return(json) |
| 79 | + expect(URI).to receive(:open).with(filename).and_yield(json) |
159 | 80 | if Puppet::PUPPETVERSION[0].to_i < 8 |
160 | 81 | expect(PSON).to receive(:load).with(json).once.and_raise StandardError, 'Something terrible have happened!' |
161 | 82 | else |
162 | | - expect(JSON).to receive(:parse).with(json).once.and_raise StandardError, 'Something terrible have happened!' |
| 83 | + expect(JSON).to receive(:load).with(json).once.and_raise StandardError, 'Something terrible have happened!' |
163 | 84 | end |
164 | | - expect(subject).to run.with_params(filename, 'default' => 'value').and_return('default' => 'value') |
| 85 | + expect(subject).to run.with_params(filename, {'default' => 'value'}).and_return({'default' => 'value'}) |
165 | 86 | } |
166 | 87 | end |
167 | 88 |
|
168 | 89 | context 'when the URL does not exist, with default specified' do |
169 | 90 | let(:filename) do |
170 | | - 'https://example.local/myhash.json' |
| 91 | + 'https://example.com/myhash.json' |
171 | 92 | end |
172 | 93 |
|
173 | 94 | it { |
174 | | - expect(OpenURI).to receive(:open_uri).with(filename, {}).and_raise OpenURI::HTTPError, '404 File not Found' |
175 | | - expect(subject).to run.with_params(filename, 'default' => 'value').and_return('default' => 'value') |
| 95 | + expect(URI).to receive(:open).with(filename).and_raise OpenURI::HTTPError, '404 File not Found' |
| 96 | + expect(subject).to run.with_params(filename, {'default' => 'value'}).and_return({'default' => 'value'}) |
176 | 97 | } |
177 | 98 | end |
178 | 99 | end |
|
0 commit comments