Skip to content

Commit f049509

Browse files
author
Ashley Penney
committed
Merge pull request redhat-openstack#270 from raphink/dev/private
Add private() function
2 parents ffe21fc + 2062f97 commit f049509

3 files changed

Lines changed: 105 additions & 0 deletions

File tree

README.markdown

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,27 @@ Will return: ['pa','pb','pc']
725725

726726
- *Type*: rvalue
727727

728+
729+
private
730+
-------
731+
This function sets the current class or definition as private.
732+
Calling the class or definition from outside the current module will fail.
733+
734+
*Examples:*
735+
736+
private()
737+
738+
called in class `foo::bar` will output the following message if class is called
739+
from outside module `foo`:
740+
741+
Class foo::bar is private
742+
743+
You can specify the error message you want to use as a parameter:
744+
745+
private("You're not supposed to do that!")
746+
747+
- *Type*: statement
748+
728749
range
729750
-----
730751
When given range in the form of (start, stop) it will extrapolate a range as
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#
2+
# private.rb
3+
#
4+
5+
module Puppet::Parser::Functions
6+
newfunction(:private, :doc => <<-'EOS'
7+
Sets the current class or definition as private.
8+
Calling the class or definition from outside the current module will fail.
9+
EOS
10+
) do |args|
11+
12+
raise(Puppet::ParseError, "private(): Wrong number of arguments "+
13+
"given (#{args.size}}) for 0 or 1)") if args.size > 1
14+
15+
scope = self
16+
if scope.lookupvar('module_name') != scope.lookupvar('caller_module_name')
17+
message = nil
18+
if args[0] and args[0].is_a? String
19+
message = args[0]
20+
else
21+
manifest_name = scope.source.name
22+
manifest_type = scope.source.type
23+
message = (manifest_type.to_s == 'hostclass') ? 'Class' : 'Definition'
24+
message += " #{manifest_name} is private"
25+
end
26+
raise(Puppet::ParseError, message)
27+
end
28+
end
29+
end

spec/functions/private_spec.rb

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#! /usr/bin/env ruby -S rspec
2+
require 'spec_helper'
3+
4+
describe Puppet::Parser::Functions.function(:private) do
5+
let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
6+
7+
subject do
8+
function_name = Puppet::Parser::Functions.function(:private)
9+
scope.method(function_name)
10+
end
11+
12+
context "when called from inside module" do
13+
it "should not fail" do
14+
scope.expects(:lookupvar).with('module_name').returns('foo')
15+
scope.expects(:lookupvar).with('caller_module_name').returns('foo')
16+
expect {
17+
subject.call []
18+
}.not_to raise_error
19+
end
20+
end
21+
22+
context "with an explicit failure message" do
23+
it "prints the failure message on error" do
24+
scope.expects(:lookupvar).with('module_name').returns('foo')
25+
scope.expects(:lookupvar).with('caller_module_name').returns('bar')
26+
expect {
27+
subject.call ['failure message!']
28+
}.to raise_error Puppet::ParseError, /failure message!/
29+
end
30+
end
31+
32+
context "when called from private class" do
33+
it "should fail with a class error message" do
34+
scope.expects(:lookupvar).with('module_name').returns('foo')
35+
scope.expects(:lookupvar).with('caller_module_name').returns('bar')
36+
scope.source.expects(:name).returns('foo::baz')
37+
scope.source.expects(:type).returns('hostclass')
38+
expect {
39+
subject.call []
40+
}.to raise_error Puppet::ParseError, /Class foo::baz is private/
41+
end
42+
end
43+
44+
context "when called from private definition" do
45+
it "should fail with a class error message" do
46+
scope.expects(:lookupvar).with('module_name').returns('foo')
47+
scope.expects(:lookupvar).with('caller_module_name').returns('bar')
48+
scope.source.expects(:name).returns('foo::baz')
49+
scope.source.expects(:type).returns('definition')
50+
expect {
51+
subject.call []
52+
}.to raise_error Puppet::ParseError, /Definition foo::baz is private/
53+
end
54+
end
55+
end

0 commit comments

Comments
 (0)