Update stdlib
[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, /wrong number of arguments/i) }
6   it { is_expected.to run.with_params("one", "two", "three").and_raise_error(Puppet::ParseError, /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(/\/(stdlib|test)\/metadata.json/, {:encoding=>"utf-8"}).and_return('{"name": "puppetlabs-stdlib"}')
11       allow(File).to receive(:read).with(/\/(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(/\/(stdlib|test)\/metadata.json/, {:encoding=>"utf-8"}).and_return('{"ĭďèʼnţĩƒіểя": "ċơņťęאּť ỡƒ ţħíš -
17 この文字"}')
18         allow(File).to receive(:read).with(/\/(stdlib|test)\/metadata.json/).and_return('{"ĭďèʼnţĩƒіểя": "ċơņťęאּť ỡƒ ţħíš -
19 この文字"}')
20       end
21
22     it "should json parse the file" do
23       if Puppet::Util::Platform.windows?
24         allow(scope).to receive(:function_get_module_path).with(['science']).and_return('C:/path/to/module/')
25         allow(File).to receive(:exists?).with('C:/path/to/module/metadata.json').and_return(true)
26         allow(File).to receive(:read).with('C:/path/to/module/metadata.json').and_return('{"name": "spencer-science"}')
27       else
28         allow(scope).to receive(:function_get_module_path).with(['science']).and_return('/path/to/module/')
29         allow(File).to receive(:exists?).with('/path/to/module/metadata.json').and_return(true)
30         allow(File).to receive(:read).with('/path/to/module/metadata.json').and_return('{"name": "spencer-science"}')
31       end
32
33       result = subject.call(['science'])
34       expect(result['name']).to eq('spencer-science')
35     end
36
37     it "should fail by default if there is no metadata.json" do
38       if Puppet::Util::Platform.windows?
39         allow(scope).to receive(:function_get_module_path).with(['science']).and_return('C:/path/to/module/')
40         allow(File).to receive(:exists?).with('C:/path/to/module/metadata.json').and_return(false)
41       else
42         allow(scope).to receive(:function_get_module_path).with(['science']).and_return('/path/to/module/')
43         allow(File).to receive(:exists?).with('/path/to/module/metadata.json').and_return(false)
44       end
45       expect {subject.call(['science'])}.to raise_error(Puppet::ParseError)
46     end
47
48     it "should return nil if user allows empty metadata.json" do
49       if Puppet::Util::Platform.windows?
50         allow(scope).to receive(:function_get_module_path).with(['science']).and_return('C:/path/to/module/')
51         allow(File).to receive(:exists?).with('C:/path/to/module/metadata.json').and_return(false)
52       else
53         allow(scope).to receive(:function_get_module_path).with(['science']).and_return('/path/to/module/')
54         allow(File).to receive(:exists?).with('/path/to/module/metadata.json').and_return(false)
55       end
56       result = subject.call(['science', true])
57       expect(result).to eq({})
58     end
59     end
60   end
61 end