1 #! /usr/bin/env ruby -S rspec
2 require 'spec_helper_acceptance'
4 tmpdir = default.tmpdir('stdlib')
6 describe 'loadjson function' do
8 it 'loadjsons array of values' do
9 shell("echo '{\"aaa\":1,\"bbb\":2,\"ccc\":3,\"ddd\":4}' > #{tmpdir}/testjson.json")
11 $o = loadjson('#{tmpdir}/testjson.json')
12 notice(inline_template('loadjson[aaa] is <%= @o["aaa"].inspect %>'))
13 notice(inline_template('loadjson[bbb] is <%= @o["bbb"].inspect %>'))
14 notice(inline_template('loadjson[ccc] is <%= @o["ccc"].inspect %>'))
15 notice(inline_template('loadjson[ddd] is <%= @o["ddd"].inspect %>'))
18 apply_manifest(pp, :catch_failures => true) do |r|
19 expect(r.stdout).to match(/loadjson\[aaa\] is 1/)
20 expect(r.stdout).to match(/loadjson\[bbb\] is 2/)
21 expect(r.stdout).to match(/loadjson\[ccc\] is 3/)
22 expect(r.stdout).to match(/loadjson\[ddd\] is 4/)
26 it 'returns the default value if there is no file to load' do
28 $o = loadjson('#{tmpdir}/no-file.json', {'default' => 'value'})
29 notice(inline_template('loadjson[default] is <%= @o["default"].inspect %>'))
32 apply_manifest(pp, :catch_failures => true) do |r|
33 expect(r.stdout).to match(/loadjson\[default\] is "value"/)
37 it 'returns the default value if the file was parsed with an error' do
38 shell("echo '!' > #{tmpdir}/testjson.json")
40 $o = loadjson('#{tmpdir}/testjson.json', {'default' => 'value'})
41 notice(inline_template('loadjson[default] is <%= @o["default"].inspect %>'))
44 apply_manifest(pp, :catch_failures => true) do |r|
45 expect(r.stdout).to match(/loadjson\[default\] is "value"/)
50 it 'fails with no arguments'