Update puppetlabs/stdlib module
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / spec / functions / private_spec.rb
1 require 'spec_helper'
2
3 describe 'private' do
4   it 'issues a warning' do
5     expect(scope).to receive(:warning).with("private() DEPRECATED: This function will cease to function on Puppet 4; please use assert_private() before upgrading to puppet 4 for backwards-compatibility, or migrate to the new parser's typing system.") # rubocop:disable Metrics/LineLength : unable to cut line to required length
6     begin
7       subject.execute
8     rescue # rubocop:disable Lint/HandleExceptions
9       # ignore this
10     end
11   end
12
13   context 'when called from inside module' do
14     it 'does not fail' do
15       expect(scope).to receive(:lookupvar).with('module_name').and_return('foo')
16       expect(scope).to receive(:lookupvar).with('caller_module_name').and_return('foo')
17       expect {
18         subject.execute
19       }.not_to raise_error
20     end
21   end
22
23   context 'with an explicit failure message' do
24     it 'prints the failure message on error' do
25       expect(scope).to receive(:lookupvar).with('module_name').and_return('foo')
26       expect(scope).to receive(:lookupvar).with('caller_module_name').and_return('bar')
27       expect {
28         subject.execute('failure message!')
29       }.to raise_error(Puppet::ParseError, %r{failure message!})
30     end
31   end
32
33   context 'when called from private class' do
34     it 'fails with a class error message' do
35       expect(scope).to receive(:lookupvar).with('module_name').and_return('foo')
36       expect(scope).to receive(:lookupvar).with('caller_module_name').and_return('bar')
37       expect(scope.source).to receive(:name).and_return('foo::baz')
38       expect(scope.source).to receive(:type).and_return('hostclass')
39       expect { subject.execute }.to raise_error(Puppet::ParseError, %r{Class foo::baz is private})
40     end
41   end
42
43   context 'when called from private definition' do
44     it 'fails with a class error message' do
45       expect(scope).to receive(:lookupvar).with('module_name').and_return('foo')
46       expect(scope).to receive(:lookupvar).with('caller_module_name').and_return('bar')
47       expect(scope.source).to receive(:name).and_return('foo::baz')
48       expect(scope.source).to receive(:type).and_return('definition')
49       expect { subject.execute }.to raise_error(Puppet::ParseError, %r{Definition foo::baz is private})
50     end
51   end
52 end