4 it { is_expected.not_to eq(nil) }
5 it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{wrong number of arguments}i) }
7 context 'when a non-existing file is specified' do
8 let(:filename) { '/tmp/doesnotexist' }
10 it "'default' => 'value'" do
11 expect(File).to receive(:exists?).with(filename).and_return(false).once
12 expect(YAML).to receive(:load_file).never
13 is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value')
17 context 'when an existing file is specified' do
18 let(:filename) { '/tmp/doesexist' }
19 let(:data) { { 'key' => 'value', 'ķęŷ' => 'νậŀųề', 'キー' => '値' } }
21 it "returns 'key' => 'value', 'ķęŷ' => 'νậŀųề', 'キー' => '値'" do
22 expect(File).to receive(:exists?).with(filename).and_return(true).once
23 expect(YAML).to receive(:load_file).with(filename).and_return(data).once
24 is_expected.to run.with_params(filename).and_return(data)
28 context 'when the file could not be parsed' do
29 let(:filename) { '/tmp/doesexist' }
31 it 'filename /tmp/doesexist' do
32 expect(File).to receive(:exists?).with(filename).and_return(true).once
33 allow(YAML).to receive(:load_file).with(filename).once.and_raise(StandardError, 'Something terrible have happened!')
34 is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value')
38 context 'when an existing URL is specified' do
39 let(:filename) { 'https://example.local/myhash.yaml' }
40 let(:basic_auth) { { :http_basic_authentication => ['', ''] } }
41 let(:yaml) { 'Dummy YAML' }
42 let(:data) { { 'key' => 'value', 'ķęŷ' => 'νậŀųề', 'キー' => '値' } }
45 expect(OpenURI).to receive(:open_uri).with(filename, basic_auth).and_return(yaml)
46 expect(YAML).to receive(:safe_load).with(yaml).and_return(data).once
47 is_expected.to run.with_params(filename).and_return(data)
51 context 'when an existing URL (with username and password) is specified' do
52 let(:filename) { 'https://user1:pass1@example.local/myhash.yaml' }
53 let(:url_no_auth) { 'https://example.local/myhash.yaml' }
54 let(:basic_auth) { { :http_basic_authentication => ['user1', 'pass1'] } }
55 let(:yaml) { 'Dummy YAML' }
56 let(:data) { { 'key' => 'value', 'ķęŷ' => 'νậŀųề', 'キー' => '値' } }
59 expect(OpenURI).to receive(:open_uri).with(url_no_auth, basic_auth).and_return(yaml)
60 expect(YAML).to receive(:safe_load).with(yaml).and_return(data).once
61 is_expected.to run.with_params(filename).and_return(data)
65 context 'when an existing URL (with username) is specified' do
66 let(:filename) { 'https://user1@example.local/myhash.yaml' }
67 let(:url_no_auth) { 'https://example.local/myhash.yaml' }
68 let(:basic_auth) { { :http_basic_authentication => ['user1', ''] } }
69 let(:yaml) { 'Dummy YAML' }
70 let(:data) { { 'key' => 'value', 'ķęŷ' => 'νậŀųề', 'キー' => '値' } }
73 expect(OpenURI).to receive(:open_uri).with(url_no_auth, basic_auth).and_return(yaml)
74 expect(YAML).to receive(:safe_load).with(yaml).and_return(data).once
75 is_expected.to run.with_params(filename).and_return(data)
79 context 'when an existing URL could not be parsed, with default specified' do
80 let(:filename) { 'https://example.local/myhash.yaml' }
81 let(:basic_auth) { { :http_basic_authentication => ['', ''] } }
82 let(:yaml) { 'Dummy YAML' }
85 expect(OpenURI).to receive(:open_uri).with(filename, basic_auth).and_return(yaml)
86 expect(YAML).to receive(:safe_load).with(yaml).and_raise StandardError, 'Cannot parse data'
87 is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value')
91 context 'when a URL does not exist, with default specified' do
92 let(:filename) { 'https://example.local/myhash.yaml' }
93 let(:basic_auth) { { :http_basic_authentication => ['', ''] } }
94 let(:yaml) { 'Dummy YAML' }
97 expect(OpenURI).to receive(:open_uri).with(filename, basic_auth).and_raise OpenURI::HTTPError, '404 File not Found'
98 is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value')