private_spec.rb 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. require 'spec_helper'
  2. describe 'private' do
  3. it 'issues a warning' do
  4. scope.expects(: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
  5. begin
  6. subject.call []
  7. rescue # rubocop:disable Lint/HandleExceptions
  8. # ignore this
  9. end
  10. end
  11. context 'when called from inside module' do
  12. it 'does not fail' do
  13. scope.expects(:lookupvar).with('module_name').returns('foo')
  14. scope.expects(:lookupvar).with('caller_module_name').returns('foo')
  15. expect {
  16. subject.call []
  17. }.not_to raise_error
  18. end
  19. end
  20. context 'with an explicit failure message' do
  21. it 'prints the failure message on error' do
  22. scope.expects(:lookupvar).with('module_name').returns('foo')
  23. scope.expects(:lookupvar).with('caller_module_name').returns('bar')
  24. expect {
  25. subject.call ['failure message!']
  26. }.to raise_error Puppet::ParseError, %r{failure message!}
  27. end
  28. end
  29. context 'when called from private class' do
  30. it 'fails with a class error message' do
  31. scope.expects(:lookupvar).with('module_name').returns('foo')
  32. scope.expects(:lookupvar).with('caller_module_name').returns('bar')
  33. scope.source.expects(:name).returns('foo::baz')
  34. scope.source.expects(:type).returns('hostclass')
  35. expect { subject.call [] }.to raise_error Puppet::ParseError, %r{Class foo::baz is private}
  36. end
  37. end
  38. context 'when called from private definition' do
  39. it 'fails with a class error message' do
  40. scope.expects(:lookupvar).with('module_name').returns('foo')
  41. scope.expects(:lookupvar).with('caller_module_name').returns('bar')
  42. scope.source.expects(:name).returns('foo::baz')
  43. scope.source.expects(:type).returns('definition')
  44. expect { subject.call [] }.to raise_error Puppet::ParseError, %r{Definition foo::baz is private}
  45. end
  46. end
  47. end