Skip to content

Commit 9c8c827

Browse files
committed
Merge pull request #159 from fiddyspence/feature/base64
Adding base64 function
2 parents 2cdbbaf + 3707c47 commit 9c8c827

File tree

4 files changed

+81
-1
lines changed

4 files changed

+81
-1
lines changed

README.markdown

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,15 @@ lists are converted to an empty array. Arrays are left untouched. Hashes are
8181
converted to arrays of alternating keys and values.
8282

8383

84+
- *Type*: rvalue
85+
86+
base64
87+
--------
88+
Converts a string to and from base64 encoding.
89+
Requires an action ['encode','decode'] and either a plain or base64 encoded
90+
string
91+
92+
8493
- *Type*: rvalue
8594

8695
bool2num

lib/puppet/parser/functions/base64.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
module Puppet::Parser::Functions
2+
3+
newfunction(:base64, :type => :rvalue, :doc => <<-'ENDHEREDOC') do |args|
4+
5+
Base64 encode or decode a string based on the command and the string submitted
6+
7+
Usage:
8+
9+
$encodestring = base64('encode','thestring')
10+
$decodestring = base64('decode','dGhlc3RyaW5n')
11+
12+
ENDHEREDOC
13+
14+
require 'base64'
15+
16+
raise Puppet::ParseError, ("base64(): Wrong number of arguments (#{args.length}; must be = 2)") unless args.length == 2
17+
18+
actions = ['encode','decode']
19+
20+
unless actions.include?(args[0])
21+
raise Puppet::ParseError, ("base64(): the first argument must be one of 'encode' or 'decode'")
22+
end
23+
24+
unless args[1].is_a?(String)
25+
raise Puppet::ParseError, ("base64(): the second argument must be a string to base64")
26+
end
27+
28+
case args[0]
29+
when 'encode'
30+
result = Base64.encode64(args[1])
31+
when 'decode'
32+
result = Base64.decode64(args[1])
33+
end
34+
35+
return result
36+
end
37+
end
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#! /usr/bin/env ruby -S rspec
2+
3+
require 'spec_helper'
4+
5+
describe "the base64 function" do
6+
let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
7+
8+
it "should exist" do
9+
Puppet::Parser::Functions.function("base64").should == "function_base64"
10+
end
11+
12+
it "should raise a ParseError if there are other than 2 arguments" do
13+
expect { scope.function_base64([]) }.to(raise_error(Puppet::ParseError))
14+
expect { scope.function_base64(["asdf"]) }.to(raise_error(Puppet::ParseError))
15+
expect { scope.function_base64(["asdf","moo","cow"]) }.to(raise_error(Puppet::ParseError))
16+
end
17+
18+
it "should raise a ParseError if argument 1 isn't 'encode' or 'decode'" do
19+
expect { scope.function_base64(["bees","astring"]) }.to(raise_error(Puppet::ParseError, /first argument must be one of/))
20+
end
21+
22+
it "should raise a ParseError if argument 2 isn't a string" do
23+
expect { scope.function_base64(["encode",["2"]]) }.to(raise_error(Puppet::ParseError, /second argument must be a string/))
24+
end
25+
26+
it "should encode a encoded string" do
27+
result = scope.function_base64(["encode",'thestring'])
28+
result.should =~ /\AdGhlc3RyaW5n\n\Z/
29+
end
30+
it "should decode a base64 encoded string" do
31+
result = scope.function_base64(["decode",'dGhlc3RyaW5n'])
32+
result.should == 'thestring'
33+
end
34+
end

spec/unit/puppet/parser/functions/validate_slength_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
end
3636

3737
it "should fail if you pass something other than a string or array" do
38-
expect { scope.function_validate_slength([Hash.new["moo" => "7"],6]) }.to(raise_error(Puppet::ParseError), /please pass a string, or an array of strings/)
38+
expect { scope.function_validate_slength([Hash.new["moo" => "7"],6]) }.to(raise_error(Puppet::ParseError, /please pass a string, or an array of strings/))
3939
end
4040

4141
it "should not fail if string is smaller or equal to size" do

0 commit comments

Comments
 (0)