loadjson_spec.rb 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. require 'spec_helper'
  2. describe 'loadjson' do
  3. it { is_expected.not_to eq(nil) }
  4. it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{wrong number of arguments}i) }
  5. describe 'when calling with valid arguments' do
  6. before :each do
  7. allow(File).to receive(:read).with(%r{\/(stdlib|test)\/metadata.json}, :encoding => 'utf-8').and_return('{"name": "puppetlabs-stdlib"}')
  8. allow(File).to receive(:read).with(%r{\/(stdlib|test)\/metadata.json}).and_return('{"name": "puppetlabs-stdlib"}')
  9. end
  10. context 'when a non-existing file is specified' do
  11. let(:filename) do
  12. if Puppet::Util::Platform.windows?
  13. 'C:/tmp/doesnotexist'
  14. else
  15. '/tmp/doesnotexist'
  16. end
  17. end
  18. before(:each) do
  19. allow(File).to receive(:exists?).with(filename).and_return(false).once
  20. allow(PSON).to receive(:load).never
  21. end
  22. it { is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value') }
  23. it { is_expected.to run.with_params(filename, 'đẽƒằưļŧ' => '٧ẵłựέ').and_return('đẽƒằưļŧ' => '٧ẵłựέ') }
  24. it { is_expected.to run.with_params(filename, 'デフォルト' => '値').and_return('デフォルト' => '値') }
  25. end
  26. context 'when an existing file is specified' do
  27. let(:filename) do
  28. if Puppet::Util::Platform.windows?
  29. 'C:/tmp/doesexist'
  30. else
  31. '/tmp/doesexist'
  32. end
  33. end
  34. let(:data) { { 'key' => 'value', 'ķęŷ' => 'νậŀųề', 'キー' => '値' } }
  35. let(:json) { '{"key":"value", {"ķęŷ":"νậŀųề" }, {"キー":"値" }' }
  36. before(:each) do
  37. allow(File).to receive(:exists?).with(filename).and_return(true).once
  38. allow(File).to receive(:read).with(filename).and_return(json).once
  39. allow(File).to receive(:read).with(filename).and_return(json).once
  40. allow(PSON).to receive(:load).with(json).and_return(data).once
  41. end
  42. it { is_expected.to run.with_params(filename).and_return(data) }
  43. end
  44. context 'when the file could not be parsed' do
  45. let(:filename) do
  46. if Puppet::Util::Platform.windows?
  47. 'C:/tmp/doesexist'
  48. else
  49. '/tmp/doesexist'
  50. end
  51. end
  52. let(:json) { '{"key":"value"}' }
  53. before(:each) do
  54. allow(File).to receive(:exists?).with(filename).and_return(true).once
  55. allow(File).to receive(:read).with(filename).and_return(json).once
  56. allow(PSON).to receive(:load).with(json).once.and_raise StandardError, 'Something terrible have happened!'
  57. end
  58. it { is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value') }
  59. end
  60. end
  61. end