loadyaml_spec.rb 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. require 'spec_helper_acceptance'
  2. tmpdir = default.tmpdir('stdlib')
  3. describe 'loadyaml function' do
  4. describe 'success' do
  5. shell("echo '---
  6. aaa: 1
  7. bbb: 2
  8. ccc: 3
  9. ddd: 4' > #{tmpdir}/test1yaml.yaml")
  10. pp1 = <<-DOC
  11. $o = loadyaml('#{tmpdir}/test1yaml.yaml')
  12. notice(inline_template('loadyaml[aaa] is <%= @o["aaa"].inspect %>'))
  13. notice(inline_template('loadyaml[bbb] is <%= @o["bbb"].inspect %>'))
  14. notice(inline_template('loadyaml[ccc] is <%= @o["ccc"].inspect %>'))
  15. notice(inline_template('loadyaml[ddd] is <%= @o["ddd"].inspect %>'))
  16. DOC
  17. regex_array = [%r{loadyaml\[aaa\] is 1}, %r{loadyaml\[bbb\] is 2}, %r{loadyaml\[ccc\] is 3}, %r{loadyaml\[ddd\] is 4}]
  18. it 'loadyamls array of values' do
  19. apply_manifest(pp1, :catch_failures => true) do |r|
  20. regex_array.each do |i|
  21. expect(r.stdout).to match(i)
  22. end
  23. end
  24. end
  25. pp2 = <<-DOC
  26. $o = loadyaml('#{tmpdir}/no-file.yaml', {'default' => 'value'})
  27. notice(inline_template('loadyaml[default] is <%= @o["default"].inspect %>'))
  28. DOC
  29. it 'returns the default value if there is no file to load' do
  30. apply_manifest(pp2, :catch_failures => true) do |r|
  31. expect(r.stdout).to match(%r{loadyaml\[default\] is "value"})
  32. end
  33. end
  34. shell("echo '!' > #{tmpdir}/test2yaml.yaml")
  35. pp3 = <<-DOC
  36. $o = loadyaml('#{tmpdir}/test2yaml.yaml', {'default' => 'value'})
  37. notice(inline_template('loadyaml[default] is <%= @o["default"].inspect %>'))
  38. DOC
  39. it 'returns the default value if the file was parsed with an error' do
  40. apply_manifest(pp3, :catch_failures => true) do |r|
  41. expect(r.stdout).to match(%r{loadyaml\[default\] is "value"})
  42. end
  43. end
  44. end
  45. describe 'failure' do
  46. it 'fails with no arguments'
  47. end
  48. end