|
12 | 12 | it "'default' => 'value'" do
|
13 | 13 | allow(File).to receive(:exist?).and_call_original
|
14 | 14 | expect(File).to receive(:exist?).with(filename).and_return(false).once
|
15 |
| - expect(YAML).not_to receive(:load_file) |
| 15 | + expect(YAML).not_to receive(:safe_load) |
16 | 16 | expect(subject).to run.with_params(filename, 'default' => 'value').and_return('default' => 'value')
|
17 | 17 | end
|
18 | 18 | end
|
19 | 19 |
|
20 | 20 | context 'when an existing file is specified' do
|
21 |
| - let(:filename) { '/tmp/doesexist' } |
| 21 | + let(:tempfile) { Tempfile.new } |
| 22 | + let(:filename) { tempfile.path } |
22 | 23 | let(:data) { { 'key' => 'value', 'ķęŷ' => 'νậŀųề', 'キー' => '値' } }
|
| 24 | + let(:yaml) do |
| 25 | + <<~YAML |
| 26 | + key: 'value' |
| 27 | + ķęŷ: 'νậŀųề' |
| 28 | + キー: '値' |
| 29 | + YAML |
| 30 | + end |
23 | 31 |
|
24 | 32 | it "returns 'key' => 'value', 'ķęŷ' => 'νậŀųề', 'キー' => '値'" do
|
| 33 | + tempfile.write(yaml) |
| 34 | + tempfile.rewind |
25 | 35 | allow(File).to receive(:exist?).and_call_original
|
26 | 36 | expect(File).to receive(:exist?).with(filename).and_return(true).once
|
27 |
| - expect(YAML).to receive(:load_file).with(filename).and_return(data).once |
| 37 | + expect(YAML).to receive(:safe_load).and_call_original |
28 | 38 | expect(subject).to run.with_params(filename).and_return(data)
|
29 | 39 | end
|
30 | 40 | end
|
31 | 41 |
|
32 |
| - context 'when the file could not be parsed' do |
33 |
| - let(:filename) { '/tmp/doesexist' } |
| 42 | + context 'when the file could not be parsed, with default specified' do |
| 43 | + let(:tempfile) { Tempfile.new } |
| 44 | + let(:filename) { tempfile.path } |
| 45 | + let(:yaml) do |
| 46 | + <<~YAML |
| 47 | + ,,,, |
| 48 | + YAML |
| 49 | + end |
34 | 50 |
|
35 |
| - it 'filename /tmp/doesexist' do |
| 51 | + it 'is expected to return the default value' do |
| 52 | + tempfile.write(yaml) |
| 53 | + tempfile.rewind |
36 | 54 | allow(File).to receive(:exist?).and_call_original
|
37 | 55 | expect(File).to receive(:exist?).with(filename).and_return(true).once
|
38 |
| - allow(YAML).to receive(:load_file).with(filename).once.and_raise(StandardError, 'Something terrible have happened!') |
| 56 | + allow(YAML).to receive(:safe_load).with(yaml, aliases: true).once.and_raise(StandardError, 'Something terrible have happened!') |
39 | 57 | expect(subject).to run.with_params(filename, 'default' => 'value').and_return('default' => 'value')
|
40 | 58 | end
|
41 | 59 | end
|
|
48 | 66 |
|
49 | 67 | it {
|
50 | 68 | expect(OpenURI).to receive(:open_uri).with(filename, basic_auth).and_return(yaml)
|
51 |
| - expect(YAML).to receive(:safe_load).with(yaml).and_return(data).once |
| 69 | + expect(YAML).to receive(:safe_load).with(yaml, aliases: true).and_return(data).once |
52 | 70 | expect(subject).to run.with_params(filename).and_return(data)
|
53 | 71 | }
|
54 | 72 | end
|
|
62 | 80 |
|
63 | 81 | it {
|
64 | 82 | expect(OpenURI).to receive(:open_uri).with(url_no_auth, basic_auth).and_return(yaml)
|
65 |
| - expect(YAML).to receive(:safe_load).with(yaml).and_return(data).once |
| 83 | + expect(YAML).to receive(:safe_load).with(yaml, aliases: true).and_return(data).once |
66 | 84 | expect(subject).to run.with_params(filename).and_return(data)
|
67 | 85 | }
|
68 | 86 | end
|
|
76 | 94 |
|
77 | 95 | it {
|
78 | 96 | expect(OpenURI).to receive(:open_uri).with(url_no_auth, basic_auth).and_return(yaml)
|
79 |
| - expect(YAML).to receive(:safe_load).with(yaml).and_return(data).once |
| 97 | + expect(YAML).to receive(:safe_load).with(yaml, aliases: true).and_return(data).once |
80 | 98 | expect(subject).to run.with_params(filename).and_return(data)
|
81 | 99 | }
|
82 | 100 | end
|
|
88 | 106 |
|
89 | 107 | it {
|
90 | 108 | expect(OpenURI).to receive(:open_uri).with(filename, basic_auth).and_return(yaml)
|
91 |
| - expect(YAML).to receive(:safe_load).with(yaml).and_raise StandardError, 'Cannot parse data' |
| 109 | + expect(YAML).to receive(:safe_load).with(yaml, aliases: true).and_raise StandardError, 'Cannot parse data' |
92 | 110 | expect(subject).to run.with_params(filename, 'default' => 'value').and_return('default' => 'value')
|
93 | 111 | }
|
94 | 112 | end
|
|
103 | 121 | expect(subject).to run.with_params(filename, 'default' => 'value').and_return('default' => 'value')
|
104 | 122 | }
|
105 | 123 | end
|
| 124 | + |
| 125 | + context 'when the file contains aliases' do |
| 126 | + let(:tempfile) { Tempfile.new } |
| 127 | + let(:filename) { tempfile.path } |
| 128 | + let(:yaml) do |
| 129 | + <<~YAML |
| 130 | + some_numbers: &nums |
| 131 | + - one |
| 132 | + - two |
| 133 | + more_numbers: *nums |
| 134 | + YAML |
| 135 | + end |
| 136 | + let(:data) { { 'some_numbers' => ['one', 'two'], 'more_numbers' => ['one', 'two'] } } |
| 137 | + |
| 138 | + it 'parses the aliases' do |
| 139 | + tempfile.write(yaml) |
| 140 | + tempfile.rewind |
| 141 | + expect(subject).to run.with_params(filename).and_return(data) |
| 142 | + end |
| 143 | + end |
| 144 | + |
| 145 | + context 'when a URL returns yaml with aliases' do |
| 146 | + let(:filename) { 'https://example.local/myhash.yaml' } |
| 147 | + let(:basic_auth) { { http_basic_authentication: ['', ''] } } |
| 148 | + let(:yaml) do |
| 149 | + <<~YAML |
| 150 | + some_numbers: &nums |
| 151 | + - one |
| 152 | + - two |
| 153 | + more_numbers: *nums |
| 154 | + YAML |
| 155 | + end |
| 156 | + let(:data) { { 'some_numbers' => ['one', 'two'], 'more_numbers' => ['one', 'two'] } } |
| 157 | + |
| 158 | + it { |
| 159 | + expect(OpenURI).to receive(:open_uri).with(filename, basic_auth).and_return(yaml) |
| 160 | + expect(subject).to run.with_params(filename).and_return(data) |
| 161 | + } |
| 162 | + end |
106 | 163 | end
|
0 commit comments