1 | require 'spec_helper'
|
---|
2 |
|
---|
3 | describe 'loadjson' do
|
---|
4 | it { is_expected.not_to eq(nil) }
|
---|
5 | it { is_expected.to run.with_params().and_raise_error(ArgumentError, /wrong number of arguments/i) }
|
---|
6 |
|
---|
7 | describe "when calling with valid arguments" do
|
---|
8 | before :each do
|
---|
9 | if RSpec.configuration.puppet_future
|
---|
10 | allow(File).to receive(:read).with(/\/stdlib\/metadata.json/, {:encoding=>"utf-8"}).and_return('{"name": "puppetlabs-stdlib"}')
|
---|
11 | else
|
---|
12 | allow(File).to receive(:read).with(/\/stdlib\/metadata.json/).and_return('{"name": "puppetlabs-stdlib"}')
|
---|
13 | end
|
---|
14 | end
|
---|
15 |
|
---|
16 | context 'when a non-existing file is specified' do
|
---|
17 | let(:filename) { '/tmp/doesnotexist' }
|
---|
18 | before {
|
---|
19 | allow(File).to receive(:exists?).with(filename).and_return(false).once
|
---|
20 | allow(PSON).to receive(:load).never
|
---|
21 | }
|
---|
22 | it { is_expected.to run.with_params(filename, {'default' => 'value'}).and_return({'default' => 'value'}) }
|
---|
23 | end
|
---|
24 |
|
---|
25 | context 'when an existing file is specified' do
|
---|
26 | let(:filename) { '/tmp/doesexist' }
|
---|
27 | let(:data) { { 'key' => 'value' } }
|
---|
28 | let(:json) { '{"key":"value"}' }
|
---|
29 | before {
|
---|
30 | allow(File).to receive(:exists?).with(filename).and_return(true).once
|
---|
31 | allow(File).to receive(:read).with(filename).and_return(json).once
|
---|
32 | allow(File).to receive(:read).with(filename).and_return(json).once
|
---|
33 | allow(PSON).to receive(:load).with(json).and_return(data).once
|
---|
34 | }
|
---|
35 | it { is_expected.to run.with_params(filename).and_return(data) }
|
---|
36 | end
|
---|
37 |
|
---|
38 | context 'when the file could not be parsed' do
|
---|
39 | let(:filename) { '/tmp/doesexist' }
|
---|
40 | let(:json) { '{"key":"value"}' }
|
---|
41 | before {
|
---|
42 | allow(File).to receive(:exists?).with(filename).and_return(true).once
|
---|
43 | allow(File).to receive(:read).with(filename).and_return(json).once
|
---|
44 | allow(PSON).to receive(:load).with(json).once.and_raise StandardError, 'Something terrible have happened!'
|
---|
45 | }
|
---|
46 | it { is_expected.to run.with_params(filename, {'default' => 'value'}).and_return({'default' => 'value'}) }
|
---|
47 | end
|
---|
48 | end
|
---|
49 | end
|
---|