Update puppetlabs/stdlib module
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / spec / functions / load_module_metadata_spec.rb
1 require 'spec_helper'
2
3 describe 'load_module_metadata' do
4   it { is_expected.not_to eq(nil) }
5   it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) }
6   it { is_expected.to run.with_params('one', 'two', 'three').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) }
7
8   describe 'when calling with valid arguments' do
9     before :each do
10       allow(File).to receive(:read).with(%r{\/(stdlib|test)\/metadata.json}, :encoding => 'utf-8').and_return('{"name": "puppetlabs-stdlib"}')
11       allow(File).to receive(:read).with(%r{\/(stdlib|test)\/metadata.json}).and_return('{"name": "puppetlabs-stdlib"}')
12     end
13
14     context 'when calling with valid utf8 and double byte character arguments' do
15       before :each do
16         allow(File).to receive(:read).with(%r{\/(stdlib|test)\/metadata.json}, :encoding => 'utf-8').and_return('{"ĭďèʼnţĩƒіểя": "ċơņťęאּť ỡƒ ţħíš -
17 この文字"}')
18         allow(File).to receive(:read).with(%r{\/(stdlib|test)\/metadata.json}).and_return('{"ĭďèʼnţĩƒіểя": "ċơņťęאּť ỡƒ ţħíš -
19 この文字"}')
20       end
21
22       let(:prefix) { 'C:' if Puppet::Util::Platform.windows? }
23
24       it 'jsons parse the file' do
25         allow(scope).to receive(:function_get_module_path).with(['science']).and_return("#{prefix}/path/to/module/")
26         allow(File).to receive(:exists?).with("#{prefix}/path/to/module/metadata.json").and_return(true)
27         allow(File).to receive(:read).with("#{prefix}/path/to/module/metadata.json").and_return('{"name": "spencer-science"}')
28
29         result = subject.execute('science')
30         expect(result['name']).to eq('spencer-science')
31       end
32
33       it 'fails by default if there is no metadata.json' do
34         allow(scope).to receive(:function_get_module_path).with(['science']).and_return("#{prefix}/path/to/module/")
35         allow(File).to receive(:exists?).with("#{prefix}/path/to/module/metadata.json").and_return(false)
36         expect { subject.call(['science']) }.to raise_error(Puppet::ParseError)
37       end
38
39       it 'returns nil if user allows empty metadata.json' do
40         allow(scope).to receive(:function_get_module_path).with(['science']).and_return("#{prefix}/path/to/module/")
41         allow(File).to receive(:exists?).with("#{prefix}/path/to/module/metadata.json").and_return(false)
42         result = subject.execute('science', true)
43         expect(result).to eq({})
44       end
45     end
46   end
47 end