summaryrefslogtreecommitdiff
path: root/spec/functions
diff options
context:
space:
mode:
Diffstat (limited to 'spec/functions')
-rwxr-xr-xspec/functions/abs_spec.rb39
-rwxr-xr-xspec/functions/any2array_spec.rb64
-rwxr-xr-xspec/functions/any2bool_spec.rb42
-rwxr-xr-xspec/functions/assert_private_spec.rb41
-rwxr-xr-xspec/functions/base64_spec.rb65
-rwxr-xr-xspec/functions/basename_spec.rb14
-rwxr-xr-xspec/functions/bool2num_spec.rb38
-rwxr-xr-xspec/functions/bool2str_spec.rb17
-rwxr-xr-xspec/functions/camelcase_spec.rb17
-rwxr-xr-xspec/functions/capitalize_spec.rb37
-rwxr-xr-xspec/functions/ceiling_spec.rb13
-rwxr-xr-xspec/functions/chomp_spec.rb40
-rwxr-xr-xspec/functions/chop_spec.rb40
-rw-r--r--spec/functions/clamp_spec.rb16
-rwxr-xr-xspec/functions/concat_spec.rb63
-rw-r--r--spec/functions/convert_base_spec.rb24
-rwxr-xr-xspec/functions/count_spec.rb43
-rwxr-xr-xspec/functions/deep_merge_spec.rb144
-rwxr-xr-xspec/functions/defined_with_params_spec.rb36
-rwxr-xr-xspec/functions/delete_at_spec.rb37
-rwxr-xr-xspec/functions/delete_regex_spec.rb54
-rwxr-xr-xspec/functions/delete_spec.rb105
-rwxr-xr-xspec/functions/delete_undef_values_spec.rb85
-rwxr-xr-xspec/functions/delete_values_spec.rb57
-rw-r--r--spec/functions/deprecation_spec.rb60
-rwxr-xr-xspec/functions/difference_spec.rb34
-rw-r--r--spec/functions/dig44_spec.rb105
-rw-r--r--spec/functions/dig_spec.rb13
-rwxr-xr-xspec/functions/dirname_spec.rb31
-rw-r--r--spec/functions/dos2unix_spec.rb40
-rwxr-xr-xspec/functions/downcase_spec.rb42
-rwxr-xr-xspec/functions/empty_spec.rb42
-rwxr-xr-xspec/functions/ensure_packages_spec.rb95
-rwxr-xr-xspec/functions/ensure_resource_spec.rb148
-rwxr-xr-xspec/functions/flatten_spec.rb35
-rwxr-xr-xspec/functions/floor_spec.rb45
-rw-r--r--spec/functions/fqdn_rand_string_spec.rb65
-rwxr-xr-xspec/functions/fqdn_rotate_spec.rb80
-rw-r--r--spec/functions/fqdn_uuid_spec.rb14
-rwxr-xr-xspec/functions/get_module_path_spec.rb56
-rwxr-xr-xspec/functions/getparam_spec.rb87
-rwxr-xr-xspec/functions/getvar_spec.rb46
-rwxr-xr-xspec/functions/grep_spec.rb32
-rwxr-xr-xspec/functions/has_interface_with_spec.rb80
-rwxr-xr-xspec/functions/has_ip_address_spec.rb45
-rwxr-xr-xspec/functions/has_ip_network_spec.rb42
-rwxr-xr-xspec/functions/has_key_spec.rb51
-rwxr-xr-xspec/functions/hash_spec.rb27
-rwxr-xr-xspec/functions/intersection_spec.rb34
-rw-r--r--spec/functions/is_a_spec.rb25
-rwxr-xr-xspec/functions/is_array_spec.rb57
-rwxr-xr-xspec/functions/is_bool_spec.rb71
-rwxr-xr-xspec/functions/is_domain_name_spec.rb99
-rwxr-xr-xspec/functions/is_email_address_spec.rb14
-rwxr-xr-xspec/functions/is_float_spec.rb53
-rwxr-xr-xspec/functions/is_function_available.rb34
-rwxr-xr-xspec/functions/is_hash_spec.rb34
-rwxr-xr-xspec/functions/is_integer_spec.rb106
-rwxr-xr-xspec/functions/is_ip_address_spec.rb71
-rw-r--r--spec/functions/is_ipv4_address_spec.rb32
-rw-r--r--spec/functions/is_ipv6_address_spec.rb30
-rwxr-xr-xspec/functions/is_mac_address_spec.rb47
-rwxr-xr-xspec/functions/is_numeric_spec.rb157
-rwxr-xr-xspec/functions/is_string_spec.rb63
-rwxr-xr-xspec/functions/join_keys_to_values_spec.rb55
-rwxr-xr-xspec/functions/join_spec.rb30
-rwxr-xr-xspec/functions/keys_spec.rb32
-rwxr-xr-xspec/functions/load_module_metadata_spec.rb38
-rw-r--r--spec/functions/loadjson_spec.rb49
-rwxr-xr-xspec/functions/loadyaml_spec.rb41
-rwxr-xr-xspec/functions/lstrip_spec.rb56
-rwxr-xr-xspec/functions/max_spec.rb38
-rwxr-xr-xspec/functions/member_spec.rb49
-rwxr-xr-xspec/functions/merge_spec.rb69
-rwxr-xr-xspec/functions/min_spec.rb38
-rwxr-xr-xspec/functions/num2bool_spec.rb83
-rwxr-xr-xspec/functions/parsejson_spec.rb68
-rwxr-xr-xspec/functions/parseyaml_spec.rb87
-rwxr-xr-xspec/functions/pick_default_spec.rb74
-rwxr-xr-xspec/functions/pick_spec.rb40
-rwxr-xr-xspec/functions/prefix_spec.rb50
-rw-r--r--[-rwxr-xr-x]spec/functions/private_spec.rb15
-rw-r--r--spec/functions/pw_hash_spec.rb69
-rwxr-xr-xspec/functions/range_spec.rb164
-rw-r--r--spec/functions/regexpescape_spec.rb36
-rwxr-xr-xspec/functions/reject_spec.rb31
-rwxr-xr-xspec/functions/reverse_spec.rb45
-rwxr-xr-xspec/functions/rstrip_spec.rb61
-rw-r--r--spec/functions/seeded_rand_spec.rb53
-rw-r--r--spec/functions/shell_escape_spec.rb22
-rw-r--r--spec/functions/shell_join_spec.rb23
-rw-r--r--spec/functions/shell_split_spec.rb24
-rwxr-xr-xspec/functions/shuffle_spec.rb48
-rwxr-xr-xspec/functions/size_spec.rb45
-rwxr-xr-xspec/functions/sort_spec.rb32
-rwxr-xr-xspec/functions/squeeze_spec.rb50
-rwxr-xr-xspec/functions/str2bool_spec.rb40
-rwxr-xr-xspec/functions/str2saltedsha512_spec.rb50
-rwxr-xr-xspec/functions/strftime_spec.rb5
-rwxr-xr-xspec/functions/strip_spec.rb55
-rwxr-xr-xspec/functions/suffix_spec.rb59
-rwxr-xr-xspec/functions/swapcase_spec.rb54
-rwxr-xr-xspec/functions/time_spec.rb40
-rwxr-xr-xspec/functions/to_bytes_spec.rb147
-rw-r--r--spec/functions/try_get_value_spec.rb100
-rw-r--r--spec/functions/type3x_spec.rb4
-rw-r--r--spec/functions/type_of_spec.rb25
-rwxr-xr-xspec/functions/type_spec.rb4
-rwxr-xr-xspec/functions/union_spec.rb33
-rwxr-xr-xspec/functions/unique_spec.rb44
-rw-r--r--spec/functions/unix2dos_spec.rb40
-rwxr-xr-xspec/functions/upcase_spec.rb39
-rwxr-xr-xspec/functions/uriescape_spec.rb49
-rwxr-xr-xspec/functions/validate_absolute_path_spec.rb127
-rwxr-xr-xspec/functions/validate_array_spec.rb55
-rwxr-xr-xspec/functions/validate_augeas_spec.rb70
-rwxr-xr-xspec/functions/validate_bool_spec.rb71
-rwxr-xr-xspec/functions/validate_cmd_spec.rb104
-rw-r--r--spec/functions/validate_email_address_spec.rb23
-rwxr-xr-xspec/functions/validate_hash_spec.rb57
-rwxr-xr-xspec/functions/validate_integer_spec.rb101
-rw-r--r--spec/functions/validate_ip_address_spec.rb65
-rwxr-xr-xspec/functions/validate_ipv4_address_spec.rb81
-rwxr-xr-xspec/functions/validate_ipv6_address_spec.rb97
-rw-r--r--spec/functions/validate_legacy_spec.rb68
-rwxr-xr-xspec/functions/validate_numeric_spec.rb100
-rwxr-xr-xspec/functions/validate_re_spec.rb109
-rwxr-xr-xspec/functions/validate_slength_spec.rb103
-rwxr-xr-xspec/functions/validate_string_spec.rb75
-rwxr-xr-xspec/functions/validate_x509_rsa_key_pair_spec.rb180
-rwxr-xr-xspec/functions/values_at_spec.rb71
-rwxr-xr-xspec/functions/values_spec.rb42
-rwxr-xr-xspec/functions/zip_spec.rb40
133 files changed, 4135 insertions, 3280 deletions
diff --git a/spec/functions/abs_spec.rb b/spec/functions/abs_spec.rb
index 3c25ce2..7d2257b 100755
--- a/spec/functions/abs_spec.rb
+++ b/spec/functions/abs_spec.rb
@@ -1,25 +1,30 @@
-#! /usr/bin/env ruby -S rspec
-
require 'spec_helper'
-describe "the abs function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- it "should exist" do
- expect(Puppet::Parser::Functions.function("abs")).to eq("function_abs")
- end
+describe 'abs' do
+ it { is_expected.not_to eq(nil) }
- it "should raise a ParseError if there is less than 1 arguments" do
- expect { scope.function_abs([]) }.to( raise_error(Puppet::ParseError))
+ describe 'signature validation in puppet3', :unless => RSpec.configuration.puppet_future do
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it {
+ pending("Current implementation ignores parameters after the first.")
+ is_expected.to run.with_params(1, 2).and_raise_error(Puppet::ParseError, /wrong number of arguments/i)
+ }
end
- it "should convert a negative number into a positive" do
- result = scope.function_abs(["-34"])
- expect(result).to(eq(34))
+ describe 'signature validation in puppet4', :if => RSpec.configuration.puppet_future do
+ it { pending "the puppet 4 implementation"; is_expected.to run.with_params().and_raise_error(ArgumentError) }
+ it { pending "the puppet 4 implementation"; is_expected.to run.with_params(1, 2).and_raise_error(ArgumentError) }
+ it { pending "the puppet 4 implementation"; is_expected.to run.with_params([]).and_raise_error(ArgumentError) }
+ it { pending "the puppet 4 implementation"; is_expected.to run.with_params({}).and_raise_error(ArgumentError) }
+ it { pending "the puppet 4 implementation"; is_expected.to run.with_params(true).and_raise_error(ArgumentError) }
end
- it "should do nothing with a positive number" do
- result = scope.function_abs(["5678"])
- expect(result).to(eq(5678))
- end
+ it { is_expected.to run.with_params(-34).and_return(34) }
+ it { is_expected.to run.with_params("-34").and_return(34) }
+ it { is_expected.to run.with_params(34).and_return(34) }
+ it { is_expected.to run.with_params("34").and_return(34) }
+ it { is_expected.to run.with_params(-34.5).and_return(34.5) }
+ it { is_expected.to run.with_params("-34.5").and_return(34.5) }
+ it { is_expected.to run.with_params(34.5).and_return(34.5) }
+ it { is_expected.to run.with_params("34.5").and_return(34.5) }
end
diff --git a/spec/functions/any2array_spec.rb b/spec/functions/any2array_spec.rb
index 87cd04b..70121f1 100755
--- a/spec/functions/any2array_spec.rb
+++ b/spec/functions/any2array_spec.rb
@@ -1,55 +1,15 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the any2array function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- it "should exist" do
- expect(Puppet::Parser::Functions.function("any2array")).to eq("function_any2array")
- end
-
- it "should return an empty array if there is less than 1 argument" do
- result = scope.function_any2array([])
- expect(result).to(eq([]))
- end
-
- it "should convert boolean true to [ true ] " do
- result = scope.function_any2array([true])
- expect(result).to(eq([true]))
- end
-
- it "should convert one object to [object]" do
- result = scope.function_any2array(['one'])
- expect(result).to(eq(['one']))
- end
-
- it "should convert multiple objects to [objects]" do
- result = scope.function_any2array(['one', 'two'])
- expect(result).to(eq(['one', 'two']))
- end
-
- it "should return empty array it was called with" do
- result = scope.function_any2array([[]])
- expect(result).to(eq([]))
- end
-
- it "should return one-member array it was called with" do
- result = scope.function_any2array([['string']])
- expect(result).to(eq(['string']))
- end
-
- it "should return multi-member array it was called with" do
- result = scope.function_any2array([['one', 'two']])
- expect(result).to(eq(['one', 'two']))
- end
-
- it "should return members of a hash it was called with" do
- result = scope.function_any2array([{ 'key' => 'value' }])
- expect(result).to(eq(['key', 'value']))
- end
-
- it "should return an empty array if it was called with an empty hash" do
- result = scope.function_any2array([{ }])
- expect(result).to(eq([]))
- end
+describe "any2array" do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_return([]) }
+ it { is_expected.to run.with_params(true).and_return([true]) }
+ it { is_expected.to run.with_params('one').and_return(['one']) }
+ it { is_expected.to run.with_params('one', 'two').and_return(['one', 'two']) }
+ it { is_expected.to run.with_params([]).and_return([]) }
+ it { is_expected.to run.with_params(['one']).and_return(['one']) }
+ it { is_expected.to run.with_params(['one', 'two']).and_return(['one', 'two']) }
+ it { is_expected.to run.with_params({}).and_return([]) }
+ it { is_expected.to run.with_params({ 'key' => 'value' }).and_return(['key', 'value']) }
+ it { is_expected.to run.with_params({ 'key' => 'value' }).and_return(['key', 'value']) }
end
diff --git a/spec/functions/any2bool_spec.rb b/spec/functions/any2bool_spec.rb
new file mode 100755
index 0000000..9d351ce
--- /dev/null
+++ b/spec/functions/any2bool_spec.rb
@@ -0,0 +1,42 @@
+require 'spec_helper'
+
+describe 'any2bool' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+
+ it { is_expected.to run.with_params(true).and_return(true) }
+ it { is_expected.to run.with_params(false).and_return(false) }
+
+ it { is_expected.to run.with_params('1.5').and_return(true) }
+
+ describe 'when testing stringy values that mean "true"' do
+ [ 'TRUE','1', 't', 'y', 'true', 'yes'].each do |value|
+ it { is_expected.to run.with_params(value).and_return(true) }
+ end
+ end
+
+ describe 'when testing stringy values that mean "false"' do
+ [ 'FALSE','', '0', 'f', 'n', 'false', 'no', 'undef', 'undefined', nil, :undef ].each do |value|
+ it { is_expected.to run.with_params(value).and_return(false) }
+ end
+ end
+
+ describe 'when testing numeric values that mean "true"' do
+ [ 1,'1',1.5, '1.5'].each do |value|
+ it { is_expected.to run.with_params(value).and_return(true) }
+ end
+ end
+
+ describe 'when testing numeric that mean "false"' do
+ [ -1, '-1', -1.5, '-1.5', '0', 0 ].each do |value|
+ it { is_expected.to run.with_params(value).and_return(false) }
+ end
+ end
+
+ describe 'everything else returns true' do
+ [ [], {}, ['1'], [1], {:one => 1} ].each do |value|
+ it { is_expected.to run.with_params(value).and_return(true) }
+ end
+ end
+
+end
diff --git a/spec/functions/assert_private_spec.rb b/spec/functions/assert_private_spec.rb
new file mode 100755
index 0000000..355e0dd
--- /dev/null
+++ b/spec/functions/assert_private_spec.rb
@@ -0,0 +1,41 @@
+require 'spec_helper'
+
+describe 'assert_private' do
+ context 'when called from inside module' do
+ it "should not fail" do
+ scope.expects(:lookupvar).with('module_name').returns('foo')
+ scope.expects(:lookupvar).with('caller_module_name').returns('foo')
+
+ is_expected.to run.with_params()
+ end
+ end
+
+ context "when called from private class" do
+ before :each do
+ scope.expects(:lookupvar).with('module_name').returns('foo')
+ scope.expects(:lookupvar).with('caller_module_name').returns('bar')
+ end
+
+ it "should fail with a class error message" do
+ scope.source.expects(:name).returns('foo::baz')
+ scope.source.expects(:type).returns('hostclass')
+
+ is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /Class foo::baz is private/)
+ end
+
+ context "with an explicit failure message" do
+ it { is_expected.to run.with_params('failure message!').and_raise_error(Puppet::ParseError, /failure message!/) }
+ end
+ end
+
+ context "when called from private definition" do
+ it "should fail with a class error message" do
+ scope.expects(:lookupvar).with('module_name').returns('foo')
+ scope.expects(:lookupvar).with('caller_module_name').returns('bar')
+ scope.source.expects(:name).returns('foo::baz')
+ scope.source.expects(:type).returns('definition')
+
+ is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /Definition foo::baz is private/)
+ end
+ end
+end
diff --git a/spec/functions/base64_spec.rb b/spec/functions/base64_spec.rb
index e93fafc..842a37a 100755
--- a/spec/functions/base64_spec.rb
+++ b/spec/functions/base64_spec.rb
@@ -1,34 +1,37 @@
-#! /usr/bin/env ruby -S rspec
-
require 'spec_helper'
-describe "the base64 function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- it "should exist" do
- expect(Puppet::Parser::Functions.function("base64")).to eq("function_base64")
- end
-
- it "should raise a ParseError if there are other than 2 arguments" do
- expect { scope.function_base64([]) }.to(raise_error(Puppet::ParseError))
- expect { scope.function_base64(["asdf"]) }.to(raise_error(Puppet::ParseError))
- expect { scope.function_base64(["asdf","moo","cow"]) }.to(raise_error(Puppet::ParseError))
- end
-
- it "should raise a ParseError if argument 1 isn't 'encode' or 'decode'" do
- expect { scope.function_base64(["bees","astring"]) }.to(raise_error(Puppet::ParseError, /first argument must be one of/))
- end
-
- it "should raise a ParseError if argument 2 isn't a string" do
- expect { scope.function_base64(["encode",["2"]]) }.to(raise_error(Puppet::ParseError, /second argument must be a string/))
- end
-
- it "should encode a encoded string" do
- result = scope.function_base64(["encode",'thestring'])
- expect(result).to match(/\AdGhlc3RyaW5n\n\Z/)
- end
- it "should decode a base64 encoded string" do
- result = scope.function_base64(["decode",'dGhlc3RyaW5n'])
- expect(result).to eq('thestring')
- end
+describe 'base64' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params("one").and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params("one", "two").and_raise_error(Puppet::ParseError, /first argument must be one of/) }
+ it { is_expected.to run.with_params("encode", ["two"]).and_raise_error(Puppet::ParseError, /second argument must be a string/) }
+ it { is_expected.to run.with_params("encode", 2).and_raise_error(Puppet::ParseError, /second argument must be a string/) }
+ it { is_expected.to run.with_params("encode", "thestring", "three").and_raise_error(Puppet::ParseError, /third argument must be one of/) }
+ it { is_expected.to run.with_params("decode", "dGhlc3RyaW5n\n", "strict").and_raise_error(ArgumentError) }
+
+ it { is_expected.to run.with_params("encode", "thestring").and_return("dGhlc3RyaW5n\n") }
+ it { is_expected.to run.with_params("decode", "dGhlc3RyaW5n").and_return("thestring") }
+ it { is_expected.to run.with_params("decode", "dGhlc3RyaW5n\n").and_return("thestring") }
+
+ it { is_expected.to run.with_params("encode", "thestring", "default").and_return("dGhlc3RyaW5n\n") }
+ it { is_expected.to run.with_params("decode", "dGhlc3RyaW5n", "default").and_return("thestring") }
+ it { is_expected.to run.with_params("decode", "dGhlc3RyaW5n\n", "default").and_return("thestring") }
+
+ it { is_expected.to run.with_params("encode", "thestring", "strict").and_return("dGhlc3RyaW5n") }
+ it { is_expected.to run.with_params("decode", "dGhlc3RyaW5n", "strict").and_return("thestring") }
+
+ it { is_expected.to run.with_params("encode", "a very long string that will cause the base64 encoder to produce output with multiple lines").and_return("YSB2ZXJ5IGxvbmcgc3RyaW5nIHRoYXQgd2lsbCBjYXVzZSB0aGUgYmFzZTY0\nIGVuY29kZXIgdG8gcHJvZHVjZSBvdXRwdXQgd2l0aCBtdWx0aXBsZSBsaW5l\ncw==\n") }
+ it { is_expected.to run.with_params("decode", "YSB2ZXJ5IGxvbmcgc3RyaW5nIHRoYXQgd2lsbCBjYXVzZSB0aGUgYmFzZTY0\nIGVuY29kZXIgdG8gcHJvZHVjZSBvdXRwdXQgd2l0aCBtdWx0aXBsZSBsaW5l\ncw==\n").and_return("a very long string that will cause the base64 encoder to produce output with multiple lines") }
+ it { is_expected.to run.with_params("decode", "YSB2ZXJ5IGxvbmcgc3RyaW5nIHRoYXQgd2lsbCBjYXVzZSB0aGUgYmFzZTY0IGVuY29kZXIgdG8gcHJvZHVjZSBvdXRwdXQgd2l0aCBtdWx0aXBsZSBsaW5lcw==").and_return("a very long string that will cause the base64 encoder to produce output with multiple lines") }
+
+ it { is_expected.to run.with_params("encode", "a very long string that will cause the base64 encoder to produce output with multiple lines", "strict").and_return("YSB2ZXJ5IGxvbmcgc3RyaW5nIHRoYXQgd2lsbCBjYXVzZSB0aGUgYmFzZTY0IGVuY29kZXIgdG8gcHJvZHVjZSBvdXRwdXQgd2l0aCBtdWx0aXBsZSBsaW5lcw==") }
+ it { is_expected.to run.with_params("decode", "YSB2ZXJ5IGxvbmcgc3RyaW5nIHRoYXQgd2lsbCBjYXVzZSB0aGUgYmFzZTY0IGVuY29kZXIgdG8gcHJvZHVjZSBvdXRwdXQgd2l0aCBtdWx0aXBsZSBsaW5lcw==").and_return("a very long string that will cause the base64 encoder to produce output with multiple lines") }
+ it { is_expected.to run.with_params("decode", "YSB2ZXJ5IGxvbmcgc3RyaW5nIHRoYXQgd2lsbCBjYXVzZSB0aGUgYmFzZTY0IGVuY29kZXIgdG8gcHJvZHVjZSBvdXRwdXQgd2l0aCBtdWx0aXBsZSBsaW5lcw==", "strict").and_return("a very long string that will cause the base64 encoder to produce output with multiple lines") }
+
+ it { is_expected.to run.with_params("encode", "https://www.google.com.tw/?gws_rd=ssl#q=hello+world", "urlsafe").and_return("aHR0cHM6Ly93d3cuZ29vZ2xlLmNvbS50dy8_Z3dzX3JkPXNzbCNxPWhlbGxvK3dvcmxk") }
+ it { is_expected.to run.with_params("decode", "aHR0cHM6Ly93d3cuZ29vZ2xlLmNvbS50dy8_Z3dzX3JkPXNzbCNxPWhlbGxvK3dvcmxk", "urlsafe").and_return("https://www.google.com.tw/?gws_rd=ssl#q=hello+world") }
+
+ it { is_expected.to run.with_params("encode", "https://github.com/puppetlabs/puppetlabs-stdlib/pulls?utf8=%E2%9C%93&q=is%3Apr+is%3Aopen+Add", "urlsafe").and_return("aHR0cHM6Ly9naXRodWIuY29tL3B1cHBldGxhYnMvcHVwcGV0bGFicy1zdGRsaWIvcHVsbHM_dXRmOD0lRTIlOUMlOTMmcT1pcyUzQXByK2lzJTNBb3BlbitBZGQ=") }
+ it { is_expected.to run.with_params("decode", "aHR0cHM6Ly9naXRodWIuY29tL3B1cHBldGxhYnMvcHVwcGV0bGFicy1zdGRsaWIvcHVsbHM_dXRmOD0lRTIlOUMlOTMmcT1pcyUzQXByK2lzJTNBb3BlbitBZGQ=", "urlsafe").and_return("https://github.com/puppetlabs/puppetlabs-stdlib/pulls?utf8=%E2%9C%93&q=is%3Apr+is%3Aopen+Add") }
end
diff --git a/spec/functions/basename_spec.rb b/spec/functions/basename_spec.rb
new file mode 100755
index 0000000..c84e192
--- /dev/null
+++ b/spec/functions/basename_spec.rb
@@ -0,0 +1,14 @@
+require 'spec_helper'
+
+describe 'basename' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params('one', 'two', 'three').and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params('/path/to/a/file.ext', []).and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params('/path/to/a/file.ext').and_return('file.ext') }
+ it { is_expected.to run.with_params('relative_path/to/a/file.ext').and_return('file.ext') }
+ it { is_expected.to run.with_params('/path/to/a/file.ext', '.ext').and_return('file') }
+ it { is_expected.to run.with_params('relative_path/to/a/file.ext', '.ext').and_return('file') }
+ it { is_expected.to run.with_params('scheme:///path/to/a/file.ext').and_return('file.ext') }
+end
diff --git a/spec/functions/bool2num_spec.rb b/spec/functions/bool2num_spec.rb
index 3904d7e..e506859 100755
--- a/spec/functions/bool2num_spec.rb
+++ b/spec/functions/bool2num_spec.rb
@@ -1,38 +1,14 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the bool2num function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+describe 'bool2num' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
- it "should exist" do
- expect(Puppet::Parser::Functions.function("bool2num")).to eq("function_bool2num")
+ [ true, 'true', AlsoString.new('true') ].each do |truthy|
+ it { is_expected.to run.with_params(truthy).and_return(1) }
end
- it "should raise a ParseError if there is less than 1 arguments" do
- expect { scope.function_bool2num([]) }.to( raise_error(Puppet::ParseError))
- end
-
- it "should convert true to 1" do
- result = scope.function_bool2num([true])
- expect(result).to(eq(1))
- end
-
- it "should convert 'true' to 1" do
- result = scope.function_bool2num(['true'])
- result.should(eq(1))
- end
-
- it "should convert 'false' to 0" do
- result = scope.function_bool2num(['false'])
- expect(result).to(eq(0))
- end
-
- it "should accept objects which extend String" do
- class AlsoString < String
- end
-
- value = AlsoString.new('true')
- result = scope.function_bool2num([value])
- result.should(eq(1))
+ [ false, 'false', AlsoString.new('false') ].each do |falsey|
+ it { is_expected.to run.with_params(falsey).and_return(0) }
end
end
diff --git a/spec/functions/bool2str_spec.rb b/spec/functions/bool2str_spec.rb
new file mode 100755
index 0000000..23a754b
--- /dev/null
+++ b/spec/functions/bool2str_spec.rb
@@ -0,0 +1,17 @@
+require 'spec_helper'
+
+describe 'bool2str' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
+ [ 'true', 'false', nil, :undef, ''].each do |invalid|
+ it { is_expected.to run.with_params(invalid).and_raise_error(Puppet::ParseError) }
+ end
+ it { is_expected.to run.with_params(true, 'yes', 'no', 'maybe').and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params(true, 'maybe').and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params(true, 0, 1).and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params(true).and_return("true") }
+ it { is_expected.to run.with_params(false).and_return("false") }
+ it { is_expected.to run.with_params(true, 'yes', 'no').and_return("yes") }
+ it { is_expected.to run.with_params(false, 'yes', 'no').and_return("no") }
+
+end
diff --git a/spec/functions/camelcase_spec.rb b/spec/functions/camelcase_spec.rb
new file mode 100755
index 0000000..c78aa62
--- /dev/null
+++ b/spec/functions/camelcase_spec.rb
@@ -0,0 +1,17 @@
+require 'spec_helper'
+
+describe 'camelcase' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params(100).and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params("abc").and_return("Abc") }
+ it { is_expected.to run.with_params("aa_bb_cc").and_return("AaBbCc") }
+ it { is_expected.to run.with_params("_aa__bb__cc_").and_return("AaBbCc") }
+ it { is_expected.to run.with_params("100").and_return("100") }
+ it { is_expected.to run.with_params("1_00").and_return("100") }
+ it { is_expected.to run.with_params("_").and_return("") }
+ it { is_expected.to run.with_params("").and_return("") }
+ it { is_expected.to run.with_params([]).and_return([]) }
+ it { is_expected.to run.with_params(["abc", "aa_bb_cc"]).and_return(["Abc", "AaBbCc"]) }
+ it { is_expected.to run.with_params(["abc", 1, "aa_bb_cc"]).and_return(["Abc", 1, "AaBbCc"]) }
+end
diff --git a/spec/functions/capitalize_spec.rb b/spec/functions/capitalize_spec.rb
index fd0e92b..7ce2e16 100755
--- a/spec/functions/capitalize_spec.rb
+++ b/spec/functions/capitalize_spec.rb
@@ -1,28 +1,15 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the capitalize function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- it "should exist" do
- expect(Puppet::Parser::Functions.function("capitalize")).to eq("function_capitalize")
- end
-
- it "should raise a ParseError if there is less than 1 arguments" do
- expect { scope.function_capitalize([]) }.to( raise_error(Puppet::ParseError))
- end
-
- it "should capitalize the beginning of a string" do
- result = scope.function_capitalize(["abc"])
- expect(result).to(eq("Abc"))
- end
-
- it "should accept objects which extend String" do
- class AlsoString < String
- end
-
- value = AlsoString.new('abc')
- result = scope.function_capitalize([value])
- result.should(eq('Abc'))
- end
+describe 'capitalize' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params(100).and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params("one").and_return("One") }
+ it { is_expected.to run.with_params("one two").and_return("One two") }
+ it { is_expected.to run.with_params("ONE TWO").and_return("One two") }
+
+ it { is_expected.to run.with_params(AlsoString.new("one")).and_return("One") }
+ it { is_expected.to run.with_params([]).and_return([]) }
+ it { is_expected.to run.with_params(["one", "two"]).and_return(["One", "Two"]) }
+ it { is_expected.to run.with_params(["one", 1, "two"]).and_return(["One", 1, "Two"]) }
end
diff --git a/spec/functions/ceiling_spec.rb b/spec/functions/ceiling_spec.rb
new file mode 100755
index 0000000..567426f
--- /dev/null
+++ b/spec/functions/ceiling_spec.rb
@@ -0,0 +1,13 @@
+require 'spec_helper'
+
+describe 'ceiling' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params("foo").and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params(34).and_return(34) }
+ it { is_expected.to run.with_params(-34).and_return(-34) }
+ it { is_expected.to run.with_params(33.1).and_return(34) }
+ it { is_expected.to run.with_params(-33.1).and_return(-33) }
+end
+
diff --git a/spec/functions/chomp_spec.rb b/spec/functions/chomp_spec.rb
index b1e1e60..6878742 100755
--- a/spec/functions/chomp_spec.rb
+++ b/spec/functions/chomp_spec.rb
@@ -1,28 +1,20 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the chomp function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+describe 'chomp' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError) }
+ it {
+ pending("Current implementation ignores parameters after the first.")
+ is_expected.to run.with_params("a", "b").and_raise_error(Puppet::ParseError)
+ }
+ it { is_expected.to run.with_params("one").and_return("one") }
+ it { is_expected.to run.with_params("one\n").and_return("one") }
+ it { is_expected.to run.with_params("one\n\n").and_return("one\n") }
+ it { is_expected.to run.with_params(["one\n", "two", "three\n"]).and_return(["one", "two", "three"]) }
- it "should exist" do
- expect(Puppet::Parser::Functions.function("chomp")).to eq("function_chomp")
- end
-
- it "should raise a ParseError if there is less than 1 arguments" do
- expect { scope.function_chomp([]) }.to( raise_error(Puppet::ParseError))
- end
-
- it "should chomp the end of a string" do
- result = scope.function_chomp(["abc\n"])
- expect(result).to(eq("abc"))
- end
-
- it "should accept objects which extend String" do
- class AlsoString < String
- end
-
- value = AlsoString.new("abc\n")
- result = scope.function_chomp([value])
- result.should(eq("abc"))
- end
+ it { is_expected.to run.with_params(AlsoString.new("one")).and_return("one") }
+ it { is_expected.to run.with_params(AlsoString.new("one\n")).and_return("one") }
+ it { is_expected.to run.with_params(AlsoString.new("one\n\n")).and_return("one\n") }
+ it { is_expected.to run.with_params([AlsoString.new("one\n"), AlsoString.new("two"), "three\n"]).and_return(["one", "two", "three"]) }
end
diff --git a/spec/functions/chop_spec.rb b/spec/functions/chop_spec.rb
index c8a1951..db7d18b 100755
--- a/spec/functions/chop_spec.rb
+++ b/spec/functions/chop_spec.rb
@@ -1,28 +1,20 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the chop function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+describe 'chop' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError) }
+ it {
+ pending("Current implementation ignores parameters after the first.")
+ is_expected.to run.with_params("a", "b").and_raise_error(Puppet::ParseError)
+ }
+ it { is_expected.to run.with_params("one").and_return("on") }
+ it { is_expected.to run.with_params("one\n").and_return("one") }
+ it { is_expected.to run.with_params("one\n\n").and_return("one\n") }
+ it { is_expected.to run.with_params(["one\n", "two", "three\n"]).and_return(["one", "tw", "three"]) }
- it "should exist" do
- expect(Puppet::Parser::Functions.function("chop")).to eq("function_chop")
- end
-
- it "should raise a ParseError if there is less than 1 arguments" do
- expect { scope.function_chop([]) }.to( raise_error(Puppet::ParseError))
- end
-
- it "should chop the end of a string" do
- result = scope.function_chop(["asdf\n"])
- expect(result).to(eq("asdf"))
- end
-
- it "should accept objects which extend String" do
- class AlsoString < String
- end
-
- value = AlsoString.new("abc\n")
- result = scope.function_chop([value])
- result.should(eq('abc'))
- end
+ it { is_expected.to run.with_params(AlsoString.new("one")).and_return("on") }
+ it { is_expected.to run.with_params(AlsoString.new("one\n")).and_return("one") }
+ it { is_expected.to run.with_params(AlsoString.new("one\n\n")).and_return("one\n") }
+ it { is_expected.to run.with_params([AlsoString.new("one\n"), AlsoString.new("two"), "three\n"]).and_return(["one", "tw", "three"]) }
end
diff --git a/spec/functions/clamp_spec.rb b/spec/functions/clamp_spec.rb
new file mode 100644
index 0000000..3e2fe7a
--- /dev/null
+++ b/spec/functions/clamp_spec.rb
@@ -0,0 +1,16 @@
+require 'spec_helper'
+
+describe 'clamp' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(ArgumentError) }
+ it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params(12, 88, 71, 190).and_raise_error(Puppet::ParseError, /Wrong number of arguments, need three to clamp/) }
+ it { is_expected.to run.with_params('12string', 88, 15).and_raise_error(Puppet::ParseError, /Required explicit numeric/) }
+ it { is_expected.to run.with_params(1, 2, {'a' => 55}).and_raise_error(Puppet::ParseError, /The Hash type is not allowed/) }
+ it { is_expected.to run.with_params('24', [575, 187]).and_return(187) }
+ it { is_expected.to run.with_params([4, 3, '99']).and_return(4) }
+ it { is_expected.to run.with_params(16, 750, 88).and_return(88) }
+ it { is_expected.to run.with_params([3, 873], 73).and_return(73) }
+ it { is_expected.to run.with_params([4], 8, 75).and_return(8) }
+ it { is_expected.to run.with_params([6], [31], 9911).and_return(31) }
+end
diff --git a/spec/functions/concat_spec.rb b/spec/functions/concat_spec.rb
index 49fa6bb..eb76233 100755
--- a/spec/functions/concat_spec.rb
+++ b/spec/functions/concat_spec.rb
@@ -1,50 +1,25 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the concat function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- it "should raise a ParseError if the client does not provide at least two arguments" do
- expect { scope.function_concat([]) }.to(raise_error(Puppet::ParseError))
- expect { scope.function_concat([[1]]) }.to(raise_error(Puppet::ParseError))
- end
-
- it "should raise a ParseError if the first parameter is not an array" do
- expect { scope.function_concat([1, []])}.to(raise_error(Puppet::ParseError))
- end
-
- it "should not raise a ParseError if the client provides more than two arguments" do
- expect { scope.function_concat([[1],[2],[3]]) }.not_to raise_error
- end
-
- it "should be able to concat an array" do
- result = scope.function_concat([['1','2','3'],['4','5','6']])
- expect(result).to(eq(['1','2','3','4','5','6']))
- end
-
- it "should be able to concat a primitive to an array" do
- result = scope.function_concat([['1','2','3'],'4'])
- expect(result).to(eq(['1','2','3','4']))
- end
-
- it "should not accidentally flatten nested arrays" do
- result = scope.function_concat([['1','2','3'],[['4','5'],'6']])
- expect(result).to(eq(['1','2','3',['4','5'],'6']))
- end
+describe 'concat' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params([1]).and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params(1, [2]).and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params([1], [2], [3]).and_return([1, 2, 3]) }
+ it { is_expected.to run.with_params(['1','2','3'],['4','5','6']).and_return(['1','2','3','4','5','6']) }
+ it { is_expected.to run.with_params(['1','2','3'],'4').and_return(['1','2','3','4']) }
+ it { is_expected.to run.with_params(['1','2','3'],[['4','5'],'6']).and_return(['1','2','3',['4','5'],'6']) }
+ it { is_expected.to run.with_params(['1','2'],['3','4'],['5','6']).and_return(['1','2','3','4','5','6']) }
+ it { is_expected.to run.with_params(['1','2'],'3','4',['5','6']).and_return(['1','2','3','4','5','6']) }
+ it { is_expected.to run.with_params([{"a" => "b"}], {"c" => "d", "e" => "f"}).and_return([{"a" => "b"}, {"c" => "d", "e" => "f"}]) }
it "should leave the original array intact" do
- array_original = ['1','2','3']
- result = scope.function_concat([array_original,['4','5','6']])
- array_original.should(eq(['1','2','3']))
- end
-
- it "should be able to concat multiple arrays" do
- result = scope.function_concat([['1','2','3'],['4','5','6'],['7','8','9']])
- expect(result).to(eq(['1','2','3','4','5','6','7','8','9']))
- end
-
- it "should be able to concat mix of primitives and arrays to a final array" do
- result = scope.function_concat([['1','2','3'],'4',['5','6','7']])
- expect(result).to(eq(['1','2','3','4','5','6','7']))
+ argument1 = ['1','2','3']
+ original1 = argument1.dup
+ argument2 = ['4','5','6']
+ original2 = argument2.dup
+ result = subject.call([argument1,argument2])
+ expect(argument1).to eq(original1)
+ expect(argument2).to eq(original2)
end
end
diff --git a/spec/functions/convert_base_spec.rb b/spec/functions/convert_base_spec.rb
new file mode 100644
index 0000000..8ab2284
--- /dev/null
+++ b/spec/functions/convert_base_spec.rb
@@ -0,0 +1,24 @@
+require 'spec_helper'
+
+describe 'convert_base' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(ArgumentError) }
+ it { is_expected.to run.with_params("asdf").and_raise_error(ArgumentError) }
+ it { is_expected.to run.with_params("asdf","moo","cow").and_raise_error(ArgumentError) }
+ it { is_expected.to run.with_params(["1"],"2").and_raise_error(Puppet::ParseError, /argument must be either a string or an integer/) }
+ it { is_expected.to run.with_params("1",["2"]).and_raise_error(Puppet::ParseError, /argument must be either a string or an integer/) }
+ it { is_expected.to run.with_params("1",1).and_raise_error(Puppet::ParseError, /base must be at least 2 and must not be greater than 36/) }
+ it { is_expected.to run.with_params("1",37).and_raise_error(Puppet::ParseError, /base must be at least 2 and must not be greater than 36/) }
+
+ it "should raise a ParseError if argument 1 is a string that does not correspond to an integer in base 10" do
+ is_expected.to run.with_params("ten",6).and_raise_error(Puppet::ParseError, /argument must be an integer or a string corresponding to an integer in base 10/)
+ end
+
+ it "should raise a ParseError if argument 2 is a string and does not correspond to an integer in base 10" do
+ is_expected.to run.with_params(100,"hex").and_raise_error(Puppet::ParseError, /argument must be an integer or a string corresponding to an integer in base 10/)
+ end
+
+ it { is_expected.to run.with_params("11",'16').and_return('b') }
+ it { is_expected.to run.with_params("35",'36').and_return('z') }
+ it { is_expected.to run.with_params(5, 2).and_return('101') }
+end
diff --git a/spec/functions/count_spec.rb b/spec/functions/count_spec.rb
index f8f1d48..c8d1960 100755
--- a/spec/functions/count_spec.rb
+++ b/spec/functions/count_spec.rb
@@ -1,31 +1,18 @@
-#! /usr/bin/env ruby -S rspec
-
require 'spec_helper'
-describe "the count function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- it "should exist" do
- expect(Puppet::Parser::Functions.function("count")).to eq("function_count")
- end
-
- it "should raise a ArgumentError if there is more than 2 arguments" do
- expect { scope.function_count(['foo', 'bar', 'baz']) }.to( raise_error(ArgumentError))
- end
-
- it "should be able to count arrays" do
- expect(scope.function_count([["1","2","3"]])).to(eq(3))
- end
-
- it "should be able to count matching elements in arrays" do
- expect(scope.function_count([["1", "2", "2"], "2"])).to(eq(2))
- end
-
- it "should not count nil or empty strings" do
- expect(scope.function_count([["foo","bar",nil,""]])).to(eq(2))
- end
-
- it 'does not count an undefined hash key or an out of bound array index (which are both :undef)' do
- expect(scope.function_count([["foo",:undef,:undef]])).to eq(1)
- end
+describe 'count' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(ArgumentError) }
+ it { is_expected.to run.with_params("one").and_raise_error(ArgumentError) }
+ it { is_expected.to run.with_params("one", "two").and_return(1) }
+ it {
+ pending("should actually be like this, and not like above")
+ is_expected.to run.with_params("one", "two").and_raise_error(ArgumentError)
+ }
+ it { is_expected.to run.with_params("one", "two", "three").and_raise_error(ArgumentError) }
+ it { is_expected.to run.with_params(["one", "two", "three"]).and_return(3) }
+ it { is_expected.to run.with_params(["one", "two", "two"], "two").and_return(2) }
+ it { is_expected.to run.with_params(["one", nil, "two"]).and_return(2) }
+ it { is_expected.to run.with_params(["one", "", "two"]).and_return(2) }
+ it { is_expected.to run.with_params(["one", :undef, "two"]).and_return(2) }
end
diff --git a/spec/functions/deep_merge_spec.rb b/spec/functions/deep_merge_spec.rb
index 7087904..397e048 100755
--- a/spec/functions/deep_merge_spec.rb
+++ b/spec/functions/deep_merge_spec.rb
@@ -1,105 +1,55 @@
-#! /usr/bin/env ruby -S rspec
-
require 'spec_helper'
-describe Puppet::Parser::Functions.function(:deep_merge) do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- describe 'when calling deep_merge from puppet' do
- it "should not compile when no arguments are passed" do
- skip("Fails on 2.6.x, see bug #15912") if Puppet.version =~ /^2\.6\./
- Puppet[:code] = '$x = deep_merge()'
- expect {
- scope.compiler.compile
- }.to raise_error(Puppet::ParseError, /wrong number of arguments/)
- end
-
- it "should not compile when 1 argument is passed" do
- skip("Fails on 2.6.x, see bug #15912") if Puppet.version =~ /^2\.6\./
- Puppet[:code] = "$my_hash={'one' => 1}\n$x = deep_merge($my_hash)"
- expect {
- scope.compiler.compile
- }.to raise_error(Puppet::ParseError, /wrong number of arguments/)
- end
+describe 'deep_merge' do
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params({ 'key' => 'value' }).and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params({}, '2').and_raise_error(Puppet::ParseError, /unexpected argument type String/) }
+ it { is_expected.to run.with_params({}, 2).and_raise_error(Puppet::ParseError, /unexpected argument type Fixnum/) }
+ it { is_expected.to run.with_params({}, '').and_return({}) }
+ it { is_expected.to run.with_params({}, {}).and_return({}) }
+ it { is_expected.to run.with_params({}, {}, {}).and_return({}) }
+ it { is_expected.to run.with_params({}, {}, {}, {}).and_return({}) }
+ it { is_expected.to run.with_params({'key' => 'value'}, '').and_return({'key' => 'value'}) }
+ it { is_expected.to run.with_params({'key1' => 'value1'}, {'key2' => 'value2' }).and_return({'key1' => 'value1', 'key2' => 'value2'}) }
+
+ describe 'when arguments have key collisions' do
+ it 'should prefer values from the last hash' do
+ is_expected.to run \
+ .with_params(
+ {'key1' => 'value1', 'key2' => 'value2' },
+ {'key2' => 'replacement_value', 'key3' => 'value3'}) \
+ .and_return(
+ {'key1' => 'value1', 'key2' => 'replacement_value', 'key3' => 'value3'})
+ end
+ it { is_expected.to run \
+ .with_params({'key1' => 'value1'}, {'key1' => 'value2'}, {'key1' => 'value3'}) \
+ .and_return({'key1' => 'value3' })
+ }
end
- describe 'when calling deep_merge on the scope instance' do
- it 'should require all parameters are hashes' do
- expect { new_hash = scope.function_deep_merge([{}, '2'])}.to raise_error(Puppet::ParseError, /unexpected argument type String/)
- expect { new_hash = scope.function_deep_merge([{}, 2])}.to raise_error(Puppet::ParseError, /unexpected argument type Fixnum/)
- end
-
- it 'should accept empty strings as puppet undef' do
- expect { new_hash = scope.function_deep_merge([{}, ''])}.not_to raise_error
- end
-
- it 'should be able to deep_merge two hashes' do
- new_hash = scope.function_deep_merge([{'one' => '1', 'two' => '1'}, {'two' => '2', 'three' => '2'}])
- expect(new_hash['one']).to eq('1')
- expect(new_hash['two']).to eq('2')
- expect(new_hash['three']).to eq('2')
- end
-
- it 'should deep_merge multiple hashes' do
- hash = scope.function_deep_merge([{'one' => 1}, {'one' => '2'}, {'one' => '3'}])
- expect(hash['one']).to eq('3')
- end
-
- it 'should accept empty hashes' do
- expect(scope.function_deep_merge([{},{},{}])).to eq({})
- end
-
- it 'should deep_merge subhashes' do
- hash = scope.function_deep_merge([{'one' => 1}, {'two' => 2, 'three' => { 'four' => 4 } }])
- expect(hash['one']).to eq(1)
- expect(hash['two']).to eq(2)
- expect(hash['three']).to eq({ 'four' => 4 })
- end
-
- it 'should append to subhashes' do
- hash = scope.function_deep_merge([{'one' => { 'two' => 2 } }, { 'one' => { 'three' => 3 } }])
- expect(hash['one']).to eq({ 'two' => 2, 'three' => 3 })
- end
-
- it 'should append to subhashes 2' do
- hash = scope.function_deep_merge([{'one' => 1, 'two' => 2, 'three' => { 'four' => 4 } }, {'two' => 'dos', 'three' => { 'five' => 5 } }])
- expect(hash['one']).to eq(1)
- expect(hash['two']).to eq('dos')
- expect(hash['three']).to eq({ 'four' => 4, 'five' => 5 })
- end
-
- it 'should append to subhashes 3' do
- hash = scope.function_deep_merge([{ 'key1' => { 'a' => 1, 'b' => 2 }, 'key2' => { 'c' => 3 } }, { 'key1' => { 'b' => 99 } }])
- expect(hash['key1']).to eq({ 'a' => 1, 'b' => 99 })
- expect(hash['key2']).to eq({ 'c' => 3 })
- end
-
- it 'should not change the original hashes' do
- hash1 = {'one' => { 'two' => 2 } }
- hash2 = { 'one' => { 'three' => 3 } }
- hash = scope.function_deep_merge([hash1, hash2])
- expect(hash1).to eq({'one' => { 'two' => 2 } })
- expect(hash2).to eq({ 'one' => { 'three' => 3 } })
- expect(hash['one']).to eq({ 'two' => 2, 'three' => 3 })
- end
+ describe 'when arguments have subhashes' do
+ it { is_expected.to run \
+ .with_params({'key1' => 'value1'}, {'key2' => 'value2', 'key3' => {'subkey1' => 'value4'}}) \
+ .and_return( {'key1' => 'value1', 'key2' => 'value2', 'key3' => {'subkey1' => 'value4'}})
+ }
+ it { is_expected.to run \
+ .with_params({'key1' => {'subkey1' => 'value1'}}, {'key1' => {'subkey2' => 'value2'}}) \
+ .and_return( {'key1' => {'subkey1' => 'value1', 'subkey2' => 'value2'}})
+ }
+ it { is_expected.to run \
+ .with_params({'key1' => {'subkey1' => {'subsubkey1' => 'value1'}}}, {'key1' => {'subkey1' => {'subsubkey1' => 'value2'}}}) \
+ .and_return( {'key1' => {'subkey1' => {'subsubkey1' => 'value2'}}})
+ }
+ end
- it 'should not change the original hashes 2' do
- hash1 = {'one' => { 'two' => [1,2] } }
- hash2 = { 'one' => { 'three' => 3 } }
- hash = scope.function_deep_merge([hash1, hash2])
- expect(hash1).to eq({'one' => { 'two' => [1,2] } })
- expect(hash2).to eq({ 'one' => { 'three' => 3 } })
- expect(hash['one']).to eq({ 'two' => [1,2], 'three' => 3 })
- end
+ it 'should not change the original hashes' do
+ argument1 = { 'key1' => 'value1' }
+ original1 = argument1.dup
+ argument2 = { 'key2' => 'value2' }
+ original2 = argument2.dup
- it 'should not change the original hashes 3' do
- hash1 = {'one' => { 'two' => [1,2, {'two' => 2} ] } }
- hash2 = { 'one' => { 'three' => 3 } }
- hash = scope.function_deep_merge([hash1, hash2])
- expect(hash1).to eq({'one' => { 'two' => [1,2, {'two' => 2}] } })
- expect(hash2).to eq({ 'one' => { 'three' => 3 } })
- expect(hash['one']).to eq({ 'two' => [1,2, {'two' => 2} ], 'three' => 3 })
- expect(hash['one']['two']).to eq([1,2, {'two' => 2}])
- end
+ subject.call([argument1, argument2])
+ expect(argument1).to eq(original1)
+ expect(argument2).to eq(original2)
end
end
diff --git a/spec/functions/defined_with_params_spec.rb b/spec/functions/defined_with_params_spec.rb
index 3590304..e2f3abe 100755
--- a/spec/functions/defined_with_params_spec.rb
+++ b/spec/functions/defined_with_params_spec.rb
@@ -1,37 +1,35 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-require 'rspec-puppet'
describe 'defined_with_params' do
- describe 'when a resource is not specified' do
+ describe 'when no resource is specified' do
it { is_expected.to run.with_params().and_raise_error(ArgumentError) }
end
describe 'when compared against a resource with no attributes' do
let :pre_condition do
'user { "dan": }'
end
- it do
- is_expected.to run.with_params('User[dan]', {}).and_return(true)
- is_expected.to run.with_params('User[bob]', {}).and_return(false)
- is_expected.to run.with_params('User[dan]', {'foo' => 'bar'}).and_return(false)
- end
+ it { is_expected.to run.with_params('User[dan]', {}).and_return(true) }
+ it { is_expected.to run.with_params('User[bob]', {}).and_return(false) }
+ it { is_expected.to run.with_params('User[dan]', {'foo' => 'bar'}).and_return(false) }
end
describe 'when compared against a resource with attributes' do
let :pre_condition do
'user { "dan": ensure => present, shell => "/bin/csh", managehome => false}'
end
- it do
- is_expected.to run.with_params('User[dan]', {}).and_return(true)
- is_expected.to run.with_params('User[dan]', '').and_return(true)
- is_expected.to run.with_params('User[dan]', {'ensure' => 'present'}
- ).and_return(true)
- is_expected.to run.with_params('User[dan]',
- {'ensure' => 'present', 'managehome' => false}
- ).and_return(true)
- is_expected.to run.with_params('User[dan]',
- {'ensure' => 'absent', 'managehome' => false}
- ).and_return(false)
+ it { is_expected.to run.with_params('User[dan]', {}).and_return(true) }
+ it { is_expected.to run.with_params('User[dan]', '').and_return(true) }
+ it { is_expected.to run.with_params('User[dan]', {'ensure' => 'present'}).and_return(true) }
+ it { is_expected.to run.with_params('User[dan]', {'ensure' => 'present', 'managehome' => false}).and_return(true) }
+ it { is_expected.to run.with_params('User[dan]', {'ensure' => 'absent', 'managehome' => false}).and_return(false) }
+ end
+
+ describe 'when passing undef values' do
+ let :pre_condition do
+ 'file { "/tmp/a": ensure => present }'
end
+
+ it { is_expected.to run.with_params('File[/tmp/a]', {}).and_return(true) }
+ it { is_expected.to run.with_params('File[/tmp/a]', { 'ensure' => 'present', 'owner' => :undef }).and_return(true) }
end
end
diff --git a/spec/functions/delete_at_spec.rb b/spec/functions/delete_at_spec.rb
index 7c20aec..0e19472 100755
--- a/spec/functions/delete_at_spec.rb
+++ b/spec/functions/delete_at_spec.rb
@@ -1,25 +1,28 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the delete_at function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+describe 'delete_at' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params('one', 1).and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params(1, 1).and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params(['one'], 'two').and_raise_error(Puppet::ParseError) }
+ it {
+ pending("Current implementation ignores parameters after the first two.")
+ is_expected.to run.with_params(['one'], 0, 1).and_raise_error(Puppet::ParseError)
+ }
- it "should exist" do
- expect(Puppet::Parser::Functions.function("delete_at")).to eq("function_delete_at")
+ describe 'argument validation' do
+ it { is_expected.to run.with_params([0, 1, 2], 3).and_raise_error(Puppet::ParseError) }
end
- it "should raise a ParseError if there is less than 1 arguments" do
- expect { scope.function_delete_at([]) }.to( raise_error(Puppet::ParseError))
- end
-
- it "should delete an item at specified location from an array" do
- result = scope.function_delete_at([['a','b','c'],1])
- expect(result).to(eq(['a','c']))
- end
+ it { is_expected.to run.with_params([0, 1, 2], 1).and_return([0, 2]) }
+ it { is_expected.to run.with_params([0, 1, 2], -1).and_return([0, 1]) }
+ it { is_expected.to run.with_params([0, 1, 2], -4).and_return([0, 1, 2]) }
- it "should not change origin array passed as argument" do
- origin_array = ['a','b','c','d']
- result = scope.function_delete_at([origin_array, 1])
- expect(origin_array).to(eq(['a','b','c','d']))
+ it "should leave the original array intact" do
+ argument = [1, 2, 3]
+ original = argument.dup
+ result = subject.call([argument,2])
+ expect(argument).to eq(original)
end
end
diff --git a/spec/functions/delete_regex_spec.rb b/spec/functions/delete_regex_spec.rb
new file mode 100755
index 0000000..f27a946
--- /dev/null
+++ b/spec/functions/delete_regex_spec.rb
@@ -0,0 +1,54 @@
+require 'spec_helper'
+
+describe 'delete_regex' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params([], 'two') }
+ it { is_expected.to run.with_params({}, 'two') }
+ it { is_expected.to run.with_params([], 'two', 'three').and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params([], 'two', 'three', 'four').and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params(1, 'two').and_raise_error(TypeError) }
+
+ describe 'deleting from an array' do
+ it { is_expected.to run.with_params([], '').and_return([]) }
+ it { is_expected.to run.with_params([], 'two').and_return([]) }
+ it { is_expected.to run.with_params(['two'], 'two').and_return([]) }
+ it { is_expected.to run.with_params(['two', 'two'], 'two').and_return([]) }
+ it { is_expected.to run.with_params(['one', 'two', 'three'], '^t.*').and_return(['one']) }
+ it { is_expected.to run.with_params(['ab', 'b', 'c', 'b'], 'b').and_return(['ab', 'c']) }
+ it { is_expected.to run.with_params(['one', 'two', 'three'], 'four').and_return(['one', 'two', 'three']) }
+ it { is_expected.to run.with_params(['one', 'two', 'three'], 'e').and_return(['one', 'two', 'three']) }
+ it { is_expected.to run.with_params(['one', 'two', 'three'], 'two').and_return(['one', 'three']) }
+ it { is_expected.to run.with_params(['two', 'one', 'two', 'three', 'two'], 'two').and_return(['one', 'three']) }
+ it { is_expected.to run.with_params(['abracadabra'], 'abr').and_return(['abracadabra']) }
+ it { is_expected.to run.with_params(['abracadabra'], '^.*jimbob.*$').and_return(['abracadabra']) }
+ end
+
+ describe 'deleting from an array' do
+ it { is_expected.to run.with_params({}, '').and_return({}) }
+ it { is_expected.to run.with_params({}, 'key').and_return({}) }
+ it { is_expected.to run.with_params({'key' => 'value'}, 'key').and_return({}) }
+ it { is_expected.to run \
+ .with_params({'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3'}, 'key2') \
+ .and_return( {'key1' => 'value1', 'key3' => 'value3'})
+ }
+ it { is_expected.to run \
+ .with_params({'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3'}, ['key1', 'key2']) \
+ .and_return( {'key3' => 'value3'})
+ }
+ end
+
+ it "should leave the original array intact" do
+ argument1 = ['one','two','three']
+ original1 = argument1.dup
+ subject.call([argument1,'two'])
+ expect(argument1).to eq(original1)
+ end
+ it "should leave the original hash intact" do
+ argument1 = {'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3'}
+ original1 = argument1.dup
+ subject.call([argument1,'key2'])
+ expect(argument1).to eq(original1)
+ end
+end
diff --git a/spec/functions/delete_spec.rb b/spec/functions/delete_spec.rb
index c8edd78..b44accf 100755
--- a/spec/functions/delete_spec.rb
+++ b/spec/functions/delete_spec.rb
@@ -1,61 +1,70 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the delete function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+describe 'delete' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params([], 'two') }
+ it { is_expected.to run.with_params([], 'two', 'three').and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params(1, 'two').and_raise_error(TypeError) }
- it "should exist" do
- expect(Puppet::Parser::Functions.function("delete")).to eq("function_delete")
+ describe 'deleting from an array' do
+ it { is_expected.to run.with_params([], '').and_return([]) }
+ it { is_expected.to run.with_params([], 'two').and_return([]) }
+ it { is_expected.to run.with_params(['two'], 'two').and_return([]) }
+ it { is_expected.to run.with_params(['two', 'two'], 'two').and_return([]) }
+ it { is_expected.to run.with_params(['ab', 'b', 'c', 'b'], 'b').and_return(['ab', 'c']) }
+ it { is_expected.to run.with_params(['one', 'two', 'three'], 'four').and_return(['one', 'two', 'three']) }
+ it { is_expected.to run.with_params(['one', 'two', 'three'], 'e').and_return(['one', 'two', 'three']) }
+ it { is_expected.to run.with_params(['one', 'two', 'three'], 'two').and_return(['one', 'three']) }
+ it { is_expected.to run.with_params(['two', 'one', 'two', 'three', 'two'], 'two').and_return(['one', 'three']) }
+ it { is_expected.to run.with_params(['one', 'two', 'three', 'two'], ['one', 'two']).and_return(['three']) }
end
- it "should raise a ParseError if there are fewer than 2 arguments" do
- expect { scope.function_delete([]) }.to(raise_error(Puppet::ParseError))
+ describe 'deleting from a string' do
+ it { is_expected.to run.with_params('', '').and_return('') }
+ it { is_expected.to run.with_params('bar', '').and_return('bar') }
+ it { is_expected.to run.with_params('', 'bar').and_return('') }
+ it { is_expected.to run.with_params('bar', 'bar').and_return('') }
+ it { is_expected.to run.with_params('barbar', 'bar').and_return('') }
+ it { is_expected.to run.with_params('barfoobar', 'bar').and_return('foo') }
+ it { is_expected.to run.with_params('foobarbabarz', 'bar').and_return('foobaz') }
+ it { is_expected.to run.with_params('foobarbabarz', ['foo', 'bar']).and_return('baz') }
+ # this is so sick
+ it { is_expected.to run.with_params('barfoobar', ['barbar', 'foo']).and_return('barbar') }
+ it { is_expected.to run.with_params('barfoobar', ['foo', 'barbar']).and_return('') }
end
- it "should raise a ParseError if there are greater than 2 arguments" do
- expect { scope.function_delete([[], 'foo', 'bar']) }.to(raise_error(Puppet::ParseError))
+ describe 'deleting from an array' do
+ it { is_expected.to run.with_params({}, '').and_return({}) }
+ it { is_expected.to run.with_params({}, 'key').and_return({}) }
+ it { is_expected.to run.with_params({'key' => 'value'}, 'key').and_return({}) }
+ it { is_expected.to run \
+ .with_params({'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3'}, 'key2') \
+ .and_return( {'key1' => 'value1', 'key3' => 'value3'})
+ }
+ it { is_expected.to run \
+ .with_params({'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3'}, ['key1', 'key2']) \
+ .and_return( {'key3' => 'value3'})
+ }
end
- it "should raise a TypeError if a number is passed as the first argument" do
- expect { scope.function_delete([1, 'bar']) }.to(raise_error(TypeError))
+ it "should leave the original array intact" do
+ argument1 = ['one','two','three']
+ original1 = argument1.dup
+ result = subject.call([argument1,'two'])
+ expect(argument1).to eq(original1)
end
-
- it "should delete all instances of an element from an array" do
- result = scope.function_delete([['a', 'b', 'c', 'b'], 'b'])
- expect(result).to(eq(['a', 'c']))
- end
-
- it "should delete all instances of a substring from a string" do
- result = scope.function_delete(['foobarbabarz', 'bar'])
- expect(result).to(eq('foobaz'))
+ it "should leave the original string intact" do
+ argument1 = 'onetwothree'
+ original1 = argument1.dup
+ result = subject.call([argument1,'two'])
+ expect(argument1).to eq(original1)
end
-
- it "should delete a key from a hash" do
- result = scope.function_delete([{'a' => 1, 'b' => 2, 'c' => 3}, 'b'])
- expect(result).to(eq({'a' => 1, 'c' => 3}))
+ it "should leave the original hash intact" do
+ argument1 = {'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3'}
+ original1 = argument1.dup
+ result = subject.call([argument1,'key2'])
+ expect(argument1).to eq(original1)
end
-
- it 'should accept an array of items to delete' do
- result = scope.function_delete([{'a' => 1, 'b' => 2, 'c' => 3}, ['b', 'c']])
- expect(result).to(eq({'a' => 1}))
- end
-
- it "should not change origin array passed as argument" do
- origin_array = ['a', 'b', 'c', 'd']
- result = scope.function_delete([origin_array, 'b'])
- expect(origin_array).to(eq(['a', 'b', 'c', 'd']))
- end
-
- it "should not change the origin string passed as argument" do
- origin_string = 'foobarbabarz'
- result = scope.function_delete([origin_string, 'bar'])
- expect(origin_string).to(eq('foobarbabarz'))
- end
-
- it "should not change origin hash passed as argument" do
- origin_hash = {'a' => 1, 'b' => 2, 'c' => 3}
- result = scope.function_delete([origin_hash, 'b'])
- expect(origin_hash).to(eq({'a' => 1, 'b' => 2, 'c' => 3}))
- end
-
end
diff --git a/spec/functions/delete_undef_values_spec.rb b/spec/functions/delete_undef_values_spec.rb
index dc67953..ec9fb9c 100755
--- a/spec/functions/delete_undef_values_spec.rb
+++ b/spec/functions/delete_undef_values_spec.rb
@@ -1,41 +1,56 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the delete_undef_values function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- it "should exist" do
- expect(Puppet::Parser::Functions.function("delete_undef_values")).to eq("function_delete_undef_values")
- end
-
- it "should raise a ParseError if there is less than 1 argument" do
- expect { scope.function_delete_undef_values([]) }.to( raise_error(Puppet::ParseError))
- end
-
- it "should raise a ParseError if the argument is not Array nor Hash" do
- expect { scope.function_delete_undef_values(['']) }.to( raise_error(Puppet::ParseError))
- expect { scope.function_delete_undef_values([nil]) }.to( raise_error(Puppet::ParseError))
- end
-
- it "should delete all undef items from Array and only these" do
- result = scope.function_delete_undef_values([['a',:undef,'c','undef']])
- expect(result).to(eq(['a','c','undef']))
- end
-
- it "should delete all undef items from Hash and only these" do
- result = scope.function_delete_undef_values([{'a'=>'A','b'=>:undef,'c'=>'C','d'=>'undef'}])
- expect(result).to(eq({'a'=>'A','c'=>'C','d'=>'undef'}))
- end
-
- it "should not change origin array passed as argument" do
- origin_array = ['a',:undef,'c','undef']
- result = scope.function_delete_undef_values([origin_array])
- expect(origin_array).to(eq(['a',:undef,'c','undef']))
+describe 'delete_undef_values' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError) }
+
+ describe 'when deleting from an array' do
+ [ :undef, '', nil ].each do |undef_value|
+ describe "when undef is represented by #{undef_value.inspect}" do
+ before do
+ pending("review behaviour when being passed undef as #{undef_value.inspect}") if undef_value == ''
+ pending("review behaviour when being passed undef as #{undef_value.inspect}") if undef_value == nil
+ end
+ it { is_expected.to run.with_params([undef_value]).and_return([]) }
+ it { is_expected.to run.with_params(['one',undef_value,'two','three']).and_return(['one','two','three']) }
+ end
+
+ it "should leave the original argument intact" do
+ argument = ['one',undef_value,'two']
+ original = argument.dup
+ result = subject.call([argument,2])
+ expect(argument).to eq(original)
+ end
+ end
+
+ it { is_expected.to run.with_params(['undef']).and_return(['undef']) }
end
- it "should not change origin hash passed as argument" do
- origin_hash = { 'a' => 1, 'b' => :undef, 'c' => 'undef' }
- result = scope.function_delete_undef_values([origin_hash])
- expect(origin_hash).to(eq({ 'a' => 1, 'b' => :undef, 'c' => 'undef' }))
+ describe 'when deleting from a hash' do
+ [ :undef, '', nil ].each do |undef_value|
+ describe "when undef is represented by #{undef_value.inspect}" do
+ before do
+ pending("review behaviour when being passed undef as #{undef_value.inspect}") if undef_value == ''
+ pending("review behaviour when being passed undef as #{undef_value.inspect}") if undef_value == nil
+ end
+ it { is_expected.to run.with_params({'key' => undef_value}).and_return({}) }
+ it { is_expected.to run \
+ .with_params({'key1' => 'value1', 'undef_key' => undef_value, 'key2' => 'value2'}) \
+ .and_return({'key1' => 'value1', 'key2' => 'value2'})
+ }
+ end
+
+ it "should leave the original argument intact" do
+ argument = { 'key1' => 'value1', 'key2' => undef_value }
+ original = argument.dup
+ result = subject.call([argument,2])
+ expect(argument).to eq(original)
+ end
+ end
+
+ it { is_expected.to run.with_params({'key' => 'undef'}).and_return({'key' => 'undef'}) }
end
end
diff --git a/spec/functions/delete_values_spec.rb b/spec/functions/delete_values_spec.rb
index 4f4d411..12907d4 100755
--- a/spec/functions/delete_values_spec.rb
+++ b/spec/functions/delete_values_spec.rb
@@ -1,36 +1,37 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the delete_values function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- it "should exist" do
- expect(Puppet::Parser::Functions.function("delete_values")).to eq("function_delete_values")
- end
-
- it "should raise a ParseError if there are fewer than 2 arguments" do
- expect { scope.function_delete_values([]) }.to( raise_error(Puppet::ParseError))
- end
-
- it "should raise a ParseError if there are greater than 2 arguments" do
- expect { scope.function_delete_values([[], 'foo', 'bar']) }.to( raise_error(Puppet::ParseError))
- end
-
- it "should raise a TypeError if the argument is not a hash" do
- expect { scope.function_delete_values([1,'bar']) }.to( raise_error(TypeError))
- expect { scope.function_delete_values(['foo','bar']) }.to( raise_error(TypeError))
- expect { scope.function_delete_values([[],'bar']) }.to( raise_error(TypeError))
+describe 'delete_values' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params('one', 'two', 'three').and_raise_error(Puppet::ParseError) }
+ describe 'when the first argument is not a hash' do
+ it { is_expected.to run.with_params(1, 'two').and_raise_error(TypeError) }
+ it { is_expected.to run.with_params('one', 'two').and_raise_error(TypeError) }
+ it { is_expected.to run.with_params([], 'two').and_raise_error(TypeError) }
end
- it "should delete all instances of a value from a hash" do
- result = scope.function_delete_values([{ 'a'=>'A', 'b'=>'B', 'B'=>'C', 'd'=>'B' },'B'])
- expect(result).to(eq({ 'a'=>'A', 'B'=>'C' }))
+ describe 'when deleting from a hash' do
+ it { is_expected.to run.with_params({}, 'value').and_return({}) }
+ it { is_expected.to run \
+ .with_params({'key1' => 'value1'}, 'non-existing value') \
+ .and_return({'key1' => 'value1'})
+ }
+ it { is_expected.to run \
+ .with_params({'key1' => 'value1', 'key2' => 'value to delete'}, 'value to delete') \
+ .and_return({'key1' => 'value1'})
+ }
+ it { is_expected.to run \
+ .with_params({'key1' => 'value1', 'key2' => 'value to delete', 'key3' => 'value to delete'}, 'value to delete') \
+ .and_return({'key1' => 'value1'})
+ }
end
- it "should not change origin hash passed as argument" do
- origin_hash = { 'a' => 1, 'b' => 2, 'c' => 3 }
- result = scope.function_delete_values([origin_hash, 2])
- expect(origin_hash).to(eq({ 'a' => 1, 'b' => 2, 'c' => 3 }))
+ it "should leave the original argument intact" do
+ argument = { 'key1' => 'value1', 'key2' => 'value2' }
+ original = argument.dup
+ result = subject.call([argument, 'value2'])
+ expect(argument).to eq(original)
end
-
end
diff --git a/spec/functions/deprecation_spec.rb b/spec/functions/deprecation_spec.rb
new file mode 100644
index 0000000..cee4f1c
--- /dev/null
+++ b/spec/functions/deprecation_spec.rb
@@ -0,0 +1,60 @@
+require 'spec_helper'
+
+if Puppet.version.to_f >= 4.0
+ describe 'deprecation' do
+ before(:each) {
+ # this is to reset the strict variable to default
+ Puppet.settings[:strict] = :warning
+ }
+
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(ArgumentError) }
+
+ it 'should display a single warning' do
+ Puppet.expects(:warning).with(includes('heelo'))
+ is_expected.to run.with_params('key', 'heelo')
+ end
+
+ it 'should display a single warning, despite multiple calls' do
+ Puppet.expects(:warning).with(includes('heelo')).once
+ is_expected.to run.with_params('key', 'heelo')
+ is_expected.to run.with_params('key', 'heelo')
+ end
+
+ it 'should fail twice with message, with multiple calls. when strict= :error' do
+ Puppet.settings[:strict] = :error
+ Puppet.expects(:warning).with(includes('heelo')).never
+ is_expected.to run.with_params('key', 'heelo').and_raise_error(RuntimeError, /deprecation. key. heelo/)
+ is_expected.to run.with_params('key', 'heelo').and_raise_error(RuntimeError, /deprecation. key. heelo/)
+ end
+
+ it 'should display nothing, despite multiple calls. strict= :off' do
+ Puppet.settings[:strict] = :off
+ Puppet.expects(:warning).with(includes('heelo')).never
+ is_expected.to run.with_params('key', 'heelo')
+ is_expected.to run.with_params('key', 'heelo')
+ end
+
+ after(:all) {
+ # this is to reset the strict variable to default
+ Puppet.settings[:strict] = :warning
+ }
+ end
+else
+ # Puppet version < 4 will use these tests.
+ describe 'deprecation' do
+ after(:all) do
+ ENV.delete('STDLIB_LOG_DEPRECATIONS')
+ end
+ before(:all) do
+ ENV['STDLIB_LOG_DEPRECATIONS'] = "true"
+ end
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+
+ it 'should display a single warning' do
+ scope.expects(:warning).with(includes('heelo'))
+ is_expected.to run.with_params('key', 'heelo')
+ end
+ end
+end
diff --git a/spec/functions/difference_spec.rb b/spec/functions/difference_spec.rb
index 24b2b1b..d5e983d 100755
--- a/spec/functions/difference_spec.rb
+++ b/spec/functions/difference_spec.rb
@@ -1,19 +1,21 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the difference function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- it "should exist" do
- expect(Puppet::Parser::Functions.function("difference")).to eq("function_difference")
- end
-
- it "should raise a ParseError if there are fewer than 2 arguments" do
- expect { scope.function_difference([]) }.to( raise_error(Puppet::ParseError) )
- end
-
- it "should return the difference between two arrays" do
- result = scope.function_difference([["a","b","c"],["b","c","d"]])
- expect(result).to(eq(["a"]))
- end
+describe 'difference' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params('one', 'two', 'three').and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params('one', []).and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params([], 'two').and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params({}, {}).and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params([], []).and_return([]) }
+ it { is_expected.to run.with_params([], ['one']).and_return([]) }
+ it { is_expected.to run.with_params(['one'], ['one']).and_return([]) }
+ it { is_expected.to run.with_params(['one'], []).and_return(['one']) }
+ it { is_expected.to run.with_params(['one', 'two', 'three'], ['two', 'three']).and_return(['one']) }
+ it { is_expected.to run.with_params(['one', 'two', 'two', 'three'], ['two', 'three']).and_return(['one']) }
+ it { is_expected.to run.with_params(['one', 'two', 'three'], ['two', 'two', 'three']).and_return(['one']) }
+ it { is_expected.to run.with_params(['one', 'two', 'three'], ['two', 'three', 'four']).and_return(['one']) }
+ it 'should not confuse types' do is_expected.to run.with_params(['1', '2', '3'], [1, 2]).and_return(['1', '2', '3']) end
end
diff --git a/spec/functions/dig44_spec.rb b/spec/functions/dig44_spec.rb
new file mode 100644
index 0000000..4f7408d
--- /dev/null
+++ b/spec/functions/dig44_spec.rb
@@ -0,0 +1,105 @@
+require 'spec_helper'
+
+describe 'dig44' do
+
+ let(:data) do
+ {
+ 'a' => {
+ 'g' => '2',
+ 'e' => [
+ 'f0',
+ 'f1',
+ {
+ 'x' => {
+ 'y' => 'z'
+ }
+ },
+ 'f3',
+ ]
+ },
+ 'b' => true,
+ 'c' => false,
+ 'd' => '1',
+ 'e' => :undef,
+ 'f' => nil,
+ }
+ end
+
+ context 'single values' do
+ it 'should exist' do
+ is_expected.not_to be_nil
+ end
+
+ it 'should require two arguments' do
+ is_expected.to run.with_params().and_raise_error(ArgumentError)
+ end
+
+ it 'should fail if the data is not a structure' do
+ is_expected.to run.with_params('test', []).and_raise_error(Puppet::Error)
+ end
+
+ it 'should fail if the path is not an array' do
+ is_expected.to run.with_params({}, '').and_raise_error(Puppet::Error)
+ end
+
+ it 'should return the value if the value is string' do
+ is_expected.to run.with_params(data, ['d'], 'default').and_return('1')
+ end
+
+ it 'should return true if the value is true' do
+ is_expected.to run.with_params(data, ['b'], 'default').and_return(true)
+ end
+
+ it 'should return false if the value is false' do
+ is_expected.to run.with_params(data, ['c'], 'default').and_return(false)
+ end
+
+ it 'should return the default if the value is nil' do
+ is_expected.to run.with_params(data, ['f'], 'default').and_return('default')
+ end
+
+ it 'should return the default if the value is :undef (same as nil)' do
+ is_expected.to run.with_params(data, ['e'], 'default').and_return('default')
+ end
+
+ it 'should return the default if the path is not found' do
+ is_expected.to run.with_params(data, ['missing'], 'default').and_return('default')
+ end
+ end
+
+ context 'structure values' do
+
+ it 'should be able to extract a deeply nested hash value' do
+ is_expected.to run.with_params(data, %w(a g), 'default').and_return('2')
+ end
+
+ it 'should return the default value if the path is too long' do
+ is_expected.to run.with_params(data, %w(a g c d), 'default').and_return('default')
+ end
+
+ it 'should support an array index (number) in the path' do
+ is_expected.to run.with_params(data, ['a', 'e', 1], 'default').and_return('f1')
+ end
+
+ it 'should support an array index (string) in the path' do
+ is_expected.to run.with_params(data, %w(a e 1), 'default').and_return('f1')
+ end
+
+ it 'should return the default value if an array index is not a number' do
+ is_expected.to run.with_params(data, %w(a b c), 'default').and_return('default')
+ end
+
+ it 'should return the default value if and index is out of array length' do
+ is_expected.to run.with_params(data, %w(a e 5), 'default').and_return('default')
+ end
+
+ it 'should be able to path though both arrays and hashes' do
+ is_expected.to run.with_params(data, %w(a e 2 x y), 'default').and_return('z')
+ end
+
+ it 'should return "nil" if value is not found and no default value is provided' do
+ is_expected.to run.with_params(data, %w(a 1)).and_return(nil)
+ end
+
+ end
+end
diff --git a/spec/functions/dig_spec.rb b/spec/functions/dig_spec.rb
new file mode 100644
index 0000000..ad16fdd
--- /dev/null
+++ b/spec/functions/dig_spec.rb
@@ -0,0 +1,13 @@
+require 'spec_helper'
+
+describe 'dig' do
+
+ it "should exist" do
+ expect(Puppet::Parser::Functions.function("dig")).to eq("function_dig")
+ end
+
+ it "should give a deprecation warning when called" do
+ scope.expects(:warning).with("dig() DEPRECATED: This function has been replaced in Puppet 4.5.0, please use dig44() for backwards compatibility or use the new version.")
+ scope.function_dig([{}, []])
+ end
+end
diff --git a/spec/functions/dirname_spec.rb b/spec/functions/dirname_spec.rb
index 8a3bcab..46c4c35 100755
--- a/spec/functions/dirname_spec.rb
+++ b/spec/functions/dirname_spec.rb
@@ -1,24 +1,13 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the dirname function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- it "should exist" do
- expect(Puppet::Parser::Functions.function("dirname")).to eq("function_dirname")
- end
-
- it "should raise a ParseError if there is less than 1 arguments" do
- expect { scope.function_dirname([]) }.to( raise_error(Puppet::ParseError))
- end
-
- it "should return dirname for an absolute path" do
- result = scope.function_dirname(['/path/to/a/file.ext'])
- expect(result).to(eq('/path/to/a'))
- end
-
- it "should return dirname for a relative path" do
- result = scope.function_dirname(['path/to/a/file.ext'])
- expect(result).to(eq('path/to/a'))
- end
+describe 'dirname' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params('/path/to/a/file.ext', []).and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params('/path/to/a/file.ext').and_return('/path/to/a') }
+ it { is_expected.to run.with_params('relative_path/to/a/file.ext').and_return('relative_path/to/a') }
end
diff --git a/spec/functions/dos2unix_spec.rb b/spec/functions/dos2unix_spec.rb
new file mode 100644
index 0000000..9c84703
--- /dev/null
+++ b/spec/functions/dos2unix_spec.rb
@@ -0,0 +1,40 @@
+require 'spec_helper'
+
+describe 'dos2unix' do
+ context 'Checking parameter validity' do
+ it { is_expected.not_to eq(nil) }
+ it do
+ is_expected.to run.with_params.and_raise_error(ArgumentError, /Wrong number of arguments/)
+ end
+ it do
+ is_expected.to run.with_params('one', 'two').and_raise_error(ArgumentError, /Wrong number of arguments/)
+ end
+ it do
+ is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError)
+ end
+ it do
+ is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError)
+ end
+ it do
+ is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError)
+ end
+ end
+
+ context 'Converting from dos to unix format' do
+ sample_text = "Hello\r\nWorld\r\n"
+ desired_output = "Hello\nWorld\n"
+
+ it 'should output unix format' do
+ should run.with_params(sample_text).and_return(desired_output)
+ end
+ end
+
+ context 'Converting from unix to unix format' do
+ sample_text = "Hello\nWorld\n"
+ desired_output = "Hello\nWorld\n"
+
+ it 'should output unix format' do
+ should run.with_params(sample_text).and_return(desired_output)
+ end
+ end
+end
diff --git a/spec/functions/downcase_spec.rb b/spec/functions/downcase_spec.rb
index edebc44..c594560 100755
--- a/spec/functions/downcase_spec.rb
+++ b/spec/functions/downcase_spec.rb
@@ -1,33 +1,15 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the downcase function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- it "should exist" do
- expect(Puppet::Parser::Functions.function("downcase")).to eq("function_downcase")
- end
-
- it "should raise a ParseError if there is less than 1 arguments" do
- expect { scope.function_downcase([]) }.to( raise_error(Puppet::ParseError))
- end
-
- it "should downcase a string" do
- result = scope.function_downcase(["ASFD"])
- expect(result).to(eq("asfd"))
- end
-
- it "should do nothing to a string that is already downcase" do
- result = scope.function_downcase(["asdf asdf"])
- expect(result).to(eq("asdf asdf"))
- end
-
- it "should accept objects which extend String" do
- class AlsoString < String
- end
-
- value = AlsoString.new("ASFD")
- result = scope.function_downcase([value])
- result.should(eq('asfd'))
- end
+describe 'downcase' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params(100).and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params("abc").and_return("abc") }
+ it { is_expected.to run.with_params("Abc").and_return("abc") }
+ it { is_expected.to run.with_params("ABC").and_return("abc") }
+
+ it { is_expected.to run.with_params(AlsoString.new("ABC")).and_return("abc") }
+ it { is_expected.to run.with_params([]).and_return([]) }
+ it { is_expected.to run.with_params(["ONE", "TWO"]).and_return(["one", "two"]) }
+ it { is_expected.to run.with_params(["One", 1, "Two"]).and_return(["one", 1, "two"]) }
end
diff --git a/spec/functions/empty_spec.rb b/spec/functions/empty_spec.rb
index 6a97c4c..a3a25d6 100755
--- a/spec/functions/empty_spec.rb
+++ b/spec/functions/empty_spec.rb
@@ -1,32 +1,22 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the empty function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
- it "should exist" do
- expect(Puppet::Parser::Functions.function("empty")).to eq("function_empty")
- end
+describe 'empty' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
+ it {
+ pending("Current implementation ignores parameters after the first.")
+ is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError)
+ }
+ it { is_expected.to run.with_params(0).and_return(false) }
+ it { is_expected.to run.with_params('').and_return(true) }
+ it { is_expected.to run.with_params('one').and_return(false) }
- it "should raise a ParseError if there is less than 1 arguments" do
- expect { scope.function_empty([]) }.to( raise_error(Puppet::ParseError))
- end
+ it { is_expected.to run.with_params(AlsoString.new('')).and_return(true) }
+ it { is_expected.to run.with_params(AlsoString.new('one')).and_return(false) }
- it "should return a true for an empty string" do
- result = scope.function_empty([''])
- expect(result).to(eq(true))
- end
+ it { is_expected.to run.with_params([]).and_return(true) }
+ it { is_expected.to run.with_params(['one']).and_return(false) }
- it "should return a false for a non-empty string" do
- result = scope.function_empty(['asdf'])
- expect(result).to(eq(false))
- end
-
- it "should accept objects which extend String" do
- class AlsoString < String
- end
-
- value = AlsoString.new()
- result = scope.function_empty([value])
- result.should(eq(true))
- end
+ it { is_expected.to run.with_params({}).and_return(true) }
+ it { is_expected.to run.with_params({'key' => 'value'}).and_return(false) }
end
diff --git a/spec/functions/ensure_packages_spec.rb b/spec/functions/ensure_packages_spec.rb
index 436be10..5d97684 100755
--- a/spec/functions/ensure_packages_spec.rb
+++ b/spec/functions/ensure_packages_spec.rb
@@ -1,81 +1,44 @@
-#! /usr/bin/env ruby
-
require 'spec_helper'
-require 'rspec-puppet'
-require 'puppet_spec/compiler'
describe 'ensure_packages' do
- include PuppetSpec::Compiler
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
+ it {
+ pending("should not accept numbers as arguments")
+ is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError)
+ }
+ it {
+ pending("should not accept numbers as arguments")
+ is_expected.to run.with_params(["packagename", 1]).and_raise_error(Puppet::ParseError)
+ }
+ it { is_expected.to run.with_params("packagename") }
+ it { is_expected.to run.with_params(["packagename1", "packagename2"]) }
- before :each do
- Puppet::Parser::Functions.autoloader.loadall
- Puppet::Parser::Functions.function(:ensure_packages)
- Puppet::Parser::Functions.function(:ensure_resource)
- Puppet::Parser::Functions.function(:defined_with_params)
- Puppet::Parser::Functions.function(:create_resources)
- end
+ context 'given a catalog with "package { puppet: ensure => absent }"' do
+ let(:pre_condition) { 'package { puppet: ensure => absent }' }
- let :node do Puppet::Node.new('localhost') end
- let :compiler do Puppet::Parser::Compiler.new(node) end
- let :scope do
- if Puppet.version.to_f >= 3.0
- Puppet::Parser::Scope.new(compiler)
- else
- newscope = Puppet::Parser::Scope.new
- newscope.compiler = compiler
- newscope.source = Puppet::Resource::Type.new(:node, :localhost)
- newscope
- end
- end
+ describe 'after running ensure_package("facter")' do
+ before { subject.call(['facter']) }
- describe 'argument handling' do
- it 'fails with no arguments' do
- expect {
- scope.function_ensure_packages([])
- }.to raise_error(Puppet::ParseError, /0 for 1 or 2/)
+ # this lambda is required due to strangeness within rspec-puppet's expectation handling
+ it { expect(lambda { catalogue }).to contain_package('puppet').with_ensure('absent') }
+ it { expect(lambda { catalogue }).to contain_package('facter').with_ensure('present') }
end
- it 'accepts an array of values' do
- scope.function_ensure_packages([['foo']])
- end
+ describe 'after running ensure_package("facter", { "provider" => "gem" })' do
+ before { subject.call(['facter', { "provider" => "gem" }]) }
- it 'accepts a single package name as a string' do
- scope.function_ensure_packages(['foo'])
+ # this lambda is required due to strangeness within rspec-puppet's expectation handling
+ it { expect(lambda { catalogue }).to contain_package('puppet').with_ensure('absent').without_provider() }
+ it { expect(lambda { catalogue }).to contain_package('facter').with_ensure('present').with_provider("gem") }
end
end
- context 'given a catalog with puppet package => absent' do
- let :catalog do
- compile_to_catalog(<<-EOS
- ensure_packages(['facter'])
- package { puppet: ensure => absent }
- EOS
- )
- end
-
- it 'has no effect on Package[puppet]' do
- expect(catalog.resource(:package, 'puppet')['ensure']).to eq('absent')
- end
- end
+ context 'given hash of packages' do
+ before { subject.call([{"foo" => { "provider" => "rpm" }, "bar" => { "provider" => "gem" }}, { "ensure" => "present"}]) }
- context 'given a clean catalog' do
- let :catalog do
- compile_to_catalog('ensure_packages(["facter"])')
- end
-
- it 'declares package resources with ensure => present' do
- expect(catalog.resource(:package, 'facter')['ensure']).to eq('present')
- end
- end
-
- context 'given a clean catalog and specified defaults' do
- let :catalog do
- compile_to_catalog('ensure_packages(["facter"], {"provider" => "gem"})')
- end
-
- it 'declares package resources with ensure => present' do
- expect(catalog.resource(:package, 'facter')['ensure']).to eq('present')
- expect(catalog.resource(:package, 'facter')['provider']).to eq('gem')
- end
+ # this lambda is required due to strangeness within rspec-puppet's expectation handling
+ it { expect(lambda { catalogue }).to contain_package('foo').with({'provider' => 'rpm', 'ensure' => 'present'}) }
+ it { expect(lambda { catalogue }).to contain_package('bar').with({'provider' => 'gem', 'ensure' => 'present'}) }
end
end
diff --git a/spec/functions/ensure_resource_spec.rb b/spec/functions/ensure_resource_spec.rb
index 33bcac0..d552f4e 100755
--- a/spec/functions/ensure_resource_spec.rb
+++ b/spec/functions/ensure_resource_spec.rb
@@ -1,113 +1,93 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-require 'rspec-puppet'
-require 'puppet_spec/compiler'
describe 'ensure_resource' do
- include PuppetSpec::Compiler
-
- before :all do
- Puppet::Parser::Functions.autoloader.loadall
- Puppet::Parser::Functions.function(:ensure_packages)
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(ArgumentError, /Must specify a type/) }
+ it { is_expected.to run.with_params('type').and_raise_error(ArgumentError, /Must specify a title/) }
+ if Puppet.version.to_f >= 4.6
+ it { is_expected.to run.with_params('type', 'title', {}, 'extras').and_raise_error(ArgumentError) }
+ else
+ it { is_expected.to run.with_params('type', 'title', {}, 'extras').and_raise_error(Puppet::ParseError) }
end
- let :node do Puppet::Node.new('localhost') end
- let :compiler do Puppet::Parser::Compiler.new(node) end
- let :scope do Puppet::Parser::Scope.new(compiler) end
+ it {
+ pending("should not accept numbers as arguments")
+ is_expected.to run.with_params(1,2,3).and_raise_error(Puppet::ParseError)
+ }
- describe 'when a type or title is not specified' do
- it { expect { scope.function_ensure_resource([]) }.to raise_error }
- it { expect { scope.function_ensure_resource(['type']) }.to raise_error }
- end
+ context 'given an empty catalog' do
+ describe 'after running ensure_resource("user", "username1", {})' do
+ before { subject.call(['User', 'username1', {}]) }
- describe 'when compared against a resource with no attributes' do
- let :catalog do
- compile_to_catalog(<<-EOS
- user { "dan": }
- ensure_resource('user', 'dan', {})
- EOS
- )
+ # this lambda is required due to strangeness within rspec-puppet's expectation handling
+ it { expect(lambda { catalogue }).to contain_user('username1').without_ensure }
end
- it 'should contain the the ensured resources' do
- expect(catalog.resource(:user, 'dan').to_s).to eq('User[dan]')
+ describe 'after running ensure_resource("user", "username1", { gid => undef })' do
+ before { subject.call(['User', 'username1', { 'gid' => :undef }]) }
+
+ # this lambda is required due to strangeness within rspec-puppet's expectation handling
+ it { expect(lambda { catalogue }).to contain_user('username1').without_ensure }
+ it { expect(lambda { catalogue }).to contain_user('username1').without_gid }
end
- end
- describe 'works when compared against a resource with non-conflicting attributes' do
- [
- "ensure_resource('User', 'dan', {})",
- "ensure_resource('User', 'dan', '')",
- "ensure_resource('User', 'dan', {'ensure' => 'present'})",
- "ensure_resource('User', 'dan', {'ensure' => 'present', 'managehome' => false})"
- ].each do |ensure_resource|
- pp = <<-EOS
- user { "dan": ensure => present, shell => "/bin/csh", managehome => false}
- #{ensure_resource}
- EOS
-
- it { expect { compile_to_catalog(pp) }.to_not raise_error }
+ describe 'after running ensure_resource("user", "username1", { ensure => present, gid => undef })' do
+ before { subject.call(['User', 'username1', { 'ensure' => 'present', 'gid' => :undef }]) }
+
+ # this lambda is required due to strangeness within rspec-puppet's expectation handling
+ it { expect(lambda { catalogue }).to contain_user('username1').with_ensure('present') }
+ it { expect(lambda { catalogue }).to contain_user('username1').without_gid }
end
+
end
- describe 'fails when compared against a resource with conflicting attributes' do
- pp = <<-EOS
- user { "dan": ensure => present, shell => "/bin/csh", managehome => false}
- ensure_resource('User', 'dan', {'ensure' => 'absent', 'managehome' => false})
- EOS
+ context 'given a catalog with "user { username1: ensure => present }"' do
+ let(:pre_condition) { 'user { username1: ensure => present }' }
- it { expect { compile_to_catalog(pp) }.to raise_error }
- end
+ describe 'after running ensure_resource("user", "username1", {})' do
+ before { subject.call(['User', 'username1', {}]) }
- describe 'when an array of new resources are passed in' do
- let :catalog do
- compile_to_catalog("ensure_resource('User', ['dan', 'alex'], {})")
+ # this lambda is required due to strangeness within rspec-puppet's expectation handling
+ it { expect(lambda { catalogue }).to contain_user('username1').with_ensure('present') }
end
- it 'should contain the ensured resources' do
- expect(catalog.resource('User[dan]').to_s).to eq('User[dan]')
- expect(catalog.resource('User[alex]').to_s).to eq('User[alex]')
+ describe 'after running ensure_resource("user", "username2", {})' do
+ before { subject.call(['User', 'username2', {}]) }
+
+ # this lambda is required due to strangeness within rspec-puppet's expectation handling
+ it { expect(lambda { catalogue }).to contain_user('username1').with_ensure('present') }
+ it { expect(lambda { catalogue }).to contain_user('username2').without_ensure }
end
- end
- describe 'when an array of existing resources is compared against existing resources' do
- pp = <<-EOS
- user { 'dan': ensure => present; 'alex': ensure => present }
- ensure_resource('User', ['dan', 'alex'], {})
- EOS
+ describe 'after running ensure_resource("user", "username1", { gid => undef })' do
+ before { subject.call(['User', 'username1', { 'gid' => :undef }]) }
- let :catalog do
- compile_to_catalog(pp)
+ # this lambda is required due to strangeness within rspec-puppet's expectation handling
+ it { expect(lambda { catalogue }).to contain_user('username1').with_ensure('present') }
end
- it 'should return the existing resources' do
- expect(catalog.resource('User[dan]').to_s).to eq('User[dan]')
- expect(catalog.resource('User[alex]').to_s).to eq('User[alex]')
- end
- end
+ describe 'after running ensure_resource("user", ["username1", "username2"], {})' do
+ before { subject.call(['User', ['username1', 'username2'], {}]) }
- describe 'works when compared against existing resources with attributes' do
- [
- "ensure_resource('User', ['dan', 'alex'], {})",
- "ensure_resource('User', ['dan', 'alex'], '')",
- "ensure_resource('User', ['dan', 'alex'], {'ensure' => 'present'})",
- ].each do |ensure_resource|
- pp = <<-EOS
- user { 'dan': ensure => present; 'alex': ensure => present }
- #{ensure_resource}
- EOS
-
- it { expect { compile_to_catalog(pp) }.to_not raise_error }
+ # this lambda is required due to strangeness within rspec-puppet's expectation handling
+ it { expect(lambda { catalogue }).to contain_user('username1').with_ensure('present') }
+ it { expect(lambda { catalogue }).to contain_user('username2').without_ensure }
end
- end
- describe 'fails when compared against existing resources with conflicting attributes' do
- pp = <<-EOS
- user { 'dan': ensure => present; 'alex': ensure => present }
- ensure_resource('User', ['dan', 'alex'], {'ensure' => 'absent'})
- EOS
+ describe 'when providing already set params' do
+ let(:params) { { 'ensure' => 'present' } }
+ before { subject.call(['User', ['username2', 'username3'], params]) }
- it { expect { compile_to_catalog(pp) }.to raise_error }
- end
+ # this lambda is required due to strangeness within rspec-puppet's expectation handling
+ it { expect(lambda { catalogue }).to contain_user('username1').with(params) }
+ it { expect(lambda { catalogue }).to contain_user('username2').with(params) }
+ end
+ context 'when trying to add params' do
+ it { is_expected.to run \
+ .with_params('User', 'username1', { 'ensure' => 'present', 'shell' => true }) \
+ .and_raise_error(Puppet::Resource::Catalog::DuplicateResourceError, /User\[username1\] is already declared/)
+ }
+ end
+ end
end
diff --git a/spec/functions/flatten_spec.rb b/spec/functions/flatten_spec.rb
index de8c66d..a4338be 100755
--- a/spec/functions/flatten_spec.rb
+++ b/spec/functions/flatten_spec.rb
@@ -1,27 +1,14 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the flatten function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
- it "should exist" do
- expect(Puppet::Parser::Functions.function("flatten")).to eq("function_flatten")
- end
-
- it "should raise a ParseError if there is less than 1 arguments" do
- expect { scope.function_flatten([]) }.to( raise_error(Puppet::ParseError))
- end
-
- it "should raise a ParseError if there is more than 1 argument" do
- expect { scope.function_flatten([[], []]) }.to( raise_error(Puppet::ParseError))
- end
-
- it "should flatten a complex data structure" do
- result = scope.function_flatten([["a","b",["c",["d","e"],"f","g"]]])
- expect(result).to(eq(["a","b","c","d","e","f","g"]))
- end
-
- it "should do nothing to a structure that is already flat" do
- result = scope.function_flatten([["a","b","c","d"]])
- expect(result).to(eq(["a","b","c","d"]))
- end
+describe 'flatten' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params([], []).and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params([]).and_return([]) }
+ it { is_expected.to run.with_params(['one']).and_return(['one']) }
+ it { is_expected.to run.with_params([['one']]).and_return(['one']) }
+ it { is_expected.to run.with_params(["a","b","c","d","e","f","g"]).and_return(["a","b","c","d","e","f","g"]) }
+ it { is_expected.to run.with_params([["a","b",["c",["d","e"],"f","g"]]]).and_return(["a","b","c","d","e","f","g"]) }
end
diff --git a/spec/functions/floor_spec.rb b/spec/functions/floor_spec.rb
index 12a6917..608c602 100755
--- a/spec/functions/floor_spec.rb
+++ b/spec/functions/floor_spec.rb
@@ -1,39 +1,12 @@
-#! /usr/bin/env ruby -S rspec
-
require 'spec_helper'
-describe "the floor function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- it "should exist" do
- expect(Puppet::Parser::Functions.function("floor")).to eq("function_floor")
- end
-
- it "should raise a ParseError if there is less than 1 argument" do
- expect { scope.function_floor([]) }.to( raise_error(Puppet::ParseError, /Wrong number of arguments/))
- end
-
- it "should should raise a ParseError if input isn't numeric (eg. String)" do
- expect { scope.function_floor(["foo"]) }.to( raise_error(Puppet::ParseError, /Wrong argument type/))
- end
-
- it "should should raise a ParseError if input isn't numeric (eg. Boolean)" do
- expect { scope.function_floor([true]) }.to( raise_error(Puppet::ParseError, /Wrong argument type/))
- end
-
- it "should return an integer when a numeric type is passed" do
- result = scope.function_floor([12.4])
- expect(result.is_a?(Integer)).to(eq(true))
- end
-
- it "should return the input when an integer is passed" do
- result = scope.function_floor([7])
- expect(result).to(eq(7))
- end
-
- it "should return the largest integer less than or equal to the input" do
- result = scope.function_floor([3.8])
- expect(result).to(eq(3))
- end
+describe 'floor' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params("foo").and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params(34).and_return(34) }
+ it { is_expected.to run.with_params(-34).and_return(-34) }
+ it { is_expected.to run.with_params(33.1).and_return(33) }
+ it { is_expected.to run.with_params(-33.1).and_return(-34) }
end
-
diff --git a/spec/functions/fqdn_rand_string_spec.rb b/spec/functions/fqdn_rand_string_spec.rb
new file mode 100644
index 0000000..e407084
--- /dev/null
+++ b/spec/functions/fqdn_rand_string_spec.rb
@@ -0,0 +1,65 @@
+require 'spec_helper'
+
+describe 'fqdn_rand_string' do
+ let(:default_charset) { %r{\A[a-zA-Z0-9]{100}\z} }
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(ArgumentError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params(0).and_raise_error(ArgumentError, /first argument must be a positive integer/) }
+ it { is_expected.to run.with_params(1.5).and_raise_error(ArgumentError, /first argument must be a positive integer/) }
+ it { is_expected.to run.with_params(-10).and_raise_error(ArgumentError, /first argument must be a positive integer/) }
+ it { is_expected.to run.with_params("-10").and_raise_error(ArgumentError, /first argument must be a positive integer/) }
+ it { is_expected.to run.with_params("string").and_raise_error(ArgumentError, /first argument must be a positive integer/) }
+ it { is_expected.to run.with_params([]).and_raise_error(ArgumentError, /first argument must be a positive integer/) }
+ it { is_expected.to run.with_params({}).and_raise_error(ArgumentError, /first argument must be a positive integer/) }
+ it { is_expected.to run.with_params(1, 1).and_raise_error(ArgumentError, /second argument must be undef or a string/) }
+ it { is_expected.to run.with_params(1, []).and_raise_error(ArgumentError, /second argument must be undef or a string/) }
+ it { is_expected.to run.with_params(1, {}).and_raise_error(ArgumentError, /second argument must be undef or a string/) }
+ it { is_expected.to run.with_params(100).and_return(default_charset) }
+ it { is_expected.to run.with_params("100").and_return(default_charset) }
+ it { is_expected.to run.with_params(100, nil).and_return(default_charset) }
+ it { is_expected.to run.with_params(100, '').and_return(default_charset) }
+ it { is_expected.to run.with_params(100, 'a').and_return(/\Aa{100}\z/) }
+ it { is_expected.to run.with_params(100, 'ab').and_return(/\A[ab]{100}\z/) }
+
+ it "provides the same 'random' value on subsequent calls for the same host" do
+ expect(fqdn_rand_string(10)).to eql(fqdn_rand_string(10))
+ end
+
+ it "considers the same host and same extra arguments to have the same random sequence" do
+ first_random = fqdn_rand_string(10, :extra_identifier => [1, "same", "host"])
+ second_random = fqdn_rand_string(10, :extra_identifier => [1, "same", "host"])
+
+ expect(first_random).to eql(second_random)
+ end
+
+ it "allows extra arguments to control the random value on a single host" do
+ first_random = fqdn_rand_string(10, :extra_identifier => [1, "different", "host"])
+ second_different_random = fqdn_rand_string(10, :extra_identifier => [2, "different", "host"])
+
+ expect(first_random).not_to eql(second_different_random)
+ end
+
+ it "should return different strings for different hosts" do
+ val1 = fqdn_rand_string(10, :host => "first.host.com")
+ val2 = fqdn_rand_string(10, :host => "second.host.com")
+
+ expect(val1).not_to eql(val2)
+ end
+
+ def fqdn_rand_string(max, args = {})
+ host = args[:host] || '127.0.0.1'
+ charset = args[:charset]
+ extra = args[:extra_identifier] || []
+
+ # workaround not being able to use let(:facts) because some tests need
+ # multiple different hostnames in one context
+ scope.stubs(:lookupvar).with("::fqdn", {}).returns(host)
+
+ function_args = [max]
+ if args.has_key?(:charset) or !extra.empty?
+ function_args << charset
+ end
+ function_args += extra
+ scope.function_fqdn_rand_string(function_args)
+ end
+end
diff --git a/spec/functions/fqdn_rotate_spec.rb b/spec/functions/fqdn_rotate_spec.rb
index 40057d4..db7a717 100755
--- a/spec/functions/fqdn_rotate_spec.rb
+++ b/spec/functions/fqdn_rotate_spec.rb
@@ -1,43 +1,75 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the fqdn_rotate function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+describe 'fqdn_rotate' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params(0).and_raise_error(Puppet::ParseError, /Requires either array or string to work with/) }
+ it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, /Requires either array or string to work with/) }
+ it { is_expected.to run.with_params('').and_return('') }
+ it { is_expected.to run.with_params('a').and_return('a') }
- it "should exist" do
- expect(Puppet::Parser::Functions.function("fqdn_rotate")).to eq("function_fqdn_rotate")
+ it { is_expected.to run.with_params([]).and_return([]) }
+ it { is_expected.to run.with_params(['a']).and_return(['a']) }
+
+ it "should rotate a string and the result should be the same size" do
+ expect(fqdn_rotate("asdf").size).to eq(4)
end
- it "should raise a ParseError if there is less than 1 arguments" do
- expect { scope.function_fqdn_rotate([]) }.to( raise_error(Puppet::ParseError))
+ it "should rotate a string to give the same results for one host" do
+ val1 = fqdn_rotate("abcdefg", :host => 'one')
+ val2 = fqdn_rotate("abcdefg", :host => 'one')
+ expect(val1).to eq(val2)
end
- it "should rotate a string and the result should be the same size" do
- scope.expects(:lookupvar).with("::fqdn").returns("127.0.0.1")
- result = scope.function_fqdn_rotate(["asdf"])
- expect(result.size).to(eq(4))
+ it "allows extra arguments to control the random rotation on a single host" do
+ val1 = fqdn_rotate("abcdefg", :extra_identifier => [1, "different", "host"])
+ val2 = fqdn_rotate("abcdefg", :extra_identifier => [2, "different", "host"])
+ expect(val1).not_to eq(val2)
end
- it "should rotate a string to give the same results for one host" do
- scope.expects(:lookupvar).with("::fqdn").returns("127.0.0.1").twice
- expect(scope.function_fqdn_rotate(["abcdefg"])).to eql(scope.function_fqdn_rotate(["abcdefg"]))
+ it "considers the same host and same extra arguments to have the same random rotation" do
+ val1 = fqdn_rotate("abcdefg", :extra_identifier => [1, "same", "host"])
+ val2 = fqdn_rotate("abcdefg", :extra_identifier => [1, "same", "host"])
+ expect(val1).to eq(val2)
end
it "should rotate a string to give different values on different hosts" do
- scope.expects(:lookupvar).with("::fqdn").returns("127.0.0.1")
- val1 = scope.function_fqdn_rotate(["abcdefghijklmnopqrstuvwxyz01234567890987654321"])
- scope.expects(:lookupvar).with("::fqdn").returns("127.0.0.2")
- val2 = scope.function_fqdn_rotate(["abcdefghijklmnopqrstuvwxyz01234567890987654321"])
- expect(val1).not_to eql(val2)
+ val1 = fqdn_rotate("abcdefg", :host => 'one')
+ val2 = fqdn_rotate("abcdefg", :host => 'two')
+ expect(val1).not_to eq(val2)
end
it "should accept objects which extend String" do
- class AlsoString < String
+ result = fqdn_rotate(AlsoString.new('asdf'))
+ expect(result).to eq('dfas')
+ end
+
+ it "should use the Puppet::Util.deterministic_rand function" do
+ if Puppet::Util.respond_to?(:deterministic_rand)
+ Puppet::Util.expects(:deterministic_rand).with(44489829212339698569024999901561968770,4)
+ fqdn_rotate("asdf")
+ else
+ skip 'Puppet::Util#deterministic_rand not available'
end
+ end
+
+ it "should not leave the global seed in a deterministic state" do
+ fqdn_rotate("asdf")
+ rand1 = rand()
+ fqdn_rotate("asdf")
+ rand2 = rand()
+ expect(rand1).not_to eql(rand2)
+ end
+
+ def fqdn_rotate(value, args = {})
+ host = args[:host] || '127.0.0.1'
+ extra = args[:extra_identifier] || []
+
+ # workaround not being able to use let(:facts) because some tests need
+ # multiple different hostnames in one context
+ scope.stubs(:lookupvar).with("::fqdn").returns(host)
- scope.expects(:lookupvar).with("::fqdn").returns("127.0.0.1")
- value = AlsoString.new("asdf")
- result = scope.function_fqdn_rotate([value])
- result.size.should(eq(4))
+ function_args = [value] + extra
+ scope.function_fqdn_rotate(function_args)
end
end
diff --git a/spec/functions/fqdn_uuid_spec.rb b/spec/functions/fqdn_uuid_spec.rb
new file mode 100644
index 0000000..a2d1618
--- /dev/null
+++ b/spec/functions/fqdn_uuid_spec.rb
@@ -0,0 +1,14 @@
+require 'spec_helper'
+
+describe 'fqdn_uuid' do
+
+ context "Invalid parameters" do
+ it { should run.with_params().and_raise_error(ArgumentError, /No arguments given$/) }
+ end
+
+ context "given string" do
+ it { should run.with_params('puppetlabs.com').and_return('9c70320f-6815-5fc5-ab0f-debe68bf764c') }
+ it { should run.with_params('google.com').and_return('64ee70a4-8cc1-5d25-abf2-dea6c79a09c8') }
+ end
+
+end
diff --git a/spec/functions/get_module_path_spec.rb b/spec/functions/get_module_path_spec.rb
index 38ce645..a39e413 100755
--- a/spec/functions/get_module_path_spec.rb
+++ b/spec/functions/get_module_path_spec.rb
@@ -1,8 +1,12 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe Puppet::Parser::Functions.function(:get_module_path) do
- Internals = PuppetlabsSpec::PuppetInternals
+describe 'get_module_path' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /Wrong number of arguments, expects one/) }
+ it { is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError, /Wrong number of arguments, expects one/) }
+ it { is_expected.to run.with_params('one', 'two', 'three').and_raise_error(Puppet::ParseError, /Wrong number of arguments, expects one/) }
+ it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, /Could not find module/) }
+
class StubModule
attr_reader :path
def initialize(path)
@@ -10,37 +14,35 @@ describe Puppet::Parser::Functions.function(:get_module_path) do
end
end
- def scope(environment = "production")
- Internals.scope(:compiler => Internals.compiler(:node => Internals.node(:environment => environment)))
- end
-
- it 'should only allow one argument' do
- expect { scope.function_get_module_path([]) }.to raise_error(Puppet::ParseError, /Wrong number of arguments, expects one/)
- expect { scope.function_get_module_path(['1','2','3']) }.to raise_error(Puppet::ParseError, /Wrong number of arguments, expects one/)
- end
- it 'should raise an exception when the module cannot be found' do
- expect { scope.function_get_module_path(['foo']) }.to raise_error(Puppet::ParseError, /Could not find module/)
- end
describe 'when locating a module' do
let(:modulepath) { "/tmp/does_not_exist" }
let(:path_of_module_foo) { StubModule.new("/tmp/does_not_exist/foo") }
before(:each) { Puppet[:modulepath] = modulepath }
- it 'should be able to find module paths from the modulepath setting' do
- Puppet::Module.expects(:find).with('foo', 'production').returns(path_of_module_foo)
- expect(scope.function_get_module_path(['foo'])).to eq(path_of_module_foo.path)
- end
- it 'should be able to find module paths when the modulepath is a list' do
- Puppet[:modulepath] = modulepath + ":/tmp"
- Puppet::Module.expects(:find).with('foo', 'production').returns(path_of_module_foo)
- expect(scope.function_get_module_path(['foo'])).to eq(path_of_module_foo.path)
+ context 'in the default environment' do
+ before(:each) { Puppet::Module.expects(:find).with('foo', 'rp_env').returns(path_of_module_foo) }
+
+ it { is_expected.to run.with_params('foo').and_return(path_of_module_foo.path) }
+
+ context 'when the modulepath is a list' do
+ before(:each) { Puppet[:modulepath] = modulepath + 'tmp/something_else' }
+
+ it { is_expected.to run.with_params('foo').and_return(path_of_module_foo.path) }
+ end
end
- it 'should respect the environment' do
- skip("Disabled on Puppet 2.6.x") if Puppet.version =~ /^2\.6\b/
- Puppet.settings[:environment] = 'danstestenv'
- Puppet::Module.expects(:find).with('foo', 'danstestenv').returns(path_of_module_foo)
- expect(scope('danstestenv').function_get_module_path(['foo'])).to eq(path_of_module_foo.path)
+
+ context 'in a non-default default environment' do
+ let(:environment) { 'test' }
+ before(:each) { Puppet::Module.expects(:find).with('foo', 'test').returns(path_of_module_foo) }
+
+ it { is_expected.to run.with_params('foo').and_return(path_of_module_foo.path) }
+
+ context 'when the modulepath is a list' do
+ before(:each) { Puppet[:modulepath] = modulepath + 'tmp/something_else' }
+
+ it { is_expected.to run.with_params('foo').and_return(path_of_module_foo.path) }
+ end
end
end
end
diff --git a/spec/functions/getparam_spec.rb b/spec/functions/getparam_spec.rb
index 833c4d4..e4ef9e6 100755
--- a/spec/functions/getparam_spec.rb
+++ b/spec/functions/getparam_spec.rb
@@ -1,76 +1,27 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-require 'rspec-puppet'
-require 'puppet_spec/compiler'
describe 'getparam' do
- include PuppetSpec::Compiler
-
- before :each do
- Puppet::Parser::Functions.autoloader.loadall
- Puppet::Parser::Functions.function(:getparam)
- end
-
- let :node do Puppet::Node.new('localhost') end
- let :compiler do Puppet::Parser::Compiler.new(node) end
- if Puppet.version.to_f >= 3.0
- let :scope do Puppet::Parser::Scope.new(compiler) end
- else
- let :scope do
- newscope = Puppet::Parser::Scope.new
- newscope.compiler = compiler
- newscope.source = Puppet::Resource::Type.new(:node, :localhost)
- newscope
- end
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(ArgumentError, /Must specify a reference/) }
+ it { is_expected.to run.with_params('User[one]').and_raise_error(ArgumentError, /Must specify name of a parameter/) }
+ it { is_expected.to run.with_params('User[one]', 2).and_raise_error(ArgumentError, /Must specify name of a parameter/) }
+ it { is_expected.to run.with_params('User[one]', []).and_raise_error(ArgumentError, /Must specify name of a parameter/) }
+ it { is_expected.to run.with_params('User[one]', {}).and_raise_error(ArgumentError, /Must specify name of a parameter/) }
+
+ describe 'when compared against a user resource with no params' do
+ let(:pre_condition) { 'user { "one": }' }
+
+ it { is_expected.to run.with_params('User[one]', 'ensure').and_return('') }
+ it { is_expected.to run.with_params('User[two]', 'ensure').and_return('') }
+ it { is_expected.to run.with_params('User[one]', 'shell').and_return('') }
end
- it "should exist" do
- expect(Puppet::Parser::Functions.function("getparam")).to eq("function_getparam")
- end
-
- describe 'when a resource is not specified' do
- it { expect { scope.function_getparam([]) }.to raise_error }
- it { expect { scope.function_getparam(['User[dan]']) }.to raise_error }
- it { expect { scope.function_getparam(['User[dan]']) }.to raise_error }
- it { expect { scope.function_getparam(['User[dan]', {}]) }.to raise_error }
- # This seems to be OK because we just check for a string.
- it { expect { scope.function_getparam(['User[dan]', '']) }.to_not raise_error }
- end
-
- describe 'when compared against a resource with no params' do
- let :catalog do
- compile_to_catalog(<<-EOS
- user { "dan": }
- EOS
- )
- end
-
- it do
- expect(scope.function_getparam(['User[dan]', 'shell'])).to eq('')
- end
- end
-
- describe 'when compared against a resource with params' do
- let :catalog do
- compile_to_catalog(<<-EOS
- user { 'dan': ensure => present, shell => '/bin/sh', managehome => false}
- $test = getparam(User[dan], 'shell')
- EOS
- )
- end
-
- it do
- resource = Puppet::Parser::Resource.new(:user, 'dan', {:scope => scope})
- resource.set_parameter('ensure', 'present')
- resource.set_parameter('shell', '/bin/sh')
- resource.set_parameter('managehome', false)
- compiler.add_resource(scope, resource)
+ describe 'when compared against a user resource with params' do
+ let(:pre_condition) { 'user { "one": ensure => present, shell => "/bin/sh", managehome => false, }' }
- expect(scope.function_getparam(['User[dan]', 'shell'])).to eq('/bin/sh')
- expect(scope.function_getparam(['User[dan]', ''])).to eq('')
- expect(scope.function_getparam(['User[dan]', 'ensure'])).to eq('present')
- # TODO: Expected this to be false, figure out why we're getting '' back.
- expect(scope.function_getparam(['User[dan]', 'managehome'])).to eq('')
- end
+ it { is_expected.to run.with_params('User[one]', 'ensure').and_return('present') }
+ it { is_expected.to run.with_params('User[two]', 'ensure').and_return('') }
+ it { is_expected.to run.with_params('User[one]', 'shell').and_return('/bin/sh') }
+ it { is_expected.to run.with_params('User[one]', 'managehome').and_return(false) }
end
end
diff --git a/spec/functions/getvar_spec.rb b/spec/functions/getvar_spec.rb
index 87ab9b5..54f1842 100755
--- a/spec/functions/getvar_spec.rb
+++ b/spec/functions/getvar_spec.rb
@@ -1,37 +1,23 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe Puppet::Parser::Functions.function(:getvar) do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
- describe 'when calling getvar from puppet' do
+describe 'getvar' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
- it "should not compile when no arguments are passed" do
- skip("Fails on 2.6.x, see bug #15912") if Puppet.version =~ /^2\.6\./
- Puppet[:code] = '$foo = getvar()'
- expect {
- scope.compiler.compile
- }.to raise_error(Puppet::ParseError, /wrong number of arguments/)
- end
+ it { is_expected.to run.with_params('$::foo').and_return(nil) }
- it "should not compile when too many arguments are passed" do
- skip("Fails on 2.6.x, see bug #15912") if Puppet.version =~ /^2\.6\./
- Puppet[:code] = '$foo = getvar("foo::bar", "baz")'
- expect {
- scope.compiler.compile
- }.to raise_error(Puppet::ParseError, /wrong number of arguments/)
- end
-
- it "should lookup variables in other namespaces" do
- skip("Fails on 2.6.x, see bug #15912") if Puppet.version =~ /^2\.6\./
- Puppet[:code] = <<-'ENDofPUPPETcode'
- class site::data { $foo = 'baz' }
- include site::data
- $foo = getvar("site::data::foo")
- if $foo != 'baz' {
- fail('getvar did not return what we expect')
- }
+ context 'given variables in namespaces' do
+ let(:pre_condition) {
+ <<-'ENDofPUPPETcode'
+ class site::data { $foo = 'baz' }
+ include site::data
ENDofPUPPETcode
- scope.compiler.compile
- end
+ }
+
+ it { is_expected.to run.with_params('site::data::foo').and_return('baz') }
+ it { is_expected.to run.with_params('::site::data::foo').and_return('baz') }
+ it { is_expected.to run.with_params('::site::data::bar').and_return(nil) }
end
end
diff --git a/spec/functions/grep_spec.rb b/spec/functions/grep_spec.rb
index 9c671dd..6e0bd6e 100755
--- a/spec/functions/grep_spec.rb
+++ b/spec/functions/grep_spec.rb
@@ -1,19 +1,19 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the grep function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- it "should exist" do
- expect(Puppet::Parser::Functions.function("grep")).to eq("function_grep")
- end
-
- it "should raise a ParseError if there is less than 1 arguments" do
- expect { scope.function_grep([]) }.to( raise_error(Puppet::ParseError))
- end
-
- it "should grep contents from an array" do
- result = scope.function_grep([["aaabbb","bbbccc","dddeee"], "bbb"])
- expect(result).to(eq(["aaabbb","bbbccc"]))
- end
+describe 'grep' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it {
+ pending("grep does not actually check this, and raises NoMethodError instead")
+ is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError, /first argument not an array/)
+ }
+ it {
+ pending("grep does not actually check this, and raises NoMethodError instead")
+ is_expected.to run.with_params(1, 'two').and_raise_error(Puppet::ParseError, /first argument not an array/)
+ }
+ it { is_expected.to run.with_params('one', 'two', 'three').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params([], 'two').and_return([]) }
+ it { is_expected.to run.with_params(['one', 'two', 'three'], 'two').and_return(['two']) }
+ it { is_expected.to run.with_params(['one', 'two', 'three'], 't(wo|hree)').and_return(['two', 'three']) }
end
diff --git a/spec/functions/has_interface_with_spec.rb b/spec/functions/has_interface_with_spec.rb
index 23e09a9..7334d38 100755
--- a/spec/functions/has_interface_with_spec.rb
+++ b/spec/functions/has_interface_with_spec.rb
@@ -1,64 +1,38 @@
-#!/usr/bin/env ruby -S rspec
require 'spec_helper'
-describe Puppet::Parser::Functions.function(:has_interface_with) do
-
- let(:scope) do
- PuppetlabsSpec::PuppetInternals.scope
- end
-
- # The subject of these examples is the method itself.
- subject do
- function_name = Puppet::Parser::Functions.function(:has_interface_with)
- scope.method(function_name)
- end
+describe 'has_interface_with' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params("one", "two", "three").and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
# We need to mock out the Facts so we can specify how we expect this function
# to behave on different platforms.
context "On Mac OS X Systems" do
- before :each do
- scope.stubs(:lookupvar).with("interfaces").returns('lo0,gif0,stf0,en1,p2p0,fw0,en0,vmnet1,vmnet8,utun0')
- end
- it 'should have loopback (lo0)' do
- expect(subject.call(['lo0'])).to be_truthy
- end
- it 'should not have loopback (lo)' do
- expect(subject.call(['lo'])).to be_falsey
- end
+ let(:facts) { { :interfaces => 'lo0,gif0,stf0,en1,p2p0,fw0,en0,vmnet1,vmnet8,utun0' } }
+ it { is_expected.to run.with_params('lo0').and_return(true) }
+ it { is_expected.to run.with_params('lo').and_return(false) }
end
+
context "On Linux Systems" do
- before :each do
- scope.stubs(:lookupvar).with("interfaces").returns('eth0,lo')
- scope.stubs(:lookupvar).with("ipaddress").returns('10.0.0.1')
- scope.stubs(:lookupvar).with("ipaddress_lo").returns('127.0.0.1')
- scope.stubs(:lookupvar).with("ipaddress_eth0").returns('10.0.0.1')
- scope.stubs(:lookupvar).with('muppet').returns('kermit')
- scope.stubs(:lookupvar).with('muppet_lo').returns('mspiggy')
- scope.stubs(:lookupvar).with('muppet_eth0').returns('kermit')
- end
- it 'should have loopback (lo)' do
- expect(subject.call(['lo'])).to be_truthy
- end
- it 'should not have loopback (lo0)' do
- expect(subject.call(['lo0'])).to be_falsey
- end
- it 'should have ipaddress with 127.0.0.1' do
- expect(subject.call(['ipaddress', '127.0.0.1'])).to be_truthy
- end
- it 'should have ipaddress with 10.0.0.1' do
- expect(subject.call(['ipaddress', '10.0.0.1'])).to be_truthy
- end
- it 'should not have ipaddress with 10.0.0.2' do
- expect(subject.call(['ipaddress', '10.0.0.2'])).to be_falsey
- end
- it 'should have muppet named kermit' do
- expect(subject.call(['muppet', 'kermit'])).to be_truthy
- end
- it 'should have muppet named mspiggy' do
- expect(subject.call(['muppet', 'mspiggy'])).to be_truthy
- end
- it 'should not have muppet named bigbird' do
- expect(subject.call(['muppet', 'bigbird'])).to be_falsey
+ let(:facts) do
+ {
+ :interfaces => 'eth0,lo',
+ :ipaddress => '10.0.0.1',
+ :ipaddress_lo => '127.0.0.1',
+ :ipaddress_eth0 => '10.0.0.1',
+ :muppet => 'kermit',
+ :muppet_lo => 'mspiggy',
+ :muppet_eth0 => 'kermit',
+ }
end
+
+ it { is_expected.to run.with_params('lo').and_return(true) }
+ it { is_expected.to run.with_params('lo0').and_return(false) }
+ it { is_expected.to run.with_params('ipaddress', '127.0.0.1').and_return(true) }
+ it { is_expected.to run.with_params('ipaddress', '10.0.0.1').and_return(true) }
+ it { is_expected.to run.with_params('ipaddress', '8.8.8.8').and_return(false) }
+ it { is_expected.to run.with_params('muppet', 'kermit').and_return(true) }
+ it { is_expected.to run.with_params('muppet', 'mspiggy').and_return(true) }
+ it { is_expected.to run.with_params('muppet', 'bigbird').and_return(false) }
end
end
diff --git a/spec/functions/has_ip_address_spec.rb b/spec/functions/has_ip_address_spec.rb
index 0df12a7..42a5a79 100755
--- a/spec/functions/has_ip_address_spec.rb
+++ b/spec/functions/has_ip_address_spec.rb
@@ -1,39 +1,22 @@
-#!/usr/bin/env ruby -S rspec
require 'spec_helper'
-describe Puppet::Parser::Functions.function(:has_ip_address) do
-
- let(:scope) do
- PuppetlabsSpec::PuppetInternals.scope
- end
-
- subject do
- function_name = Puppet::Parser::Functions.function(:has_ip_address)
- scope.method(function_name)
- end
+describe 'has_ip_address' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params("one", "two").and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
context "On Linux Systems" do
- before :each do
- scope.stubs(:lookupvar).with('interfaces').returns('eth0,lo')
- scope.stubs(:lookupvar).with('ipaddress').returns('10.0.2.15')
- scope.stubs(:lookupvar).with('ipaddress_eth0').returns('10.0.2.15')
- scope.stubs(:lookupvar).with('ipaddress_lo').returns('127.0.0.1')
- end
-
- it 'should have primary address (10.0.2.15)' do
- expect(subject.call(['10.0.2.15'])).to be_truthy
+ let(:facts) do
+ {
+ :interfaces => 'eth0,lo',
+ :ipaddress => '10.0.0.1',
+ :ipaddress_lo => '127.0.0.1',
+ :ipaddress_eth0 => '10.0.0.1',
+ }
end
- it 'should have lookupback address (127.0.0.1)' do
- expect(subject.call(['127.0.0.1'])).to be_truthy
- end
-
- it 'should not have other address' do
- expect(subject.call(['192.1681.1.1'])).to be_falsey
- end
-
- it 'should not have "mspiggy" on an interface' do
- expect(subject.call(['mspiggy'])).to be_falsey
- end
+ it { is_expected.to run.with_params('127.0.0.1').and_return(true) }
+ it { is_expected.to run.with_params('10.0.0.1').and_return(true) }
+ it { is_expected.to run.with_params('8.8.8.8').and_return(false) }
end
end
diff --git a/spec/functions/has_ip_network_spec.rb b/spec/functions/has_ip_network_spec.rb
index 2a2578e..7b5fe66 100755
--- a/spec/functions/has_ip_network_spec.rb
+++ b/spec/functions/has_ip_network_spec.rb
@@ -1,36 +1,22 @@
-#!/usr/bin/env ruby -S rspec
require 'spec_helper'
-describe Puppet::Parser::Functions.function(:has_ip_network) do
-
- let(:scope) do
- PuppetlabsSpec::PuppetInternals.scope
- end
-
- subject do
- function_name = Puppet::Parser::Functions.function(:has_ip_network)
- scope.method(function_name)
- end
+describe 'has_ip_network' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params("one", "two").and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
context "On Linux Systems" do
- before :each do
- scope.stubs(:lookupvar).with('interfaces').returns('eth0,lo')
- scope.stubs(:lookupvar).with('network').returns(:undefined)
- scope.stubs(:lookupvar).with('network_eth0').returns('10.0.2.0')
- scope.stubs(:lookupvar).with('network_lo').returns('127.0.0.1')
- end
-
- it 'should have primary network (10.0.2.0)' do
- expect(subject.call(['10.0.2.0'])).to be_truthy
- end
-
- it 'should have loopback network (127.0.0.0)' do
- expect(subject.call(['127.0.0.1'])).to be_truthy
+ let(:facts) do
+ {
+ :interfaces => 'eth0,lo',
+ :network => :undefined,
+ :network_lo => '127.0.0.0',
+ :network_eth0 => '10.0.0.0',
+ }
end
- it 'should not have other network' do
- expect(subject.call(['192.168.1.0'])).to be_falsey
- end
+ it { is_expected.to run.with_params('127.0.0.0').and_return(true) }
+ it { is_expected.to run.with_params('10.0.0.0').and_return(true) }
+ it { is_expected.to run.with_params('8.8.8.0').and_return(false) }
end
end
-
diff --git a/spec/functions/has_key_spec.rb b/spec/functions/has_key_spec.rb
index 6b71800..965d5a6 100755
--- a/spec/functions/has_key_spec.rb
+++ b/spec/functions/has_key_spec.rb
@@ -1,42 +1,15 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe Puppet::Parser::Functions.function(:has_key) do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- describe 'when calling has_key from puppet' do
- it "should not compile when no arguments are passed" do
- skip("Fails on 2.6.x, see bug #15912") if Puppet.version =~ /^2\.6\./
- Puppet[:code] = '$x = has_key()'
- expect {
- scope.compiler.compile
- }.to raise_error(Puppet::ParseError, /wrong number of arguments/)
- end
-
- it "should not compile when 1 argument is passed" do
- skip("Fails on 2.6.x, see bug #15912") if Puppet.version =~ /^2\.6\./
- Puppet[:code] = "$x = has_key('foo')"
- expect {
- scope.compiler.compile
- }.to raise_error(Puppet::ParseError, /wrong number of arguments/)
- end
-
- it "should require the first value to be a Hash" do
- skip("Fails on 2.6.x, see bug #15912") if Puppet.version =~ /^2\.6\./
- Puppet[:code] = "$x = has_key('foo', 'bar')"
- expect {
- scope.compiler.compile
- }.to raise_error(Puppet::ParseError, /expects the first argument to be a hash/)
- end
- end
-
- describe 'when calling the function has_key from a scope instance' do
- it 'should detect existing keys' do
- expect(scope.function_has_key([{'one' => 1}, 'one'])).to be_truthy
- end
-
- it 'should detect existing keys' do
- expect(scope.function_has_key([{'one' => 1}, 'two'])).to be_falsey
- end
- end
+describe 'has_key' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params("one").and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params("one", "two", "three").and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params("one", "two").and_raise_error(Puppet::ParseError, /expects the first argument to be a hash/) }
+ it { is_expected.to run.with_params(1, "two").and_raise_error(Puppet::ParseError, /expects the first argument to be a hash/) }
+ it { is_expected.to run.with_params([], "two").and_raise_error(Puppet::ParseError, /expects the first argument to be a hash/) }
+
+ it { is_expected.to run.with_params({ 'key' => 'value' }, "key").and_return(true) }
+ it { is_expected.to run.with_params({}, "key").and_return(false) }
+ it { is_expected.to run.with_params({ 'key' => 'value'}, "not a key").and_return(false) }
end
diff --git a/spec/functions/hash_spec.rb b/spec/functions/hash_spec.rb
index ec2988b..4fe99ce 100755
--- a/spec/functions/hash_spec.rb
+++ b/spec/functions/hash_spec.rb
@@ -1,19 +1,14 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the hash function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- it "should exist" do
- expect(Puppet::Parser::Functions.function("hash")).to eq("function_hash")
- end
-
- it "should raise a ParseError if there is less than 1 arguments" do
- expect { scope.function_hash([]) }.to( raise_error(Puppet::ParseError))
- end
-
- it "should convert an array to a hash" do
- result = scope.function_hash([['a',1,'b',2,'c',3]])
- expect(result).to(eq({'a'=>1,'b'=>2,'c'=>3}))
- end
+describe 'hash' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it {
+ pending("Current implementation ignores parameters after the first.")
+ is_expected.to run.with_params([], 'two').and_raise_error(Puppet::ParseError, /wrong number of arguments/i)
+ }
+ it { is_expected.to run.with_params(['one']).and_raise_error(Puppet::ParseError, /Unable to compute/) }
+ it { is_expected.to run.with_params([]).and_return({}) }
+ it { is_expected.to run.with_params(['key1', 'value1']).and_return({ 'key1' => 'value1' }) }
+ it { is_expected.to run.with_params(['key1', 'value1', 'key2', 'value2']).and_return({ 'key1' => 'value1', 'key2' => 'value2' }) }
end
diff --git a/spec/functions/intersection_spec.rb b/spec/functions/intersection_spec.rb
index 6361304..c0f6086 100755
--- a/spec/functions/intersection_spec.rb
+++ b/spec/functions/intersection_spec.rb
@@ -1,19 +1,21 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the intersection function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- it "should exist" do
- expect(Puppet::Parser::Functions.function("intersection")).to eq("function_intersection")
- end
-
- it "should raise a ParseError if there are fewer than 2 arguments" do
- expect { scope.function_intersection([]) }.to( raise_error(Puppet::ParseError) )
- end
-
- it "should return the intersection of two arrays" do
- result = scope.function_intersection([["a","b","c"],["b","c","d"]])
- expect(result).to(eq(["b","c"]))
- end
+describe 'intersection' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params('one', 'two', 'three').and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params('one', []).and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params([], 'two').and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params({}, {}).and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params([], []).and_return([]) }
+ it { is_expected.to run.with_params([], ['one']).and_return([]) }
+ it { is_expected.to run.with_params(['one'], []).and_return([]) }
+ it { is_expected.to run.with_params(['one'], ['one']).and_return(['one']) }
+ it { is_expected.to run.with_params(['one', 'two', 'three'], ['two', 'three']).and_return(['two', 'three']) }
+ it { is_expected.to run.with_params(['one', 'two', 'two', 'three'], ['two', 'three']).and_return(['two', 'three']) }
+ it { is_expected.to run.with_params(['one', 'two', 'three'], ['two', 'two', 'three']).and_return(['two', 'three']) }
+ it { is_expected.to run.with_params(['one', 'two', 'three'], ['two', 'three', 'four']).and_return(['two', 'three']) }
+ it 'should not confuse types' do is_expected.to run.with_params(['1', '2', '3'], [1, 2]).and_return([]) end
end
diff --git a/spec/functions/is_a_spec.rb b/spec/functions/is_a_spec.rb
new file mode 100644
index 0000000..8dec13f
--- /dev/null
+++ b/spec/functions/is_a_spec.rb
@@ -0,0 +1,25 @@
+require 'spec_helper'
+
+if ENV["FUTURE_PARSER"] == 'yes'
+ describe 'type_of' do
+ pending 'teach rspec-puppet to load future-only functions under 3.7.5' do
+ it { is_expected.not_to eq(nil) }
+ end
+ end
+end
+
+if Puppet.version.to_f >= 4.0
+ describe 'is_a' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(ArgumentError) }
+ it { is_expected.to run.with_params('', '').and_raise_error(ArgumentError) }
+
+ it 'succeeds when comparing a string and a string' do
+ is_expected.to run.with_params('hello world', String).and_return(true)
+ end
+
+ it 'fails when comparing an integer and a string' do
+ is_expected.to run.with_params(5, String).and_return(false)
+ end
+ end
+end
diff --git a/spec/functions/is_array_spec.rb b/spec/functions/is_array_spec.rb
index 94920a4..e89f54b 100755
--- a/spec/functions/is_array_spec.rb
+++ b/spec/functions/is_array_spec.rb
@@ -1,29 +1,36 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the is_array function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- it "should exist" do
- expect(Puppet::Parser::Functions.function("is_array")).to eq("function_is_array")
- end
-
- it "should raise a ParseError if there is less than 1 arguments" do
- expect { scope.function_is_array([]) }.to( raise_error(Puppet::ParseError))
- end
-
- it "should return true if passed an array" do
- result = scope.function_is_array([[1,2,3]])
- expect(result).to(eq(true))
- end
-
- it "should return false if passed a hash" do
- result = scope.function_is_array([{'a'=>1}])
- expect(result).to(eq(false))
- end
-
- it "should return false if passed a string" do
- result = scope.function_is_array(["asdf"])
- expect(result).to(eq(false))
+describe 'is_array' do
+
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it {
+ pending("Current implementation ignores parameters after the first.")
+ is_expected.to run.with_params([], []).and_raise_error(Puppet::ParseError, /wrong number of arguments/i)
+ }
+ it { is_expected.to run.with_params([]).and_return(true) }
+ it { is_expected.to run.with_params(['one']).and_return(true) }
+ it { is_expected.to run.with_params([1]).and_return(true) }
+ it { is_expected.to run.with_params([{}]).and_return(true) }
+ it { is_expected.to run.with_params([[]]).and_return(true) }
+ it { is_expected.to run.with_params('').and_return(false) }
+ it { is_expected.to run.with_params('one').and_return(false) }
+ it { is_expected.to run.with_params(1).and_return(false) }
+ it { is_expected.to run.with_params({}).and_return(false) }
+ context 'Checking for deprecation warning' do
+ after(:all) do
+ ENV.delete('STDLIB_LOG_DEPRECATIONS')
+ end
+ # Checking for deprecation warning, which should only be provoked when the env variable for it is set.
+ it 'should display a single deprecation' do
+ ENV['STDLIB_LOG_DEPRECATIONS'] = "true"
+ scope.expects(:warning).with(includes('This method is deprecated'))
+ is_expected.to run.with_params(['1.2.3.4']).and_return(true)
+ end
+ it 'should display no warning for deprecation' do
+ ENV['STDLIB_LOG_DEPRECATIONS'] = "false"
+ scope.expects(:warning).with(includes('This method is deprecated')).never
+ is_expected.to run.with_params(['1.2.3.4']).and_return(true)
+ end
end
end
diff --git a/spec/functions/is_bool_spec.rb b/spec/functions/is_bool_spec.rb
index 4a342ba..d21345c 100755
--- a/spec/functions/is_bool_spec.rb
+++ b/spec/functions/is_bool_spec.rb
@@ -1,44 +1,33 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the is_bool function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- it "should exist" do
- expect(Puppet::Parser::Functions.function("is_bool")).to eq("function_is_bool")
- end
-
- it "should raise a ParseError if there is less than 1 arguments" do
- expect { scope.function_is_bool([]) }.to( raise_error(Puppet::ParseError))
- end
-
- it "should return true if passed a TrueClass" do
- result = scope.function_is_bool([true])
- expect(result).to(eq(true))
- end
-
- it "should return true if passed a FalseClass" do
- result = scope.function_is_bool([false])
- expect(result).to(eq(true))
- end
-
- it "should return false if passed the string 'true'" do
- result = scope.function_is_bool(['true'])
- expect(result).to(eq(false))
- end
-
- it "should return false if passed the string 'false'" do
- result = scope.function_is_bool(['false'])
- expect(result).to(eq(false))
- end
-
- it "should return false if passed an array" do
- result = scope.function_is_bool([["a","b"]])
- expect(result).to(eq(false))
- end
-
- it "should return false if passed a hash" do
- result = scope.function_is_bool([{"a" => "b"}])
- expect(result).to(eq(false))
- end
+describe 'is_bool' do
+
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params(true, false).and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params(true).and_return(true) }
+ it { is_expected.to run.with_params(false).and_return(true) }
+ it { is_expected.to run.with_params([1]).and_return(false) }
+ it { is_expected.to run.with_params([{}]).and_return(false) }
+ it { is_expected.to run.with_params([[]]).and_return(false) }
+ it { is_expected.to run.with_params([true]).and_return(false) }
+ it { is_expected.to run.with_params('true').and_return(false) }
+ it { is_expected.to run.with_params('false').and_return(false) }
+ context 'Checking for deprecation warning' do
+ after(:all) do
+ ENV.delete('STDLIB_LOG_DEPRECATIONS')
+ end
+ # Checking for deprecation warning, which should only be provoked when the env variable for it is set.
+ it 'should display a single deprecation' do
+ ENV['STDLIB_LOG_DEPRECATIONS'] = "true"
+ scope.expects(:warning).with(includes('This method is deprecated'))
+ is_expected.to run.with_params(true).and_return(true)
+ end
+ it 'should display no warning for deprecation' do
+ ENV['STDLIB_LOG_DEPRECATIONS'] = "false"
+ scope.expects(:warning).with(includes('This method is deprecated')).never
+ is_expected.to run.with_params(false).and_return(true)
+ end
+ end
end
+
diff --git a/spec/functions/is_domain_name_spec.rb b/spec/functions/is_domain_name_spec.rb
index 4d05f5c..c1bf0e3 100755
--- a/spec/functions/is_domain_name_spec.rb
+++ b/spec/functions/is_domain_name_spec.rb
@@ -1,64 +1,43 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the is_domain_name function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- it "should exist" do
- expect(Puppet::Parser::Functions.function("is_domain_name")).to eq("function_is_domain_name")
- end
-
- it "should raise a ParseError if there is less than 1 arguments" do
- expect { scope.function_is_domain_name([]) }.to( raise_error(Puppet::ParseError))
- end
-
- it "should return true if a valid short domain name" do
- result = scope.function_is_domain_name(["x.com"])
- expect(result).to(be_truthy)
- end
-
- it "should return true if the domain is ." do
- result = scope.function_is_domain_name(["."])
- expect(result).to(be_truthy)
- end
-
- it "should return true if the domain is x.com." do
- result = scope.function_is_domain_name(["x.com."])
- expect(result).to(be_truthy)
- end
-
- it "should return true if a valid domain name" do
- result = scope.function_is_domain_name(["foo.bar.com"])
- expect(result).to(be_truthy)
- end
-
- it "should allow domain parts to start with numbers" do
- result = scope.function_is_domain_name(["3foo.2bar.com"])
- expect(result).to(be_truthy)
- end
-
- it "should allow domain to end with a dot" do
- result = scope.function_is_domain_name(["3foo.2bar.com."])
- expect(result).to(be_truthy)
- end
-
- it "should allow a single part domain" do
- result = scope.function_is_domain_name(["orange"])
- expect(result).to(be_truthy)
- end
-
- it "should return false if domain parts start with hyphens" do
- result = scope.function_is_domain_name(["-3foo.2bar.com"])
- expect(result).to(be_falsey)
- end
-
- it "should return true if domain contains hyphens" do
- result = scope.function_is_domain_name(["3foo-bar.2bar-fuzz.com"])
- expect(result).to(be_truthy)
- end
-
- it "should return false if domain name contains spaces" do
- result = scope.function_is_domain_name(["not valid"])
- expect(result).to(be_falsey)
+describe 'is_domain_name' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params(1).and_return(false) }
+ it { is_expected.to run.with_params([]).and_return(false) }
+ it { is_expected.to run.with_params({}).and_return(false) }
+ it { is_expected.to run.with_params('').and_return(false) }
+ it { is_expected.to run.with_params('.').and_return(true) }
+ it { is_expected.to run.with_params('com').and_return(true) }
+ it { is_expected.to run.with_params('com.').and_return(true) }
+ it { is_expected.to run.with_params('x.com').and_return(true) }
+ it { is_expected.to run.with_params('x.com.').and_return(true) }
+ it { is_expected.to run.with_params('foo.example.com').and_return(true) }
+ it { is_expected.to run.with_params('foo.example.com.').and_return(true) }
+ it { is_expected.to run.with_params('2foo.example.com').and_return(true) }
+ it { is_expected.to run.with_params('2foo.example.com.').and_return(true) }
+ it { is_expected.to run.with_params('www.2foo.example.com').and_return(true) }
+ it { is_expected.to run.with_params('www.2foo.example.com.').and_return(true) }
+ describe 'inputs with spaces' do
+ it { is_expected.to run.with_params('invalid domain').and_return(false) }
+ end
+ describe 'inputs with hyphens' do
+ it { is_expected.to run.with_params('foo-bar.example.com').and_return(true) }
+ it { is_expected.to run.with_params('foo-bar.example.com.').and_return(true) }
+ it { is_expected.to run.with_params('www.foo-bar.example.com').and_return(true) }
+ it { is_expected.to run.with_params('www.foo-bar.example.com.').and_return(true) }
+ it { is_expected.to run.with_params('-foo.example.com').and_return(false) }
+ it { is_expected.to run.with_params('-foo.example.com').and_return(false) }
+ end
+ # Values obtained from Facter values will be frozen strings
+ # in newer versions of Facter:
+ it { is_expected.to run.with_params('www.example.com'.freeze).and_return(true) }
+ describe 'top level domain must be alphabetic if there are multiple labels' do
+ it { is_expected.to run.with_params('2com').and_return(true) }
+ it { is_expected.to run.with_params('www.example.2com').and_return(false) }
+ end
+ describe 'IP addresses are not domain names' do
+ it { is_expected.to run.with_params('192.168.1.1').and_return(false) }
end
end
diff --git a/spec/functions/is_email_address_spec.rb b/spec/functions/is_email_address_spec.rb
new file mode 100755
index 0000000..8b7b358
--- /dev/null
+++ b/spec/functions/is_email_address_spec.rb
@@ -0,0 +1,14 @@
+require 'spec_helper'
+
+describe 'is_email_address' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params([], []).and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params('bob@gmail.com').and_return(true) }
+ it { is_expected.to run.with_params('alice+puppetlabs.com@gmail.com').and_return(true) }
+ it { is_expected.to run.with_params('peter.parker@gmail.com').and_return(true) }
+ it { is_expected.to run.with_params('1.2.3@domain').and_return(false) }
+ it { is_expected.to run.with_params('1.2.3.4.5@').and_return(false) }
+ it { is_expected.to run.with_params({}).and_return(false) }
+ it { is_expected.to run.with_params([]).and_return(false) }
+end
diff --git a/spec/functions/is_float_spec.rb b/spec/functions/is_float_spec.rb
index d926634..af3322e 100755
--- a/spec/functions/is_float_spec.rb
+++ b/spec/functions/is_float_spec.rb
@@ -1,33 +1,42 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the is_float function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+describe 'is_float' do
- it "should exist" do
- expect(Puppet::Parser::Functions.function("is_float")).to eq("function_is_float")
- end
+ it { is_expected.not_to eq(nil) }
- it "should raise a ParseError if there is less than 1 arguments" do
- expect { scope.function_is_float([]) }.to( raise_error(Puppet::ParseError))
- end
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params(0.1, 0.2).and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
- it "should return true if a float" do
- result = scope.function_is_float(["0.12"])
- expect(result).to(eq(true))
+ describe 'passing a string' do
+ it { is_expected.to run.with_params('0.1').and_return(true) }
+ it { is_expected.to run.with_params('1.0').and_return(true) }
+ it { is_expected.to run.with_params('1').and_return(false) }
+ it { is_expected.to run.with_params('one').and_return(false) }
+ it { is_expected.to run.with_params('one 1.0').and_return(false) }
+ it { is_expected.to run.with_params('1.0 one').and_return(false) }
end
- it "should return false if a string" do
- result = scope.function_is_float(["asdf"])
- expect(result).to(eq(false))
+ describe 'passing numbers' do
+ it { is_expected.to run.with_params(0.1).and_return(true) }
+ it { is_expected.to run.with_params(1.0).and_return(true) }
+ it { is_expected.to run.with_params(1).and_return(false) }
end
- it "should return false if an integer" do
- result = scope.function_is_float(["3"])
- expect(result).to(eq(false))
- end
- it "should return true if a float is created from an arithmetical operation" do
- result = scope.function_is_float([3.2*2])
- expect(result).to(eq(true))
+ context 'Checking for deprecation warning' do
+ after(:all) do
+ ENV.delete('STDLIB_LOG_DEPRECATIONS')
+ end
+ # Checking for deprecation warning, which should only be provoked when the env variable for it is set.
+ it 'should display a single deprecation' do
+ ENV['STDLIB_LOG_DEPRECATIONS'] = "true"
+ scope.expects(:warning).with(includes('This method is deprecated'))
+ is_expected.to run.with_params(2.2).and_return(true)
+ end
+ it 'should display no warning for deprecation' do
+ ENV['STDLIB_LOG_DEPRECATIONS'] = "false"
+ scope.expects(:warning).with(includes('This method is deprecated')).never
+ is_expected.to run.with_params(1.0).and_return(true)
+ end
end
+
end
diff --git a/spec/functions/is_function_available.rb b/spec/functions/is_function_available.rb
index 3a9aa1b..44f08c0 100755
--- a/spec/functions/is_function_available.rb
+++ b/spec/functions/is_function_available.rb
@@ -1,31 +1,9 @@
-#!/usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the is_function_available function" do
- before :all do
- Puppet::Parser::Functions.autoloader.loadall
- end
-
- before :each do
- @scope = Puppet::Parser::Scope.new
- end
-
- it "should exist" do
- expect(Puppet::Parser::Functions.function("is_function_available")).to eq("function_is_function_available")
- end
-
- it "should raise a ParseError if there is less than 1 arguments" do
- expect { @scope.function_is_function_available([]) }.to( raise_error(Puppet::ParseError))
- end
-
- it "should return false if a nonexistent function is passed" do
- result = @scope.function_is_function_available(['jeff_mccunes_left_sock'])
- expect(result).to(eq(false))
- end
-
- it "should return true if an available function is passed" do
- result = @scope.function_is_function_available(['require'])
- expect(result).to(eq(true))
- end
-
+describe 'is_function_available' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params('include').and_return(true) }
+ it { is_expected.to run.with_params('no_such_function').and_return(false) }
end
diff --git a/spec/functions/is_hash_spec.rb b/spec/functions/is_hash_spec.rb
index a849411..c2599a0 100755
--- a/spec/functions/is_hash_spec.rb
+++ b/spec/functions/is_hash_spec.rb
@@ -1,29 +1,11 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the is_hash function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- it "should exist" do
- expect(Puppet::Parser::Functions.function("is_hash")).to eq("function_is_hash")
- end
-
- it "should raise a ParseError if there is less than 1 arguments" do
- expect { scope.function_is_hash([]) }.to( raise_error(Puppet::ParseError))
- end
-
- it "should return true if passed a hash" do
- result = scope.function_is_hash([{"a"=>1,"b"=>2}])
- expect(result).to(eq(true))
- end
-
- it "should return false if passed an array" do
- result = scope.function_is_hash([["a","b"]])
- expect(result).to(eq(false))
- end
-
- it "should return false if passed a string" do
- result = scope.function_is_hash(["asdf"])
- expect(result).to(eq(false))
- end
+describe 'is_hash' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params({}, {}).and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params('').and_return(false) }
+ it { is_expected.to run.with_params({}).and_return(true) }
+ it { is_expected.to run.with_params([]).and_return(false) }
+ it { is_expected.to run.with_params(1).and_return(false) }
end
diff --git a/spec/functions/is_integer_spec.rb b/spec/functions/is_integer_spec.rb
index f0cbca8..b296830 100755
--- a/spec/functions/is_integer_spec.rb
+++ b/spec/functions/is_integer_spec.rb
@@ -1,69 +1,45 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the is_integer function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+describe 'is_integer' do
+
+ it { is_expected.not_to eq(nil) }
+
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params(1, 2).and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+
+ it { is_expected.to run.with_params(3).and_return(true) }
+ it { is_expected.to run.with_params('3').and_return(true) }
+ it { is_expected.to run.with_params(-3).and_return(true) }
+ it { is_expected.to run.with_params('-3').and_return(true) }
+
+ it { is_expected.to run.with_params(3.7).and_return(false) }
+ it { is_expected.to run.with_params('3.7').and_return(false) }
+ it { is_expected.to run.with_params(-3.7).and_return(false) }
+ it { is_expected.to run.with_params('3.7').and_return(false) }
+
+ it { is_expected.to run.with_params('one').and_return(false) }
+ it { is_expected.to run.with_params([]).and_return(false) }
+ it { is_expected.to run.with_params([1]).and_return(false) }
+ it { is_expected.to run.with_params({}).and_return(false) }
+ it { is_expected.to run.with_params(true).and_return(false) }
+ it { is_expected.to run.with_params(false).and_return(false) }
+ it { is_expected.to run.with_params('0001234').and_return(false) }
+
+ context 'Checking for deprecation warning' do
+ after(:all) do
+ ENV.delete('STDLIB_LOG_DEPRECATIONS')
+ end
+ # Checking for deprecation warning, which should only be provoked when the env variable for it is set.
+ it 'should display a single deprecation' do
+ ENV['STDLIB_LOG_DEPRECATIONS'] = "true"
+ scope.expects(:warning).with(includes('This method is deprecated'))
+ is_expected.to run.with_params(50).and_return(true)
+ end
+ it 'should display no warning for deprecation' do
+ ENV['STDLIB_LOG_DEPRECATIONS'] = "false"
+ scope.expects(:warning).with(includes('This method is deprecated')).never
+ is_expected.to run.with_params(50).and_return(true)
+ end
+ end
- it "should exist" do
- expect(Puppet::Parser::Functions.function("is_integer")).to eq("function_is_integer")
- end
-
- it "should raise a ParseError if there is less than 1 arguments" do
- expect { scope.function_is_integer([]) }.to( raise_error(Puppet::ParseError))
- end
-
- it "should return true if an integer" do
- result = scope.function_is_integer(["3"])
- expect(result).to(eq(true))
- end
-
- it "should return true if a negative integer" do
- result = scope.function_is_integer(["-7"])
- expect(result).to(eq(true))
- end
-
- it "should return false if a float" do
- result = scope.function_is_integer(["3.2"])
- expect(result).to(eq(false))
- end
-
- it "should return false if a string" do
- result = scope.function_is_integer(["asdf"])
- expect(result).to(eq(false))
- end
-
- it "should return true if an integer is created from an arithmetical operation" do
- result = scope.function_is_integer([3*2])
- expect(result).to(eq(true))
- end
-
- it "should return false if an array" do
- result = scope.function_is_numeric([["asdf"]])
- expect(result).to(eq(false))
- end
-
- it "should return false if a hash" do
- result = scope.function_is_numeric([{"asdf" => false}])
- expect(result).to(eq(false))
- end
-
- it "should return false if a boolean" do
- result = scope.function_is_numeric([true])
- expect(result).to(eq(false))
- end
-
- it "should return false if a whitespace is in the string" do
- result = scope.function_is_numeric([" -1324"])
- expect(result).to(eq(false))
- end
-
- it "should return false if it is zero prefixed" do
- result = scope.function_is_numeric(["0001234"])
- expect(result).to(eq(false))
- end
-
- it "should return false if it is wrapped inside an array" do
- result = scope.function_is_numeric([[1234]])
- expect(result).to(eq(false))
- end
end
diff --git a/spec/functions/is_ip_address_spec.rb b/spec/functions/is_ip_address_spec.rb
index c16d12b..39525d7 100755
--- a/spec/functions/is_ip_address_spec.rb
+++ b/spec/functions/is_ip_address_spec.rb
@@ -1,39 +1,40 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the is_ip_address function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- it "should exist" do
- expect(Puppet::Parser::Functions.function("is_ip_address")).to eq("function_is_ip_address")
- end
-
- it "should raise a ParseError if there is less than 1 arguments" do
- expect { scope.function_is_ip_address([]) }.to( raise_error(Puppet::ParseError))
- end
-
- it "should return true if an IPv4 address" do
- result = scope.function_is_ip_address(["1.2.3.4"])
- expect(result).to(eq(true))
- end
-
- it "should return true if a full IPv6 address" do
- result = scope.function_is_ip_address(["fe80:0000:cd12:d123:e2f8:47ff:fe09:dd74"])
- expect(result).to(eq(true))
- end
-
- it "should return true if a compressed IPv6 address" do
- result = scope.function_is_ip_address(["fe00::1"])
- expect(result).to(eq(true))
- end
-
- it "should return false if not valid" do
- result = scope.function_is_ip_address(["asdf"])
- expect(result).to(eq(false))
- end
-
- it "should return false if IP octets out of range" do
- result = scope.function_is_ip_address(["1.1.1.300"])
- expect(result).to(eq(false))
+describe 'is_ip_address' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params([], []).and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params('1.2.3.4').and_return(true) }
+ it { is_expected.to run.with_params('1.2.3.255').and_return(true) }
+ it { is_expected.to run.with_params('1.2.3.256').and_return(false) }
+ it { is_expected.to run.with_params('1.2.3').and_return(false) }
+ it { is_expected.to run.with_params('1.2.3.4.5').and_return(false) }
+ it { is_expected.to run.with_params('fe00::1').and_return(true) }
+ it { is_expected.to run.with_params('fe80:0000:cd12:d123:e2f8:47ff:fe09:dd74').and_return(true) }
+ it { is_expected.to run.with_params('FE80:0000:CD12:D123:E2F8:47FF:FE09:DD74').and_return(true) }
+ it { is_expected.to run.with_params('fe80:0000:cd12:d123:e2f8:47ff:fe09:zzzz').and_return(false) }
+ it { is_expected.to run.with_params('fe80:0000:cd12:d123:e2f8:47ff:fe09').and_return(false) }
+ it { is_expected.to run.with_params('fe80:0000:cd12:d123:e2f8:47ff:fe09:dd74:dd74').and_return(false) }
+ it { is_expected.to run.with_params('').and_return(false) }
+ it { is_expected.to run.with_params('one').and_return(false) }
+ it { is_expected.to run.with_params(1).and_return(false) }
+ it { is_expected.to run.with_params({}).and_return(false) }
+ it { is_expected.to run.with_params([]).and_return(false) }
+
+ context 'Checking for deprecation warning', if: Puppet.version.to_f < 4.0 do
+ # Checking for deprecation warning, which should only be provoked when the env variable for it is set.
+ it 'should display a single deprecation' do
+ ENV['STDLIB_LOG_DEPRECATIONS'] = "true"
+ scope.expects(:warning).with(includes('This method is deprecated'))
+ is_expected.to run.with_params('1.2.3.4').and_return(true)
+ end
+ it 'should display no warning for deprecation' do
+ ENV['STDLIB_LOG_DEPRECATIONS'] = "false"
+ scope.expects(:warning).with(includes('This method is deprecated')).never
+ is_expected.to run.with_params('1.2.3.4').and_return(true)
+ end
+ after(:all) do
+ ENV.delete('STDLIB_LOG_DEPRECATIONS')
+ end
end
end
diff --git a/spec/functions/is_ipv4_address_spec.rb b/spec/functions/is_ipv4_address_spec.rb
new file mode 100644
index 0000000..985260c
--- /dev/null
+++ b/spec/functions/is_ipv4_address_spec.rb
@@ -0,0 +1,32 @@
+require 'spec_helper'
+
+describe 'is_ipv4_address' do
+
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+
+ SharedData::IPV4_PATTERNS.each do |value|
+ it { is_expected.to run.with_params(value).and_return(true) }
+ end
+
+ SharedData::IPV4_NEGATIVE_PATTERNS.each do |value|
+ it { is_expected.to run.with_params(value).and_return(false) }
+ end
+
+ context 'Checking for deprecation warning', if: Puppet.version.to_f < 4.0 do
+ after(:all) do
+ ENV.delete('STDLIB_LOG_DEPRECATIONS')
+ end
+ # Checking for deprecation warning, which should only be provoked when the env variable for it is set.
+ it 'should display a single deprecation' do
+ ENV['STDLIB_LOG_DEPRECATIONS'] = "true"
+ scope.expects(:warning).with(includes('This method is deprecated'))
+ is_expected.to run.with_params(SharedData::IPV4_PATTERNS.first).and_return(true)
+ end
+ it 'should display no warning for deprecation' do
+ ENV['STDLIB_LOG_DEPRECATIONS'] = "false"
+ scope.expects(:warning).with(includes('This method is deprecated')).never
+ is_expected.to run.with_params(SharedData::IPV4_PATTERNS.first).and_return(true)
+ end
+ end
+end
diff --git a/spec/functions/is_ipv6_address_spec.rb b/spec/functions/is_ipv6_address_spec.rb
new file mode 100644
index 0000000..acd6a87
--- /dev/null
+++ b/spec/functions/is_ipv6_address_spec.rb
@@ -0,0 +1,30 @@
+require 'spec_helper'
+
+describe 'is_ipv6_address' do
+
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params('2001:0db8:85a3:0000:0000:8a2e:0370:7334').and_return(true) }
+ it { is_expected.to run.with_params('85a3:0000:0000:8a2e:0370:7334:100.100.100.100').and_return(true) }
+ it { is_expected.to run.with_params('1.2.3').and_return(false) }
+ it { is_expected.to run.with_params('1.2.3.4.5').and_return(false) }
+ it { is_expected.to run.with_params('').and_return(false) }
+ it { is_expected.to run.with_params('one').and_return(false) }
+
+ context 'Checking for deprecation warning', if: Puppet.version.to_f < 4.0 do
+ after(:all) do
+ ENV.delete('STDLIB_LOG_DEPRECATIONS')
+ end
+ # Checking for deprecation warning, which should only be provoked when the env variable for it is set.
+ it 'should display a single deprecation' do
+ ENV['STDLIB_LOG_DEPRECATIONS'] = "true"
+ scope.expects(:warning).with(includes('This method is deprecated'))
+ is_expected.to run.with_params('2001:0db8:85a3:0000:0000:8a2e:0370:7334').and_return(true)
+ end
+ it 'should display no warning for deprecation' do
+ ENV['STDLIB_LOG_DEPRECATIONS'] = "false"
+ scope.expects(:warning).with(includes('This method is deprecated')).never
+ is_expected.to run.with_params('2001:0db8:85a3:0000:0000:8a2e:0370:7334').and_return(true)
+ end
+ end
+end
diff --git a/spec/functions/is_mac_address_spec.rb b/spec/functions/is_mac_address_spec.rb
index 66edd19..5f76a91 100755
--- a/spec/functions/is_mac_address_spec.rb
+++ b/spec/functions/is_mac_address_spec.rb
@@ -1,29 +1,24 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the is_mac_address function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- it "should exist" do
- expect(Puppet::Parser::Functions.function("is_mac_address")).to eq("function_is_mac_address")
- end
-
- it "should raise a ParseError if there is less than 1 arguments" do
- expect { scope.function_is_mac_address([]) }.to( raise_error(Puppet::ParseError))
- end
-
- it "should return true if a valid mac address" do
- result = scope.function_is_mac_address(["00:a0:1f:12:7f:a0"])
- expect(result).to(eq(true))
- end
-
- it "should return false if octets are out of range" do
- result = scope.function_is_mac_address(["00:a0:1f:12:7f:g0"])
- expect(result).to(eq(false))
- end
-
- it "should return false if not valid" do
- result = scope.function_is_mac_address(["not valid"])
- expect(result).to(eq(false))
- end
+describe 'is_mac_address' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params([], []).and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params('00:a0:1f:12:7f:a0').and_return(true) }
+ it { is_expected.to run.with_params('00:A0:1F:12:7F:A0').and_return(true) }
+ it { is_expected.to run.with_params('00:00:00:00:00:0g').and_return(false) }
+ it { is_expected.to run.with_params('').and_return(false) }
+ it { is_expected.to run.with_params('one').and_return(false) }
+ it {
+ pending "should properly typecheck its arguments"
+ is_expected.to run.with_params(1).and_return(false)
+ }
+ it {
+ pending "should properly typecheck its arguments"
+ is_expected.to run.with_params({}).and_return(false)
+ }
+ it {
+ pending "should properly typecheck its arguments"
+ is_expected.to run.with_params([]).and_return(false)
+ }
end
diff --git a/spec/functions/is_numeric_spec.rb b/spec/functions/is_numeric_spec.rb
index 4176961..5962d8a 100755
--- a/spec/functions/is_numeric_spec.rb
+++ b/spec/functions/is_numeric_spec.rb
@@ -1,119 +1,48 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the is_numeric function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- it "should exist" do
- expect(Puppet::Parser::Functions.function("is_numeric")).to eq("function_is_numeric")
- end
-
- it "should raise a ParseError if there is less than 1 argument" do
- expect { scope.function_is_numeric([]) }.to( raise_error(Puppet::ParseError))
- end
-
- it "should return true if an integer" do
- result = scope.function_is_numeric(["3"])
- expect(result).to(eq(true))
- end
-
- it "should return true if a float" do
- result = scope.function_is_numeric(["3.2"])
- expect(result).to(eq(true))
- end
-
- it "should return true if an integer is created from an arithmetical operation" do
- result = scope.function_is_numeric([3*2])
- expect(result).to(eq(true))
- end
-
- it "should return true if a float is created from an arithmetical operation" do
- result = scope.function_is_numeric([3.2*2])
- expect(result).to(eq(true))
- end
-
- it "should return false if a string" do
- result = scope.function_is_numeric(["asdf"])
- expect(result).to(eq(false))
- end
-
- it "should return false if an array" do
- result = scope.function_is_numeric([["asdf"]])
- expect(result).to(eq(false))
- end
-
- it "should return false if an array of integers" do
- result = scope.function_is_numeric([[1,2,3,4]])
- expect(result).to(eq(false))
- end
-
- it "should return false if a hash" do
- result = scope.function_is_numeric([{"asdf" => false}])
- expect(result).to(eq(false))
- end
-
- it "should return false if a hash with numbers in it" do
- result = scope.function_is_numeric([{1 => 2}])
- expect(result).to(eq(false))
- end
-
- it "should return false if a boolean" do
- result = scope.function_is_numeric([true])
- expect(result).to(eq(false))
- end
-
- it "should return true if a negative float with exponent" do
- result = scope.function_is_numeric(["-342.2315e-12"])
- expect(result).to(eq(true))
- end
-
- it "should return false if a negative integer with whitespaces before/after the dash" do
- result = scope.function_is_numeric([" - 751"])
- expect(result).to(eq(false))
+describe 'is_numeric' do
+
+ it { is_expected.not_to eq(nil) }
+
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params(1, 2).and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+
+ it { is_expected.to run.with_params(3).and_return(true) }
+ it { is_expected.to run.with_params('3').and_return(true) }
+ it { is_expected.to run.with_params(-3).and_return(true) }
+ it { is_expected.to run.with_params('-3').and_return(true) }
+
+ it { is_expected.to run.with_params(3.7).and_return(true) }
+ it { is_expected.to run.with_params('3.7').and_return(true) }
+ it { is_expected.to run.with_params(-3.7).and_return(true) }
+ it { is_expected.to run.with_params('3.7').and_return(true) }
+
+ it { is_expected.to run.with_params('-342.2315e-12').and_return(true) }
+
+ it { is_expected.to run.with_params('one').and_return(false) }
+ it { is_expected.to run.with_params([]).and_return(false) }
+ it { is_expected.to run.with_params([1]).and_return(false) }
+ it { is_expected.to run.with_params({}).and_return(false) }
+ it { is_expected.to run.with_params(true).and_return(false) }
+ it { is_expected.to run.with_params(false).and_return(false) }
+ it { is_expected.to run.with_params('0001234').and_return(false) }
+ it { is_expected.to run.with_params(' - 1234').and_return(false) }
+
+ context 'Checking for deprecation warning' do
+ after(:all) do
+ ENV.delete('STDLIB_LOG_DEPRECATIONS')
+ end
+ # Checking for deprecation warning, which should only be provoked when the env variable for it is set.
+ it 'should display a single deprecation' do
+ ENV['STDLIB_LOG_DEPRECATIONS'] = "true"
+ scope.expects(:warning).with(includes('This method is deprecated'))
+ is_expected.to run.with_params(7).and_return(true)
+ end
+ it 'should display no warning for deprecation' do
+ ENV['STDLIB_LOG_DEPRECATIONS'] = "false"
+ scope.expects(:warning).with(includes('This method is deprecated')).never
+ is_expected.to run.with_params(7).and_return(true)
+ end
end
-# it "should return true if a hexadecimal" do
-# result = scope.function_is_numeric(["0x52F8c"])
-# result.should(eq(true))
-# end
-#
-# it "should return true if a hexadecimal with uppercase 0X prefix" do
-# result = scope.function_is_numeric(["0X52F8c"])
-# result.should(eq(true))
-# end
-#
-# it "should return false if a hexadecimal without a prefix" do
-# result = scope.function_is_numeric(["52F8c"])
-# result.should(eq(false))
-# end
-#
-# it "should return true if a octal" do
-# result = scope.function_is_numeric(["0751"])
-# result.should(eq(true))
-# end
-#
-# it "should return true if a negative hexadecimal" do
-# result = scope.function_is_numeric(["-0x52F8c"])
-# result.should(eq(true))
-# end
-#
-# it "should return true if a negative octal" do
-# result = scope.function_is_numeric(["-0751"])
-# result.should(eq(true))
-# end
-#
-# it "should return false if a negative octal with whitespaces before/after the dash" do
-# result = scope.function_is_numeric([" - 0751"])
-# result.should(eq(false))
-# end
-#
-# it "should return false if a bad hexadecimal" do
-# result = scope.function_is_numeric(["0x23d7g"])
-# result.should(eq(false))
-# end
-#
-# it "should return false if a bad octal" do
-# result = scope.function_is_numeric(["0287"])
-# result.should(eq(false))
-# end
end
diff --git a/spec/functions/is_string_spec.rb b/spec/functions/is_string_spec.rb
index 6a0801a..e92f85c 100755
--- a/spec/functions/is_string_spec.rb
+++ b/spec/functions/is_string_spec.rb
@@ -1,34 +1,47 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the is_string function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+describe 'is_string' do
- it "should exist" do
- expect(Puppet::Parser::Functions.function("is_string")).to eq("function_is_string")
- end
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it {
+ pending("Current implementation ignores parameters after the first.")
+ is_expected.to run.with_params('', '').and_raise_error(Puppet::ParseError, /wrong number of arguments/i)
+ }
- it "should raise a ParseError if there is less than 1 arguments" do
- expect { scope.function_is_string([]) }.to( raise_error(Puppet::ParseError))
- end
+ it { is_expected.to run.with_params(3).and_return(false) }
+ it { is_expected.to run.with_params('3').and_return(false) }
+ it { is_expected.to run.with_params(-3).and_return(false) }
+ it { is_expected.to run.with_params('-3').and_return(false) }
- it "should return true if a string" do
- result = scope.function_is_string(["asdf"])
- expect(result).to(eq(true))
- end
+ it { is_expected.to run.with_params(3.7).and_return(false) }
+ it { is_expected.to run.with_params('3.7').and_return(false) }
+ it { is_expected.to run.with_params(-3.7).and_return(false) }
+ it { is_expected.to run.with_params('3.7').and_return(false) }
- it "should return false if an integer" do
- result = scope.function_is_string(["3"])
- expect(result).to(eq(false))
- end
+ it { is_expected.to run.with_params([]).and_return(false) }
+ it { is_expected.to run.with_params([1]).and_return(false) }
+ it { is_expected.to run.with_params({}).and_return(false) }
+ it { is_expected.to run.with_params(true).and_return(false) }
+ it { is_expected.to run.with_params(false).and_return(false) }
+ it { is_expected.to run.with_params('one').and_return(true) }
+ it { is_expected.to run.with_params('0001234').and_return(true) }
- it "should return false if a float" do
- result = scope.function_is_string(["3.23"])
- expect(result).to(eq(false))
- end
+ context 'Checking for deprecation warning' do
+ after(:all) do
+ ENV.delete('STDLIB_LOG_DEPRECATIONS')
+ end
+ # Checking for deprecation warning, which should only be provoked when the env variable for it is set.
+ it 'should display a single deprecation' do
+ ENV['STDLIB_LOG_DEPRECATIONS'] = "true"
+ scope.expects(:warning).with(includes('This method is deprecated'))
+ is_expected.to run.with_params('sponge').and_return(true)
+ end
+ it 'should display no warning for deprecation' do
+ ENV['STDLIB_LOG_DEPRECATIONS'] = "false"
+ scope.expects(:warning).with(includes('This method is deprecated')).never
+ is_expected.to run.with_params('bob').and_return(true)
+ end
+ end
- it "should return false if an array" do
- result = scope.function_is_string([["a","b","c"]])
- expect(result).to(eq(false))
- end
end
diff --git a/spec/functions/join_keys_to_values_spec.rb b/spec/functions/join_keys_to_values_spec.rb
index 4a9ae87..c2bae5b 100755
--- a/spec/functions/join_keys_to_values_spec.rb
+++ b/spec/functions/join_keys_to_values_spec.rb
@@ -1,40 +1,23 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the join_keys_to_values function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- it "should exist" do
- expect(Puppet::Parser::Functions.function("join_keys_to_values")).to eq("function_join_keys_to_values")
- end
-
- it "should raise a ParseError if there are fewer than two arguments" do
- expect { scope.function_join_keys_to_values([{}]) }.to raise_error Puppet::ParseError
- end
-
- it "should raise a ParseError if there are greater than two arguments" do
- expect { scope.function_join_keys_to_values([{}, 'foo', 'bar']) }.to raise_error Puppet::ParseError
- end
-
- it "should raise a TypeError if the first argument is an array" do
- expect { scope.function_join_keys_to_values([[1,2], ',']) }.to raise_error TypeError
- end
-
- it "should raise a TypeError if the second argument is an array" do
- expect { scope.function_join_keys_to_values([{}, [1,2]]) }.to raise_error TypeError
- end
-
- it "should raise a TypeError if the second argument is a number" do
- expect { scope.function_join_keys_to_values([{}, 1]) }.to raise_error TypeError
- end
-
- it "should return an empty array given an empty hash" do
- result = scope.function_join_keys_to_values([{}, ":"])
- expect(result).to eq([])
- end
-
- it "should join hash's keys to its values" do
- result = scope.function_join_keys_to_values([{'a'=>1,2=>'foo',:b=>nil}, ":"])
- expect(result).to match_array(['a:1','2:foo','b:'])
+describe 'join_keys_to_values' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /Takes exactly two arguments/) }
+ it { is_expected.to run.with_params({}, '', '').and_raise_error(Puppet::ParseError, /Takes exactly two arguments/) }
+ it { is_expected.to run.with_params('one', '').and_raise_error(TypeError, /The first argument must be a hash/) }
+ it { is_expected.to run.with_params({}, 2).and_raise_error(TypeError, /The second argument must be a string/) }
+
+ it { is_expected.to run.with_params({}, '').and_return([]) }
+ it { is_expected.to run.with_params({}, ':').and_return([]) }
+ it { is_expected.to run.with_params({ 'key' => 'value' }, '').and_return(['keyvalue']) }
+ it { is_expected.to run.with_params({ 'key' => 'value' }, ':').and_return(['key:value']) }
+ it { is_expected.to run.with_params({ 'key' => nil }, ':').and_return(['key:']) }
+ it 'should run join_keys_to_values(<hash with multiple keys>, ":") and return the proper array' do
+ result = subject.call([{ 'key1' => 'value1', 'key2' => 'value2' }, ':'])
+ expect(result.sort).to eq(['key1:value1', 'key2:value2'].sort)
+ end
+ it 'should run join_keys_to_values(<hash with array value>, " ") and return the proper array' do
+ result = subject.call([{ 'key1' => 'value1', 'key2' => ['value2', 'value3'] }, ' '])
+ expect(result.sort).to eq(['key1 value1', 'key2 value2', 'key2 value3'].sort)
end
end
diff --git a/spec/functions/join_spec.rb b/spec/functions/join_spec.rb
index 793c36f..a300571 100755
--- a/spec/functions/join_spec.rb
+++ b/spec/functions/join_spec.rb
@@ -1,19 +1,19 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the join function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+describe 'join' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it {
+ pending("Current implementation ignores parameters after the second.")
+ is_expected.to run.with_params([], '', '').and_raise_error(Puppet::ParseError, /wrong number of arguments/i)
+ }
+ it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, /Requires array to work with/) }
+ it { is_expected.to run.with_params([], 2).and_raise_error(Puppet::ParseError, /Requires string to work with/) }
- it "should exist" do
- expect(Puppet::Parser::Functions.function("join")).to eq("function_join")
- end
-
- it "should raise a ParseError if there is less than 1 arguments" do
- expect { scope.function_join([]) }.to( raise_error(Puppet::ParseError))
- end
-
- it "should join an array into a string" do
- result = scope.function_join([["a","b","c"], ":"])
- expect(result).to(eq("a:b:c"))
- end
+ it { is_expected.to run.with_params([]).and_return('') }
+ it { is_expected.to run.with_params([], ':').and_return('') }
+ it { is_expected.to run.with_params(['one']).and_return('one') }
+ it { is_expected.to run.with_params(['one'], ':').and_return('one') }
+ it { is_expected.to run.with_params(['one', 'two', 'three']).and_return('onetwothree') }
+ it { is_expected.to run.with_params(['one', 'two', 'three'], ':').and_return('one:two:three') }
end
diff --git a/spec/functions/keys_spec.rb b/spec/functions/keys_spec.rb
index f2e7d42..2e009dc 100755
--- a/spec/functions/keys_spec.rb
+++ b/spec/functions/keys_spec.rb
@@ -1,21 +1,19 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the keys function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- it "should exist" do
- expect(Puppet::Parser::Functions.function("keys")).to eq("function_keys")
- end
-
- it "should raise a ParseError if there is less than 1 arguments" do
- expect { scope.function_keys([]) }.to( raise_error(Puppet::ParseError))
- end
-
- it "should return an array of keys when given a hash" do
- result = scope.function_keys([{'a'=>1, 'b'=>2}])
- # =~ performs 'array with same elements' (set) matching
- # For more info see RSpec::Matchers::MatchArray
- expect(result).to match_array(['a','b'])
+describe 'keys' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it {
+ pending("Current implementation ignores parameters after the first.")
+ is_expected.to run.with_params({}, {}).and_raise_error(Puppet::ParseError, /wrong number of arguments/i)
+ }
+ it { is_expected.to run.with_params('').and_raise_error(Puppet::ParseError, /Requires hash to work with/) }
+ it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, /Requires hash to work with/) }
+ it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError, /Requires hash to work with/) }
+ it { is_expected.to run.with_params({}).and_return([]) }
+ it { is_expected.to run.with_params({ 'key' => 'value' }).and_return(['key']) }
+ it 'should return the array of keys' do
+ result = subject.call([{ 'key1' => 'value1', 'key2' => 'value2' }])
+ expect(result).to match_array(['key1', 'key2'])
end
end
diff --git a/spec/functions/load_module_metadata_spec.rb b/spec/functions/load_module_metadata_spec.rb
new file mode 100755
index 0000000..1a61e2c
--- /dev/null
+++ b/spec/functions/load_module_metadata_spec.rb
@@ -0,0 +1,38 @@
+require 'spec_helper'
+
+describe 'load_module_metadata' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params("one", "two", "three").and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+
+ describe "when calling with valid arguments" do
+ before :each do
+ if RSpec.configuration.puppet_future
+ allow(File).to receive(:read).with(/\/stdlib\/metadata.json/, {:encoding=>"utf-8"}).and_return('{"name": "puppetlabs-stdlib"}')
+ else
+ allow(File).to receive(:read).with(/\/stdlib\/metadata.json/).and_return('{"name": "puppetlabs-stdlib"}')
+ end
+ end
+ it "should json parse the file" do
+ allow(scope).to receive(:function_get_module_path).with(['science']).and_return('/path/to/module/')
+ allow(File).to receive(:exists?).with('/path/to/module/metadata.json').and_return(true)
+ allow(File).to receive(:read).with('/path/to/module/metadata.json').and_return('{"name": "spencer-science"}')
+
+ result = subject.call(['science'])
+ expect(result['name']).to eq('spencer-science')
+ end
+
+ it "should fail by default if there is no metadata.json" do
+ allow(scope).to receive(:function_get_module_path).with(['science']).and_return('/path/to/module/')
+ allow(File).to receive(:exists?).with('/path/to/module/metadata.json').and_return(false)
+ expect {subject.call(['science'])}.to raise_error(Puppet::ParseError)
+ end
+
+ it "should return nil if user allows empty metadata.json" do
+ allow(scope).to receive(:function_get_module_path).with(['science']).and_return('/path/to/module/')
+ allow(File).to receive(:exists?).with('/path/to/module/metadata.json').and_return(false)
+ result = subject.call(['science', true])
+ expect(result).to eq({})
+ end
+ end
+end
diff --git a/spec/functions/loadjson_spec.rb b/spec/functions/loadjson_spec.rb
new file mode 100644
index 0000000..a00dff9
--- /dev/null
+++ b/spec/functions/loadjson_spec.rb
@@ -0,0 +1,49 @@
+require 'spec_helper'
+
+describe 'loadjson' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(ArgumentError, /wrong number of arguments/i) }
+
+ describe "when calling with valid arguments" do
+ before :each do
+ if RSpec.configuration.puppet_future
+ allow(File).to receive(:read).with(/\/stdlib\/metadata.json/, {:encoding=>"utf-8"}).and_return('{"name": "puppetlabs-stdlib"}')
+ else
+ allow(File).to receive(:read).with(/\/stdlib\/metadata.json/).and_return('{"name": "puppetlabs-stdlib"}')
+ end
+ end
+
+ context 'when a non-existing file is specified' do
+ let(:filename) { '/tmp/doesnotexist' }
+ before {
+ allow(File).to receive(:exists?).with(filename).and_return(false).once
+ allow(PSON).to receive(:load).never
+ }
+ it { is_expected.to run.with_params(filename, {'default' => 'value'}).and_return({'default' => 'value'}) }
+ end
+
+ context 'when an existing file is specified' do
+ let(:filename) { '/tmp/doesexist' }
+ let(:data) { { 'key' => 'value' } }
+ let(:json) { '{"key":"value"}' }
+ before {
+ allow(File).to receive(:exists?).with(filename).and_return(true).once
+ allow(File).to receive(:read).with(filename).and_return(json).once
+ allow(File).to receive(:read).with(filename).and_return(json).once
+ allow(PSON).to receive(:load).with(json).and_return(data).once
+ }
+ it { is_expected.to run.with_params(filename).and_return(data) }
+ end
+
+ context 'when the file could not be parsed' do
+ let(:filename) { '/tmp/doesexist' }
+ let(:json) { '{"key":"value"}' }
+ before {
+ allow(File).to receive(:exists?).with(filename).and_return(true).once
+ allow(File).to receive(:read).with(filename).and_return(json).once
+ allow(PSON).to receive(:load).with(json).once.and_raise StandardError, 'Something terrible have happened!'
+ }
+ it { is_expected.to run.with_params(filename, {'default' => 'value'}).and_return({'default' => 'value'}) }
+ end
+ end
+end
diff --git a/spec/functions/loadyaml_spec.rb b/spec/functions/loadyaml_spec.rb
index cdc3d6f..9f16a1a 100755
--- a/spec/functions/loadyaml_spec.rb
+++ b/spec/functions/loadyaml_spec.rb
@@ -1,25 +1,34 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the loadyaml function" do
- include PuppetlabsSpec::Files
+describe 'loadyaml' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(ArgumentError, /wrong number of arguments/i) }
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- it "should exist" do
- expect(Puppet::Parser::Functions.function("loadyaml")).to eq("function_loadyaml")
+ context 'when a non-existing file is specified' do
+ let(:filename) { '/tmp/doesnotexist' }
+ before {
+ File.expects(:exists?).with(filename).returns(false).once
+ YAML.expects(:load_file).never
+ }
+ it { is_expected.to run.with_params(filename, {'default' => 'value'}).and_return({'default' => 'value'}) }
end
- it "should raise a ParseError if there is less than 1 arguments" do
- expect { scope.function_loadyaml([]) }.to raise_error(Puppet::ParseError)
+ context 'when an existing file is specified' do
+ let(:filename) { '/tmp/doesexist' }
+ let(:data) { { 'key' => 'value' } }
+ before {
+ File.expects(:exists?).with(filename).returns(true).once
+ YAML.expects(:load_file).with(filename).returns(data).once
+ }
+ it { is_expected.to run.with_params(filename).and_return(data) }
end
- it "should convert YAML file to a data structure" do
- yaml_file = tmpfilename ('yamlfile')
- File.open(yaml_file, 'w') do |fh|
- fh.write("---\n aaa: 1\n bbb: 2\n ccc: 3\n ddd: 4\n")
- end
- result = scope.function_loadyaml([yaml_file])
- expect(result).to eq({"aaa" => 1, "bbb" => 2, "ccc" => 3, "ddd" => 4 })
+ context 'when the file could not be parsed' do
+ let(:filename) { '/tmp/doesexist' }
+ before {
+ File.expects(:exists?).with(filename).returns(true).once
+ YAML.stubs(:load_file).with(filename).once.raises StandardError, 'Something terrible have happened!'
+ }
+ it { is_expected.to run.with_params(filename, {'default' => 'value'}).and_return({'default' => 'value'}) }
end
end
diff --git a/spec/functions/lstrip_spec.rb b/spec/functions/lstrip_spec.rb
index 68cca1c..981794e 100755
--- a/spec/functions/lstrip_spec.rb
+++ b/spec/functions/lstrip_spec.rb
@@ -1,28 +1,34 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the lstrip function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- it "should exist" do
- expect(Puppet::Parser::Functions.function("lstrip")).to eq("function_lstrip")
- end
-
- it "should raise a ParseError if there is less than 1 arguments" do
- expect { scope.function_lstrip([]) }.to( raise_error(Puppet::ParseError))
- end
-
- it "should lstrip a string" do
- result = scope.function_lstrip([" asdf"])
- expect(result).to(eq('asdf'))
- end
-
- it "should accept objects which extend String" do
- class AlsoString < String
- end
-
- value = AlsoString.new(" asdf")
- result = scope.function_lstrip([value])
- result.should(eq("asdf"))
- end
+describe 'lstrip' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it {
+ pending("Current implementation ignores parameters after the first.")
+ is_expected.to run.with_params('', '').and_raise_error(Puppet::ParseError, /wrong number of arguments/i)
+ }
+ it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, /Requires either array or string to work with/) }
+ it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, /Requires either array or string to work with/) }
+ it { is_expected.to run.with_params('').and_return('') }
+ it { is_expected.to run.with_params(' ').and_return('') }
+ it { is_expected.to run.with_params(' ').and_return('') }
+ it { is_expected.to run.with_params("\t").and_return('') }
+ it { is_expected.to run.with_params("\t ").and_return('') }
+ it { is_expected.to run.with_params('one').and_return('one') }
+ it { is_expected.to run.with_params(' one').and_return('one') }
+ it { is_expected.to run.with_params(' one').and_return('one') }
+ it { is_expected.to run.with_params("\tone").and_return('one') }
+ it { is_expected.to run.with_params("\t one").and_return('one') }
+ it { is_expected.to run.with_params('one ').and_return('one ') }
+ it { is_expected.to run.with_params(' one ').and_return('one ') }
+ it { is_expected.to run.with_params(' one ').and_return('one ') }
+ it { is_expected.to run.with_params("\tone ").and_return('one ') }
+ it { is_expected.to run.with_params("\t one ").and_return('one ') }
+ it { is_expected.to run.with_params("one \t").and_return("one \t") }
+ it { is_expected.to run.with_params(" one \t").and_return("one \t") }
+ it { is_expected.to run.with_params(" one \t").and_return("one \t") }
+ it { is_expected.to run.with_params("\tone \t").and_return("one \t") }
+ it { is_expected.to run.with_params("\t one \t").and_return("one \t") }
+ it { is_expected.to run.with_params(' o n e ').and_return('o n e ') }
+ it { is_expected.to run.with_params(AlsoString.new(' one ')).and_return('one ') }
end
diff --git a/spec/functions/max_spec.rb b/spec/functions/max_spec.rb
index c3d8a13..66fb0c8 100755
--- a/spec/functions/max_spec.rb
+++ b/spec/functions/max_spec.rb
@@ -1,27 +1,21 @@
-#! /usr/bin/env ruby -S rspec
-
require 'spec_helper'
-describe "the max function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- it "should exist" do
- expect(Puppet::Parser::Functions.function("max")).to eq("function_max")
- end
-
- it "should raise a ParseError if there is less than 1 arguments" do
- expect { scope.function_max([]) }.to( raise_error(Puppet::ParseError))
- end
-
- it "should be able to compare strings" do
- expect(scope.function_max(["albatross","dog","horse"])).to(eq("horse"))
- end
-
- it "should be able to compare numbers" do
- expect(scope.function_max([6,8,4])).to(eq(8))
- end
+describe 'max' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params(1).and_return(1) }
+ it { is_expected.to run.with_params(1, 2).and_return(2) }
+ it { is_expected.to run.with_params(1, 2, 3).and_return(3) }
+ it { is_expected.to run.with_params(3, 2, 1).and_return(3) }
+ it { is_expected.to run.with_params('one').and_return('one') }
+ it { is_expected.to run.with_params('one', 'two').and_return('two') }
+ it { is_expected.to run.with_params('one', 'two', 'three').and_return('two') }
+ it { is_expected.to run.with_params('three', 'two', 'one').and_return('two') }
- it "should be able to compare a number with a stringified number" do
- expect(scope.function_max([1,"2"])).to(eq("2"))
+ describe 'implementation artifacts' do
+ it { is_expected.to run.with_params(1, 'one').and_return('one') }
+ it { is_expected.to run.with_params('1', 'one').and_return('one') }
+ it { is_expected.to run.with_params('1.3e1', '1.4e0').and_return('1.4e0') }
+ it { is_expected.to run.with_params(1.3e1, 1.4e0).and_return(1.3e1) }
end
end
diff --git a/spec/functions/member_spec.rb b/spec/functions/member_spec.rb
index 1a1d7c6..527f887 100755
--- a/spec/functions/member_spec.rb
+++ b/spec/functions/member_spec.rb
@@ -1,34 +1,21 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the member function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- it "should exist" do
- expect(Puppet::Parser::Functions.function("member")).to eq("function_member")
- end
-
- it "should raise a ParseError if there is less than 1 arguments" do
- expect { scope.function_member([]) }.to( raise_error(Puppet::ParseError))
- end
-
- it "should return true if a member is in an array" do
- result = scope.function_member([["a","b","c"], "a"])
- expect(result).to(eq(true))
- end
-
- it "should return false if a member is not in an array" do
- result = scope.function_member([["a","b","c"], "d"])
- expect(result).to(eq(false))
- end
-
- it "should return true if a member array is in an array" do
- result = scope.function_member([["a","b","c"], ["a", "b"]])
- expect(result).to(eq(true))
- end
-
- it "should return false if a member array is not in an array" do
- result = scope.function_member([["a","b","c"], ["d", "e"]])
- expect(result).to(eq(false))
- end
+describe 'member' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it {
+ pending("Current implementation ignores parameters after the first.")
+ is_expected.to run.with_params([], [], []).and_raise_error(Puppet::ParseError, /wrong number of arguments/i)
+ }
+ it { is_expected.to run.with_params([], '').and_return(false) }
+ it { is_expected.to run.with_params([], ['']).and_return(false) }
+ it { is_expected.to run.with_params([''], '').and_return(true) }
+ it { is_expected.to run.with_params([''], ['']).and_return(true) }
+ it { is_expected.to run.with_params([], 'one').and_return(false) }
+ it { is_expected.to run.with_params([], ['one']).and_return(false) }
+ it { is_expected.to run.with_params(['one'], 'one').and_return(true) }
+ it { is_expected.to run.with_params(['one'], ['one']).and_return(true) }
+ it { is_expected.to run.with_params(['one', 'two', 'three', 'four'], ['four', 'two']).and_return(true) }
+ it { is_expected.to run.with_params(['one', 'two', 'three', 'four'], ['four', 'five']).and_return(false) }
end
diff --git a/spec/functions/merge_spec.rb b/spec/functions/merge_spec.rb
index 2abf976..7b53363 100755
--- a/spec/functions/merge_spec.rb
+++ b/spec/functions/merge_spec.rb
@@ -1,52 +1,25 @@
-#! /usr/bin/env ruby -S rspec
-
require 'spec_helper'
-describe Puppet::Parser::Functions.function(:merge) do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- describe 'when calling merge from puppet' do
- it "should not compile when no arguments are passed" do
- skip("Fails on 2.6.x, see bug #15912") if Puppet.version =~ /^2\.6\./
- Puppet[:code] = '$x = merge()'
- expect {
- scope.compiler.compile
- }.to raise_error(Puppet::ParseError, /wrong number of arguments/)
- end
-
- it "should not compile when 1 argument is passed" do
- skip("Fails on 2.6.x, see bug #15912") if Puppet.version =~ /^2\.6\./
- Puppet[:code] = "$my_hash={'one' => 1}\n$x = merge($my_hash)"
- expect {
- scope.compiler.compile
- }.to raise_error(Puppet::ParseError, /wrong number of arguments/)
- end
- end
-
- describe 'when calling merge on the scope instance' do
- it 'should require all parameters are hashes' do
- expect { new_hash = scope.function_merge([{}, '2'])}.to raise_error(Puppet::ParseError, /unexpected argument type String/)
- expect { new_hash = scope.function_merge([{}, 2])}.to raise_error(Puppet::ParseError, /unexpected argument type Fixnum/)
- end
-
- it 'should accept empty strings as puppet undef' do
- expect { new_hash = scope.function_merge([{}, ''])}.not_to raise_error
- end
-
- it 'should be able to merge two hashes' do
- new_hash = scope.function_merge([{'one' => '1', 'two' => '1'}, {'two' => '2', 'three' => '2'}])
- expect(new_hash['one']).to eq('1')
- expect(new_hash['two']).to eq('2')
- expect(new_hash['three']).to eq('2')
- end
-
- it 'should merge multiple hashes' do
- hash = scope.function_merge([{'one' => 1}, {'one' => '2'}, {'one' => '3'}])
- expect(hash['one']).to eq('3')
- end
-
- it 'should accept empty hashes' do
- expect(scope.function_merge([{},{},{}])).to eq({})
- end
+describe 'merge' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params({}, 'two').and_raise_error(Puppet::ParseError, /unexpected argument type String/) }
+ it { is_expected.to run.with_params({}, 1).and_raise_error(Puppet::ParseError, /unexpected argument type Fixnum/) }
+ it {
+ pending 'should not special case this'
+ is_expected.to run.with_params({}).and_return({})
+ }
+ it { is_expected.to run.with_params({}, {}).and_return({}) }
+ it { is_expected.to run.with_params({}, {}, {}).and_return({}) }
+ describe 'should accept empty strings as puppet undef' do
+ it { is_expected.to run.with_params({}, '').and_return({}) }
end
+ it { is_expected.to run.with_params({ 'key' => 'value' }, {}).and_return({ 'key' => 'value' }) }
+ it { is_expected.to run.with_params({}, { 'key' => 'value' }).and_return({ 'key' => 'value' }) }
+ it { is_expected.to run.with_params({ 'key' => 'value1' }, { 'key' => 'value2' }).and_return({ 'key' => 'value2' }) }
+ it {
+ is_expected.to run \
+ .with_params({ 'key1' => 'value1' }, { 'key2' => 'value2' }, { 'key3' => 'value3' }) \
+ .and_return({ 'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3' })
+ }
end
diff --git a/spec/functions/min_spec.rb b/spec/functions/min_spec.rb
index 35a0890..c840a72 100755
--- a/spec/functions/min_spec.rb
+++ b/spec/functions/min_spec.rb
@@ -1,27 +1,21 @@
-#! /usr/bin/env ruby -S rspec
-
require 'spec_helper'
-describe "the min function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- it "should exist" do
- expect(Puppet::Parser::Functions.function("min")).to eq("function_min")
- end
-
- it "should raise a ParseError if there is less than 1 arguments" do
- expect { scope.function_min([]) }.to( raise_error(Puppet::ParseError))
- end
-
- it "should be able to compare strings" do
- expect(scope.function_min(["albatross","dog","horse"])).to(eq("albatross"))
- end
-
- it "should be able to compare numbers" do
- expect(scope.function_min([6,8,4])).to(eq(4))
- end
+describe 'min' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params(1).and_return(1) }
+ it { is_expected.to run.with_params(1, 2).and_return(1) }
+ it { is_expected.to run.with_params(1, 2, 3).and_return(1) }
+ it { is_expected.to run.with_params(3, 2, 1).and_return(1) }
+ it { is_expected.to run.with_params('one').and_return('one') }
+ it { is_expected.to run.with_params('one', 'two').and_return('one') }
+ it { is_expected.to run.with_params('one', 'two', 'three').and_return('one') }
+ it { is_expected.to run.with_params('three', 'two', 'one').and_return('one') }
- it "should be able to compare a number with a stringified number" do
- expect(scope.function_min([1,"2"])).to(eq(1))
+ describe 'implementation artifacts' do
+ it { is_expected.to run.with_params(1, 'one').and_return(1) }
+ it { is_expected.to run.with_params('1', 'one').and_return('1') }
+ it { is_expected.to run.with_params('1.3e1', '1.4e0').and_return('1.3e1') }
+ it { is_expected.to run.with_params(1.3e1, 1.4e0).and_return(1.4e0) }
end
end
diff --git a/spec/functions/num2bool_spec.rb b/spec/functions/num2bool_spec.rb
index d0ba935..494afff 100755
--- a/spec/functions/num2bool_spec.rb
+++ b/spec/functions/num2bool_spec.rb
@@ -1,67 +1,22 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the num2bool function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- it "should exist" do
- expect(Puppet::Parser::Functions.function("num2bool")).to eq("function_num2bool")
- end
-
- it "should raise a ParseError if there are no arguments" do
- expect { scope.function_num2bool([]) }.to( raise_error(Puppet::ParseError))
- end
-
- it "should raise a ParseError if there are more than 1 arguments" do
- expect { scope.function_num2bool(["foo","bar"]) }.to( raise_error(Puppet::ParseError))
- end
-
- it "should raise a ParseError if passed something non-numeric" do
- expect { scope.function_num2bool(["xyzzy"]) }.to( raise_error(Puppet::ParseError))
- end
-
- it "should return true if passed string 1" do
- result = scope.function_num2bool(["1"])
- expect(result).to(be_truthy)
- end
-
- it "should return true if passed string 1.5" do
- result = scope.function_num2bool(["1.5"])
- expect(result).to(be_truthy)
- end
-
- it "should return true if passed number 1" do
- result = scope.function_num2bool([1])
- expect(result).to(be_truthy)
- end
-
- it "should return false if passed string 0" do
- result = scope.function_num2bool(["0"])
- expect(result).to(be_falsey)
- end
-
- it "should return false if passed number 0" do
- result = scope.function_num2bool([0])
- expect(result).to(be_falsey)
- end
-
- it "should return false if passed string -1" do
- result = scope.function_num2bool(["-1"])
- expect(result).to(be_falsey)
- end
-
- it "should return false if passed string -1.5" do
- result = scope.function_num2bool(["-1.5"])
- expect(result).to(be_falsey)
- end
-
- it "should return false if passed number -1" do
- result = scope.function_num2bool([-1])
- expect(result).to(be_falsey)
- end
-
- it "should return false if passed float -1.5" do
- result = scope.function_num2bool([-1.5])
- expect(result).to(be_falsey)
- end
+describe 'num2bool' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params(1, 2).and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params('abc').and_raise_error(Puppet::ParseError, /does not look like a number/) }
+ it { is_expected.to run.with_params(1).and_return(true) }
+ it { is_expected.to run.with_params('1').and_return(true) }
+ it { is_expected.to run.with_params(1.5).and_return(true) }
+ it { is_expected.to run.with_params('1.5').and_return(true) }
+ it { is_expected.to run.with_params(-1).and_return(false) }
+ it { is_expected.to run.with_params('-1').and_return(false) }
+ it { is_expected.to run.with_params(-1.5).and_return(false) }
+ it { is_expected.to run.with_params('-1.5').and_return(false) }
+ it { is_expected.to run.with_params(0).and_return(false) }
+ it { is_expected.to run.with_params('0').and_return(false) }
+ it { is_expected.to run.with_params([]).and_return(false) }
+ it { is_expected.to run.with_params('[]').and_raise_error(Puppet::ParseError, /does not look like a number/) }
+ it { is_expected.to run.with_params({}).and_return(false) }
+ it { is_expected.to run.with_params('{}').and_raise_error(Puppet::ParseError, /does not look like a number/) }
end
diff --git a/spec/functions/parsejson_spec.rb b/spec/functions/parsejson_spec.rb
index 1dd41b9..a01f1f6 100755
--- a/spec/functions/parsejson_spec.rb
+++ b/spec/functions/parsejson_spec.rb
@@ -1,22 +1,64 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the parsejson function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+describe 'parsejson' do
+ it 'should exist' do
+ is_expected.not_to eq(nil)
+ end
- it "should exist" do
- expect(Puppet::Parser::Functions.function("parsejson")).to eq("function_parsejson")
+ it 'should raise an error if called without any arguments' do
+ is_expected.to run.with_params().
+ and_raise_error(/wrong number of arguments/i)
end
- it "should raise a ParseError if there is less than 1 arguments" do
- expect { scope.function_parsejson([]) }.to( raise_error(Puppet::ParseError))
+ context 'with correct JSON data' do
+
+ it 'should be able to parse a JSON data with a Hash' do
+ is_expected.to run.with_params('{"a":"1","b":"2"}').
+ and_return({'a'=>'1', 'b'=>'2'})
+ end
+
+ it 'should be able to parse a JSON data with an Array' do
+ is_expected.to run.with_params('["a","b","c"]').
+ and_return(['a', 'b', 'c'])
+ end
+
+ it 'should be able to parse empty JSON values' do
+ is_expected.to run.with_params('[]').
+ and_return([])
+ is_expected.to run.with_params('{}').
+ and_return({})
+ end
+
+ it 'should be able to parse a JSON data with a mixed structure' do
+ is_expected.to run.with_params('{"a":"1","b":2,"c":{"d":[true,false]}}').
+ and_return({'a' =>'1', 'b' => 2, 'c' => { 'd' => [true, false] } })
+ end
+
+ it 'should not return the default value if the data was parsed correctly' do
+ is_expected.to run.with_params('{"a":"1"}', 'default_value').
+ and_return({'a' => '1'})
+ end
+
end
- it "should convert JSON to a data structure" do
- json = <<-EOS
-["aaa","bbb","ccc"]
-EOS
- result = scope.function_parsejson([json])
- expect(result).to(eq(['aaa','bbb','ccc']))
+ context 'with incorrect JSON data' do
+ it 'should raise an error with invalid JSON and no default' do
+ is_expected.to run.with_params('').
+ and_raise_error(PSON::ParserError)
+ end
+
+ it 'should support a structure for a default value' do
+ is_expected.to run.with_params('', {'a' => '1'}).
+ and_return({'a' => '1'})
+ end
+
+ ['', 1, 1.2, nil, true, false, [], {}, :yaml].each do |value|
+ it "should return the default value for an incorrect #{value.inspect} (#{value.class}) parameter" do
+ is_expected.to run.with_params(value, 'default_value').
+ and_return('default_value')
+ end
+ end
+
end
+
end
diff --git a/spec/functions/parseyaml_spec.rb b/spec/functions/parseyaml_spec.rb
index e5f145b..fa947ca 100755
--- a/spec/functions/parseyaml_spec.rb
+++ b/spec/functions/parseyaml_spec.rb
@@ -1,24 +1,81 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the parseyaml function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+describe 'parseyaml' do
+ it 'should exist' do
+ is_expected.not_to eq(nil)
+ end
- it "should exist" do
- expect(Puppet::Parser::Functions.function("parseyaml")).to eq("function_parseyaml")
+ it 'should raise an error if called without any arguments' do
+ is_expected.to run.with_params().
+ and_raise_error(/wrong number of arguments/i)
end
- it "should raise a ParseError if there is less than 1 arguments" do
- expect { scope.function_parseyaml([]) }.to( raise_error(Puppet::ParseError))
+ context 'with correct YAML data' do
+ it 'should be able to parse a YAML data with a String' do
+ is_expected.to run.with_params('--- just a string').
+ and_return('just a string')
+ is_expected.to run.with_params('just a string').
+ and_return('just a string')
+ end
+
+ it 'should be able to parse a YAML data with a Hash' do
+ is_expected.to run.with_params("---\na: '1'\nb: '2'\n").
+ and_return({'a' => '1', 'b' => '2'})
+ end
+
+ it 'should be able to parse a YAML data with an Array' do
+ is_expected.to run.with_params("---\n- a\n- b\n- c\n").
+ and_return(['a', 'b', 'c'])
+ end
+
+ it 'should be able to parse a YAML data with a mixed structure' do
+ is_expected.to run.with_params("---\na: '1'\nb: 2\nc:\n d:\n - :a\n - true\n - false\n").
+ and_return({'a' => '1', 'b' => 2, 'c' => {'d' => [:a, true, false]}})
+ end
+
+ it 'should not return the default value if the data was parsed correctly' do
+ is_expected.to run.with_params("---\na: '1'\n", 'default_value').
+ and_return({'a' => '1'})
+ end
+
end
- it "should convert YAML to a data structure" do
- yaml = <<-EOS
-- aaa
-- bbb
-- ccc
-EOS
- result = scope.function_parseyaml([yaml])
- expect(result).to(eq(['aaa','bbb','ccc']))
+ context 'on a modern ruby', :unless => RUBY_VERSION == '1.8.7' do
+ it 'should raise an error with invalid YAML and no default' do
+ is_expected.to run.with_params('["one"').
+ and_raise_error(Psych::SyntaxError)
+ end
end
+
+ context 'when running on ruby 1.8.7, which does not have Psych', :if => RUBY_VERSION == '1.8.7' do
+ it 'should raise an error with invalid YAML and no default' do
+ is_expected.to run.with_params('["one"').
+ and_raise_error(ArgumentError)
+ end
+ end
+
+ context 'with incorrect YAML data' do
+ it 'should support a structure for a default value' do
+ is_expected.to run.with_params('', {'a' => '1'}).
+ and_return({'a' => '1'})
+ end
+
+ [1, 1.2, nil, true, false, [], {}, :yaml].each do |value|
+ it "should return the default value for an incorrect #{value.inspect} (#{value.class}) parameter" do
+ is_expected.to run.with_params(value, 'default_value').
+ and_return('default_value')
+ end
+ end
+
+ context 'when running on modern rubies', :unless => RUBY_VERSION == '1.8.7' do
+ ['---', '...', '*8', ''].each do |value|
+ it "should return the default value for an incorrect #{value.inspect} string parameter" do
+ is_expected.to run.with_params(value, 'default_value').
+ and_return('default_value')
+ end
+ end
+ end
+
+ end
+
end
diff --git a/spec/functions/pick_default_spec.rb b/spec/functions/pick_default_spec.rb
index db10cc3..e2fc64a 100755
--- a/spec/functions/pick_default_spec.rb
+++ b/spec/functions/pick_default_spec.rb
@@ -1,58 +1,24 @@
-#!/usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the pick_default function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- it "should exist" do
- expect(Puppet::Parser::Functions.function("pick_default")).to eq("function_pick_default")
- end
-
- it 'should return the correct value' do
- expect(scope.function_pick_default(['first', 'second'])).to eq('first')
- end
-
- it 'should return the correct value if the first value is empty' do
- expect(scope.function_pick_default(['', 'second'])).to eq('second')
- end
-
- it 'should skip empty string values' do
- expect(scope.function_pick_default(['', 'first'])).to eq('first')
- end
-
- it 'should skip :undef values' do
- expect(scope.function_pick_default([:undef, 'first'])).to eq('first')
- end
-
- it 'should skip :undefined values' do
- expect(scope.function_pick_default([:undefined, 'first'])).to eq('first')
- end
-
- it 'should return the empty string if it is the last possibility' do
- expect(scope.function_pick_default([:undef, :undefined, ''])).to eq('')
- end
-
- it 'should return :undef if it is the last possibility' do
- expect(scope.function_pick_default(['', :undefined, :undef])).to eq(:undef)
- end
-
- it 'should return :undefined if it is the last possibility' do
- expect(scope.function_pick_default([:undef, '', :undefined])).to eq(:undefined)
- end
-
- it 'should return the empty string if it is the only possibility' do
- expect(scope.function_pick_default([''])).to eq('')
- end
-
- it 'should return :undef if it is the only possibility' do
- expect(scope.function_pick_default([:undef])).to eq(:undef)
- end
-
- it 'should return :undefined if it is the only possibility' do
- expect(scope.function_pick_default([:undefined])).to eq(:undefined)
- end
-
- it 'should error if no values are passed' do
- expect { scope.function_pick_default([]) }.to raise_error(Puppet::Error, /Must receive at least one argument./)
+describe 'pick_default' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::Error, /Must receive at least one argument/) }
+
+ it { is_expected.to run.with_params('one', 'two').and_return('one') }
+ it { is_expected.to run.with_params('', 'two').and_return('two') }
+ it { is_expected.to run.with_params(:undef, 'two').and_return('two') }
+ it { is_expected.to run.with_params(:undefined, 'two').and_return('two') }
+ it { is_expected.to run.with_params(nil, 'two').and_return('two') }
+
+ [ '', :undef, :undefined, nil, {}, [], 1, 'default' ].each do |value|
+ describe "when providing #{value.inspect} as default" do
+ it { is_expected.to run.with_params('one', value).and_return('one') }
+ it { is_expected.to run.with_params([], value).and_return([]) }
+ it { is_expected.to run.with_params({}, value).and_return({}) }
+ it { is_expected.to run.with_params(value, value).and_return(value) }
+ it { is_expected.to run.with_params(:undef, value).and_return(value) }
+ it { is_expected.to run.with_params(:undefined, value).and_return(value) }
+ it { is_expected.to run.with_params(nil, value).and_return(value) }
+ end
end
end
diff --git a/spec/functions/pick_spec.rb b/spec/functions/pick_spec.rb
index 8be8f58..2c7caa8 100755
--- a/spec/functions/pick_spec.rb
+++ b/spec/functions/pick_spec.rb
@@ -1,34 +1,12 @@
-#!/usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the pick function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- it "should exist" do
- expect(Puppet::Parser::Functions.function("pick")).to eq("function_pick")
- end
-
- it 'should return the correct value' do
- expect(scope.function_pick(['first', 'second'])).to eq('first')
- end
-
- it 'should return the correct value if the first value is empty' do
- expect(scope.function_pick(['', 'second'])).to eq('second')
- end
-
- it 'should remove empty string values' do
- expect(scope.function_pick(['', 'first'])).to eq('first')
- end
-
- it 'should remove :undef values' do
- expect(scope.function_pick([:undef, 'first'])).to eq('first')
- end
-
- it 'should remove :undefined values' do
- expect(scope.function_pick([:undefined, 'first'])).to eq('first')
- end
-
- it 'should error if no values are passed' do
- expect { scope.function_pick([]) }.to( raise_error(Puppet::ParseError, "pick(): must receive at least one non empty value"))
- end
+describe 'pick' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /must receive at least one non empty value/) }
+ it { is_expected.to run.with_params('', nil, :undef, :undefined).and_raise_error(Puppet::ParseError, /must receive at least one non empty value/) }
+ it { is_expected.to run.with_params('one', 'two').and_return('one') }
+ it { is_expected.to run.with_params('', 'two').and_return('two') }
+ it { is_expected.to run.with_params(:undef, 'two').and_return('two') }
+ it { is_expected.to run.with_params(:undefined, 'two').and_return('two') }
+ it { is_expected.to run.with_params(nil, 'two').and_return('two') }
end
diff --git a/spec/functions/prefix_spec.rb b/spec/functions/prefix_spec.rb
index 34cac53..3761022 100755
--- a/spec/functions/prefix_spec.rb
+++ b/spec/functions/prefix_spec.rb
@@ -1,28 +1,28 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the prefix function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- it "raises a ParseError if there is less than 1 arguments" do
- expect { scope.function_prefix([]) }.to raise_error(Puppet::ParseError, /number of arguments/)
- end
-
- it "raises an error if the first argument is not an array" do
- expect {
- scope.function_prefix([Object.new])
- }.to raise_error(Puppet::ParseError, /expected first argument to be an Array/)
- end
-
-
- it "raises an error if the second argument is not a string" do
- expect {
- scope.function_prefix([['first', 'second'], 42])
- }.to raise_error(Puppet::ParseError, /expected second argument to be a String/)
- end
-
- it "returns a prefixed array" do
- result = scope.function_prefix([['a','b','c'], 'p'])
- expect(result).to(eq(['pa','pb','pc']))
- end
+describe 'prefix' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it {
+ pending("Current implementation ignores parameters after the second.")
+ is_expected.to run.with_params([], 'a', '').and_raise_error(Puppet::ParseError, /wrong number of arguments/i)
+ }
+ it { is_expected.to run.with_params('', '').and_raise_error(Puppet::ParseError, /expected first argument to be an Array or a Hash/) }
+ it { is_expected.to run.with_params([], 2).and_raise_error(Puppet::ParseError, /expected second argument to be a String/) }
+ it { is_expected.to run.with_params([]).and_return([]) }
+ it { is_expected.to run.with_params(['one', 2]).and_return(['one', '2']) }
+ it { is_expected.to run.with_params([], '').and_return([]) }
+ it { is_expected.to run.with_params([''], '').and_return(['']) }
+ it { is_expected.to run.with_params(['one'], 'pre').and_return(['preone']) }
+ it { is_expected.to run.with_params(['one', 'two', 'three'], 'pre').and_return(['preone', 'pretwo', 'prethree']) }
+ it { is_expected.to run.with_params({}).and_return({}) }
+ it { is_expected.to run.with_params({ 'key1' => 'value1', 2 => 3}).and_return({ 'key1' => 'value1', '2' => 3 }) }
+ it { is_expected.to run.with_params({}, '').and_return({}) }
+ it { is_expected.to run.with_params({ 'key' => 'value' }, '').and_return({ 'key' => 'value' }) }
+ it { is_expected.to run.with_params({ 'key' => 'value' }, 'pre').and_return({ 'prekey' => 'value' }) }
+ it {
+ is_expected.to run \
+ .with_params({ 'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3' }, 'pre') \
+ .and_return({ 'prekey1' => 'value1', 'prekey2' => 'value2', 'prekey3' => 'value3' })
+ }
end
diff --git a/spec/functions/private_spec.rb b/spec/functions/private_spec.rb
index c70759f..a13be64 100755..100644
--- a/spec/functions/private_spec.rb
+++ b/spec/functions/private_spec.rb
@@ -1,12 +1,13 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe Puppet::Parser::Functions.function(:private) do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- subject do
- function_name = Puppet::Parser::Functions.function(:private)
- scope.method(function_name)
+describe 'private' do
+ it 'should issue a warning' do
+ 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.")
+ begin
+ subject.call []
+ rescue
+ # ignore this
+ end
end
context "when called from inside module" do
diff --git a/spec/functions/pw_hash_spec.rb b/spec/functions/pw_hash_spec.rb
new file mode 100644
index 0000000..df5348c
--- /dev/null
+++ b/spec/functions/pw_hash_spec.rb
@@ -0,0 +1,69 @@
+require 'spec_helper'
+
+describe 'pw_hash' do
+
+ it { is_expected.not_to eq(nil) }
+
+ context 'when there are less than 3 arguments' do
+ it { is_expected.to run.with_params().and_raise_error(ArgumentError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params('password').and_raise_error(ArgumentError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params('password', 'sha-256').and_raise_error(ArgumentError, /wrong number of arguments/i) }
+ end
+
+ context 'when there are more than 3 arguments' do
+ it { is_expected.to run.with_params('password', 'sha-256', 'salt', 'extra').and_raise_error(ArgumentError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params('password', 'sha-256', 'salt', 'extra', 'extra').and_raise_error(ArgumentError, /wrong number of arguments/i) }
+ end
+
+ context 'when the first argument is not a string' do
+ it { is_expected.to run.with_params([], 'sha-256', 'salt').and_raise_error(ArgumentError, /first argument must be a string/) }
+ it { is_expected.to run.with_params({}, 'sha-256', 'salt').and_raise_error(ArgumentError, /first argument must be a string/) }
+ it { is_expected.to run.with_params(1, 'sha-256', 'salt').and_raise_error(ArgumentError, /first argument must be a string/) }
+ it { is_expected.to run.with_params(true, 'sha-256', 'salt').and_raise_error(ArgumentError, /first argument must be a string/) }
+ end
+
+ context 'when the first argument is undefined' do
+ it { is_expected.to run.with_params('', 'sha-256', 'salt').and_return(nil) }
+ it { is_expected.to run.with_params(nil, 'sha-256', 'salt').and_return(nil) }
+ end
+
+ context 'when the second argument is not a string' do
+ it { is_expected.to run.with_params('password', [], 'salt').and_raise_error(ArgumentError, /second argument must be a string/) }
+ it { is_expected.to run.with_params('password', {}, 'salt').and_raise_error(ArgumentError, /second argument must be a string/) }
+ it { is_expected.to run.with_params('password', 1, 'salt').and_raise_error(ArgumentError, /second argument must be a string/) }
+ it { is_expected.to run.with_params('password', true, 'salt').and_raise_error(ArgumentError, /second argument must be a string/) }
+ end
+
+ context 'when the second argument is not one of the supported hashing algorithms' do
+ it { is_expected.to run.with_params('password', 'no such algo', 'salt').and_raise_error(ArgumentError, /is not a valid hash type/) }
+ end
+
+ context 'when the third argument is not a string' do
+ it { is_expected.to run.with_params('password', 'sha-256', []).and_raise_error(ArgumentError, /third argument must be a string/) }
+ it { is_expected.to run.with_params('password', 'sha-256', {}).and_raise_error(ArgumentError, /third argument must be a string/) }
+ it { is_expected.to run.with_params('password', 'sha-256', 1).and_raise_error(ArgumentError, /third argument must be a string/) }
+ it { is_expected.to run.with_params('password', 'sha-256', true).and_raise_error(ArgumentError, /third argument must be a string/) }
+ end
+
+ context 'when the third argument is empty' do
+ it { is_expected.to run.with_params('password', 'sha-512', '').and_raise_error(ArgumentError, /third argument must not be empty/) }
+ end
+
+ context 'when the third argument contains invalid characters' do
+ it { is_expected.to run.with_params('password', 'sha-512', 'one%').and_raise_error(ArgumentError, /characters in salt must be in the set/) }
+ end
+
+ context 'when running on a platform with a weak String#crypt implementation' do
+ before(:each) { allow_any_instance_of(String).to receive(:crypt).with('$1$1').and_return('a bad hash') }
+
+ it { is_expected.to run.with_params('password', 'sha-512', 'salt').and_raise_error(Puppet::ParseError, /system does not support enhanced salts/) }
+ end
+
+ if RUBY_PLATFORM == 'java' or 'test'.crypt('$1$1') == '$1$1$Bp8CU9Oujr9SSEw53WV6G.'
+ describe "on systems with enhanced salts support" do
+ it { is_expected.to run.with_params('password', 'md5', 'salt').and_return('$1$salt$qJH7.N4xYta3aEG/dfqo/0') }
+ it { is_expected.to run.with_params('password', 'sha-256', 'salt').and_return('$5$salt$Gcm6FsVtF/Qa77ZKD.iwsJlCVPY0XSMgLJL0Hnww/c1') }
+ it { is_expected.to run.with_params('password', 'sha-512', 'salt').and_return('$6$salt$IxDD3jeSOb5eB1CX5LBsqZFVkJdido3OUILO5Ifz5iwMuTS4XMS130MTSuDDl3aCI6WouIL9AjRbLCelDCy.g.') }
+ end
+ end
+end
diff --git a/spec/functions/range_spec.rb b/spec/functions/range_spec.rb
index ef86f97..492cad4 100755
--- a/spec/functions/range_spec.rb
+++ b/spec/functions/range_spec.rb
@@ -1,86 +1,118 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the range function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+describe 'range' do
+ it { is_expected.not_to eq(nil) }
- it "exists" do
- expect(Puppet::Parser::Functions.function("range")).to eq("function_range")
+ describe 'signature validation in puppet3', :unless => RSpec.configuration.puppet_future do
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it {
+ pending("Current implementation ignores parameters after the third.")
+ is_expected.to run.with_params(1, 2, 3, 4).and_raise_error(Puppet::ParseError, /wrong number of arguments/i)
+ }
+ it { is_expected.to run.with_params('1..2..3').and_raise_error(Puppet::ParseError, /Unable to compute range/i) }
+ it { is_expected.to run.with_params('').and_raise_error(Puppet::ParseError, /Unknown range format/i) }
end
- it "raises a ParseError if there is less than 1 arguments" do
- expect { scope.function_range([]) }.to raise_error Puppet::ParseError, /Wrong number of arguments.*0 for 1/
+ describe 'signature validation in puppet4', :if => RSpec.configuration.puppet_future do
+ it { pending "the puppet 4 implementation"; is_expected.to run.with_params().and_raise_error(ArgumentError) }
+ it { pending "the puppet 4 implementation"; is_expected.to run.with_params('').and_raise_error(ArgumentError) }
+ it { pending "the puppet 4 implementation"; is_expected.to run.with_params({}).and_raise_error(ArgumentError) }
+ it { pending "the puppet 4 implementation"; is_expected.to run.with_params([]).and_raise_error(ArgumentError) }
+ it { pending "the puppet 4 implementation"; is_expected.to run.with_params(true).and_raise_error(ArgumentError) }
+ it { pending "the puppet 4 implementation"; is_expected.to run.with_params(true).and_raise_error(ArgumentError) }
+ it { is_expected.to run.with_params(1, 2, 'foo').and_raise_error(ArgumentError) }
+ it { pending "the puppet 4 implementation"; is_expected.to run.with_params(1, 2, []).and_raise_error(ArgumentError) }
+ it { pending "the puppet 4 implementation"; is_expected.to run.with_params(1, 2, {}).and_raise_error(ArgumentError) }
+ it { pending "the puppet 4 implementation"; is_expected.to run.with_params(1, 2, true).and_raise_error(ArgumentError) }
+ it { pending "the puppet 4 implementation"; is_expected.to run.with_params(1, 2, 3, 4).and_raise_error(ArgumentError) }
+ it { pending "the puppet 4 implementation"; is_expected.to run.with_params('1..2..3').and_raise_error(ArgumentError) }
end
- describe 'with a letter range' do
- it "returns a letter range" do
- result = scope.function_range(["a","d"])
- expect(result).to eq ['a','b','c','d']
- end
-
- it "returns a letter range given a step of 1" do
- result = scope.function_range(["a","d","1"])
- expect(result).to eq ['a','b','c','d']
- end
-
- it "returns a stepped letter range" do
- result = scope.function_range(["a","d","2"])
- expect(result).to eq ['a','c']
- end
+ context 'with characters as bounds' do
+ it { is_expected.to run.with_params('d', 'a').and_return([]) }
+ it { is_expected.to run.with_params('a', 'a').and_return(['a']) }
+ it { is_expected.to run.with_params('a', 'b').and_return(['a', 'b']) }
+ it { is_expected.to run.with_params('a', 'd').and_return(['a', 'b', 'c', 'd']) }
+ it { is_expected.to run.with_params('a', 'd', 1).and_return(['a', 'b', 'c', 'd']) }
+ it { is_expected.to run.with_params('a', 'd', '1').and_return(['a', 'b', 'c', 'd']) }
+ it { is_expected.to run.with_params('a', 'd', 2).and_return(['a', 'c']) }
+ it { is_expected.to run.with_params('a', 'd', -2).and_return(['a', 'c']) }
+ it { is_expected.to run.with_params('a', 'd', 3).and_return(['a', 'd']) }
+ it { is_expected.to run.with_params('a', 'd', 4).and_return(['a']) }
+ end
- it "returns a stepped letter range given a negative step" do
- result = scope.function_range(["a","d","-2"])
- expect(result).to eq ['a','c']
- end
+ context 'with strings as bounds' do
+ it { is_expected.to run.with_params('onea', 'oned').and_return(['onea', 'oneb', 'onec', 'oned']) }
+ it { is_expected.to run.with_params('two', 'one').and_return([]) }
+ it { is_expected.to run.with_params('true', 'false').and_return([]) }
+ it { is_expected.to run.with_params('false', 'true').and_return(['false']) }
end
- describe 'with a number range' do
- it "returns a number range" do
- result = scope.function_range(["1","4"])
- expect(result).to eq [1,2,3,4]
- end
+ context 'with integers as bounds' do
+ it { is_expected.to run.with_params(4, 1).and_return([]) }
+ it { is_expected.to run.with_params(1, 1).and_return([1]) }
+ it { is_expected.to run.with_params(1, 2).and_return([1, 2]) }
+ it { is_expected.to run.with_params(1, 4).and_return([1, 2, 3, 4]) }
+ it { is_expected.to run.with_params(1, 4, 1).and_return([1, 2, 3, 4]) }
+ it { is_expected.to run.with_params(1, 4, '1').and_return([1, 2, 3, 4]) }
+ it { is_expected.to run.with_params(1, 4, 2).and_return([1, 3]) }
+ it { is_expected.to run.with_params(1, 4, -2).and_return([1, 3]) }
+ it { is_expected.to run.with_params(1, 4, 3).and_return([1, 4]) }
+ it { is_expected.to run.with_params(1, 4, 4).and_return([1]) }
+ end
- it "returns a number range given a step of 1" do
- result = scope.function_range(["1","4","1"])
- expect(result).to eq [1,2,3,4]
- end
+ context 'with integers as strings as bounds' do
+ it { is_expected.to run.with_params('4', '1').and_return([]) }
+ it { is_expected.to run.with_params('1', '1').and_return([1]) }
+ it { is_expected.to run.with_params('1', '2').and_return([1, 2]) }
+ it { is_expected.to run.with_params('1', '4').and_return([1, 2, 3, 4]) }
+ it { is_expected.to run.with_params('1', '4', 1).and_return([1, 2, 3, 4]) }
+ it { is_expected.to run.with_params('1', '4', '1').and_return([1, 2, 3, 4]) }
+ it { is_expected.to run.with_params('1', '4', 2).and_return([1, 3]) }
+ it { is_expected.to run.with_params('1', '4', -2).and_return([1, 3]) }
+ it { is_expected.to run.with_params('1', '4', 3).and_return([1, 4]) }
+ it { is_expected.to run.with_params('1', '4', 4).and_return([1]) }
+ end
- it "returns a stepped number range" do
- result = scope.function_range(["1","4","2"])
- expect(result).to eq [1,3]
- end
+ context 'with prefixed numbers as strings as bounds' do
+ it { is_expected.to run.with_params('host01', 'host04').and_return(['host01', 'host02', 'host03', 'host04']) }
+ it { is_expected.to run.with_params('01', '04').and_return([1, 2, 3, 4]) }
+ end
- it "returns a stepped number range given a negative step" do
- result = scope.function_range(["1","4","-2"])
- expect(result).to eq [1,3]
- end
+ context 'with dash-range syntax' do
+ it { is_expected.to run.with_params('4-1').and_return([]) }
+ it { is_expected.to run.with_params('1-1').and_return([1]) }
+ it { is_expected.to run.with_params('1-2').and_return([1, 2]) }
+ it { is_expected.to run.with_params('1-4').and_return([1, 2, 3, 4]) }
end
- describe 'with a numeric-like string range' do
- it "works with padded hostname like strings" do
- expected = ("host01".."host10").to_a
- expect(scope.function_range(["host01","host10"])).to eq expected
- end
+ context 'with two-dot-range syntax' do
+ it { is_expected.to run.with_params('4..1').and_return([]) }
+ it { is_expected.to run.with_params('1..1').and_return([1]) }
+ it { is_expected.to run.with_params('1..2').and_return([1, 2]) }
+ it { is_expected.to run.with_params('1..4').and_return([1, 2, 3, 4]) }
+ end
- it "coerces zero padded digits to integers" do
- expected = (0..10).to_a
- expect(scope.function_range(["00", "10"])).to eq expected
- end
+ context 'with three-dot-range syntax' do
+ it { is_expected.to run.with_params('4...1').and_return([]) }
+ it { is_expected.to run.with_params('1...1').and_return([]) }
+ it { is_expected.to run.with_params('1...2').and_return([1]) }
+ it { is_expected.to run.with_params('1...3').and_return([1, 2]) }
+ it { is_expected.to run.with_params('1...5').and_return([1, 2, 3, 4]) }
end
- describe 'with a numeric range' do
- it "returns a range of numbers" do
- expected = (1..10).to_a
- expect(scope.function_range([1,10])).to eq expected
- end
- it "returns a range of numbers with step parameter" do
- expected = (1..10).step(2).to_a
- expect(scope.function_range([1,10,2])).to eq expected
- end
- it "works with mixed numeric like strings and numeric arguments" do
- expected = (1..10).to_a
- expect(scope.function_range(['1',10])).to eq expected
- expect(scope.function_range([1,'10'])).to eq expected
- end
+ describe 'when passing mixed arguments as bounds' do
+ it {
+ pending('these bounds should not be allowed as ruby will OOM hard. e.g. `(\'host0\'..\'hosta\').to_a` has 3239930 elements on ruby 1.9, adding more \'0\'s and \'a\'s increases that exponentially')
+ is_expected.to run.with_params('0', 'a').and_raise_error(Puppet::ParseError, /cannot interpolate between numeric and non-numeric bounds/)
+ }
+ it {
+ pending('these bounds should not be allowed as ruby will OOM hard. e.g. `(\'host0\'..\'hosta\').to_a` has 3239930 elements on ruby 1.9, adding more \'0\'s and \'a\'s increases that exponentially')
+ is_expected.to run.with_params(0, 'a').and_raise_error(Puppet::ParseError, /cannot interpolate between numeric and non-numeric bounds/)
+ }
+ it {
+ pending('these bounds should not be allowed as ruby will OOM hard. e.g. `(\'host0\'..\'hosta\').to_a` has 3239930 elements on ruby 1.9, adding more \'0\'s and \'a\'s increases that exponentially')
+ is_expected.to run.with_params('h0', 'ha').and_raise_error(Puppet::ParseError, /cannot interpolate between numeric and non-numeric bounds/)
+ }
end
end
diff --git a/spec/functions/regexpescape_spec.rb b/spec/functions/regexpescape_spec.rb
new file mode 100644
index 0000000..6efa847
--- /dev/null
+++ b/spec/functions/regexpescape_spec.rb
@@ -0,0 +1,36 @@
+require 'spec_helper'
+
+describe 'regexpescape' do
+ describe 'signature validation' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it {
+ pending("Current implementation ignores parameters after the first.")
+ is_expected.to run.with_params('', '').and_raise_error(Puppet::ParseError, /wrong number of arguments/i)
+ }
+ it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, /Requires either array or string to work/) }
+ it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, /Requires either array or string to work/) }
+ it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, /Requires either array or string to work/) }
+ end
+
+ describe 'handling normal strings' do
+ it 'should call ruby\'s Regexp.escape function' do
+ Regexp.expects(:escape).with('regexp_string').returns('escaped_regexp_string').once
+ is_expected.to run.with_params('regexp_string').and_return('escaped_regexp_string')
+ end
+ end
+
+ describe 'handling classes derived from String' do
+ it 'should call ruby\'s Regexp.escape function' do
+ regexp_string = AlsoString.new('regexp_string')
+ Regexp.expects(:escape).with(regexp_string).returns('escaped_regexp_string').once
+ is_expected.to run.with_params(regexp_string).and_return("escaped_regexp_string")
+ end
+ end
+
+ describe 'strings in arrays handling' do
+ it { is_expected.to run.with_params([]).and_return([]) }
+ it { is_expected.to run.with_params(['one*', "two"]).and_return(['one\*', "two"]) }
+ it { is_expected.to run.with_params(['one*', 1, true, {}, "two"]).and_return(['one\*', 1, true, {}, "two"]) }
+ end
+end
diff --git a/spec/functions/reject_spec.rb b/spec/functions/reject_spec.rb
index 88a992e..4863050 100755
--- a/spec/functions/reject_spec.rb
+++ b/spec/functions/reject_spec.rb
@@ -1,20 +1,19 @@
-#!/usr/bin/env ruby
-
require 'spec_helper'
-describe "the reject function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- it "should exist" do
- expect(Puppet::Parser::Functions.function("reject")).to eq("function_reject")
- end
-
- it "should raise a ParseError if there is less than 1 arguments" do
- expect { scope.function_reject([]) }.to( raise_error(Puppet::ParseError))
- end
+describe 'reject' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params([], 'pattern', 'extra').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
- it "should reject contents from an array" do
- result = scope.function_reject([["1111", "aaabbb","bbbccc","dddeee"], "bbb"])
- expect(result).to(eq(["1111", "dddeee"]))
- end
+ it {
+ pending("reject does not actually check this, and raises NoMethodError instead")
+ is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError, /first argument not an array/)
+ }
+ it {
+ pending("reject does not actually check this, and raises NoMethodError instead")
+ is_expected.to run.with_params(1, 'two').and_raise_error(Puppet::ParseError, /first argument not an array/)
+ }
+ it { is_expected.to run.with_params([], 'two').and_return([]) }
+ it { is_expected.to run.with_params(['one', 'two', 'three'], 'two').and_return(['one', 'three']) }
+ it { is_expected.to run.with_params(['one', 'two', 'three'], 't(wo|hree)').and_return(['one']) }
end
diff --git a/spec/functions/reverse_spec.rb b/spec/functions/reverse_spec.rb
index 1f04794..e00dee9 100755
--- a/spec/functions/reverse_spec.rb
+++ b/spec/functions/reverse_spec.rb
@@ -1,28 +1,31 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the reverse function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+describe 'reverse' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it {
+ pending("Current implementation ignores parameters after the first.")
+ is_expected.to run.with_params([], 'extra').and_raise_error(Puppet::ParseError, /wrong number of arguments/i)
+ }
+ it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, /Requires either array or string to work/) }
+ it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, /Requires either array or string to work/) }
+ it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, /Requires either array or string to work/) }
+ it { is_expected.to run.with_params([]).and_return([]) }
+ it { is_expected.to run.with_params(['a']).and_return(['a']) }
+ it { is_expected.to run.with_params(['one']).and_return(['one']) }
+ it { is_expected.to run.with_params(['one', 'two', 'three']).and_return(['three', 'two', 'one']) }
+ it { is_expected.to run.with_params(['one', 'two', 'three', 'four']).and_return(['four', 'three', 'two', 'one']) }
- it "should exist" do
- expect(Puppet::Parser::Functions.function("reverse")).to eq("function_reverse")
- end
-
- it "should raise a ParseError if there is less than 1 arguments" do
- expect { scope.function_reverse([]) }.to( raise_error(Puppet::ParseError))
- end
+ it { is_expected.to run.with_params('').and_return('') }
+ it { is_expected.to run.with_params('a').and_return('a') }
+ it { is_expected.to run.with_params('abc').and_return('cba') }
+ it { is_expected.to run.with_params('abcd').and_return('dcba') }
- it "should reverse a string" do
- result = scope.function_reverse(["asdfghijkl"])
- expect(result).to(eq('lkjihgfdsa'))
- end
-
- it "should accept objects which extend String" do
- class AlsoString < String
+ context 'when using a class extending String' do
+ it 'should call its reverse method' do
+ value = AlsoString.new('asdfghjkl')
+ value.expects(:reverse).returns('foo')
+ expect(subject).to run.with_params(value).and_return('foo')
end
-
- value = AlsoString.new('asdfghjkl')
- result = scope.function_reverse([value])
- result.should(eq('lkjhgfdsa'))
end
end
diff --git a/spec/functions/rstrip_spec.rb b/spec/functions/rstrip_spec.rb
index f6b4838..d2efac8 100755
--- a/spec/functions/rstrip_spec.rb
+++ b/spec/functions/rstrip_spec.rb
@@ -1,33 +1,34 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the rstrip function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- it "should exist" do
- expect(Puppet::Parser::Functions.function("rstrip")).to eq("function_rstrip")
- end
-
- it "should raise a ParseError if there is less than 1 arguments" do
- expect { scope.function_rstrip([]) }.to( raise_error(Puppet::ParseError))
- end
-
- it "should rstrip a string" do
- result = scope.function_rstrip(["asdf "])
- expect(result).to(eq('asdf'))
- end
-
- it "should rstrip each element in an array" do
- result = scope.function_rstrip([["a ","b ", "c "]])
- expect(result).to(eq(['a','b','c']))
- end
-
- it "should accept objects which extend String" do
- class AlsoString < String
- end
-
- value = AlsoString.new('asdf ')
- result = scope.function_rstrip([value])
- result.should(eq('asdf'))
- end
+describe 'rstrip' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it {
+ pending("Current implementation ignores parameters after the first.")
+ is_expected.to run.with_params('', '').and_raise_error(Puppet::ParseError, /wrong number of arguments/i)
+ }
+ it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, /Requires either array or string to work with/) }
+ it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, /Requires either array or string to work with/) }
+ it { is_expected.to run.with_params('').and_return('') }
+ it { is_expected.to run.with_params(' ').and_return('') }
+ it { is_expected.to run.with_params(' ').and_return('') }
+ it { is_expected.to run.with_params("\t").and_return('') }
+ it { is_expected.to run.with_params("\t ").and_return('') }
+ it { is_expected.to run.with_params('one').and_return('one') }
+ it { is_expected.to run.with_params(' one').and_return(' one') }
+ it { is_expected.to run.with_params(' one').and_return(' one') }
+ it { is_expected.to run.with_params("\tone").and_return("\tone") }
+ it { is_expected.to run.with_params("\t one").and_return("\t one") }
+ it { is_expected.to run.with_params('one ').and_return('one') }
+ it { is_expected.to run.with_params(' one ').and_return(' one') }
+ it { is_expected.to run.with_params(' one ').and_return(' one') }
+ it { is_expected.to run.with_params("\tone ").and_return("\tone") }
+ it { is_expected.to run.with_params("\t one ").and_return("\t one") }
+ it { is_expected.to run.with_params("one\t").and_return('one') }
+ it { is_expected.to run.with_params(" one\t").and_return(' one') }
+ it { is_expected.to run.with_params(" one\t").and_return(' one') }
+ it { is_expected.to run.with_params("\tone\t").and_return("\tone") }
+ it { is_expected.to run.with_params("\t one\t").and_return("\t one") }
+ it { is_expected.to run.with_params(' o n e ').and_return(' o n e') }
+ it { is_expected.to run.with_params(AlsoString.new(' one ')).and_return(' one') }
end
diff --git a/spec/functions/seeded_rand_spec.rb b/spec/functions/seeded_rand_spec.rb
new file mode 100644
index 0000000..38e134e
--- /dev/null
+++ b/spec/functions/seeded_rand_spec.rb
@@ -0,0 +1,53 @@
+require 'spec_helper'
+
+describe 'seeded_rand' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(ArgumentError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params(1).and_raise_error(ArgumentError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params(0, '').and_raise_error(ArgumentError, /first argument must be a positive integer/) }
+ it { is_expected.to run.with_params(1.5, '').and_raise_error(ArgumentError, /first argument must be a positive integer/) }
+ it { is_expected.to run.with_params(-10, '').and_raise_error(ArgumentError, /first argument must be a positive integer/) }
+ it { is_expected.to run.with_params("-10", '').and_raise_error(ArgumentError, /first argument must be a positive integer/) }
+ it { is_expected.to run.with_params("string", '').and_raise_error(ArgumentError, /first argument must be a positive integer/) }
+ it { is_expected.to run.with_params([], '').and_raise_error(ArgumentError, /first argument must be a positive integer/) }
+ it { is_expected.to run.with_params({}, '').and_raise_error(ArgumentError, /first argument must be a positive integer/) }
+ it { is_expected.to run.with_params(1, 1).and_raise_error(ArgumentError, /second argument must be a string/) }
+ it { is_expected.to run.with_params(1, []).and_raise_error(ArgumentError, /second argument must be a string/) }
+ it { is_expected.to run.with_params(1, {}).and_raise_error(ArgumentError, /second argument must be a string/) }
+
+ it "provides a random number strictly less than the given max" do
+ expect(seeded_rand(3, 'seed')).to satisfy {|n| n.to_i < 3 }
+ end
+
+ it "provides a random number greater or equal to zero" do
+ expect(seeded_rand(3, 'seed')).to satisfy {|n| n.to_i >= 0 }
+ end
+
+ it "provides the same 'random' value on subsequent calls for the same host" do
+ expect(seeded_rand(10, 'seed')).to eql(seeded_rand(10, 'seed'))
+ end
+
+ it "allows seed to control the random value on a single host" do
+ first_random = seeded_rand(1000, 'seed1')
+ second_different_random = seeded_rand(1000, 'seed2')
+
+ expect(first_random).not_to eql(second_different_random)
+ end
+
+ it "should not return different values for different hosts" do
+ val1 = seeded_rand(1000, 'foo', :host => "first.host.com")
+ val2 = seeded_rand(1000, 'foo', :host => "second.host.com")
+
+ expect(val1).to eql(val2)
+ end
+
+ def seeded_rand(max, seed, args = {})
+ host = args[:host] || '127.0.0.1'
+
+ # workaround not being able to use let(:facts) because some tests need
+ # multiple different hostnames in one context
+ scope.stubs(:lookupvar).with("::fqdn", {}).returns(host)
+
+ scope.function_seeded_rand([max, seed])
+ end
+end
diff --git a/spec/functions/shell_escape_spec.rb b/spec/functions/shell_escape_spec.rb
new file mode 100644
index 0000000..3061dec
--- /dev/null
+++ b/spec/functions/shell_escape_spec.rb
@@ -0,0 +1,22 @@
+require 'spec_helper'
+
+describe 'shell_escape' do
+ it { is_expected.not_to eq(nil) }
+
+ describe 'signature validation' do
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params('foo', 'bar').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ end
+
+ describe 'stringification' do
+ it { is_expected.to run.with_params(10).and_return('10') }
+ it { is_expected.to run.with_params(false).and_return('false') }
+ end
+
+ describe 'escaping' do
+ it { is_expected.to run.with_params('foo').and_return('foo') }
+ it { is_expected.to run.with_params('foo bar').and_return('foo\ bar') }
+ it { is_expected.to run.with_params('~`!@#$%^&*()_+-=[]\{}|;\':",./<>?')
+ .and_return('\~\`\!@\#\$\%\^\&\*\(\)_\+-\=\[\]\\\\\{\}\|\;\\\':\",./\<\>\?') }
+ end
+end
diff --git a/spec/functions/shell_join_spec.rb b/spec/functions/shell_join_spec.rb
new file mode 100644
index 0000000..6815f7c
--- /dev/null
+++ b/spec/functions/shell_join_spec.rb
@@ -0,0 +1,23 @@
+require 'spec_helper'
+
+describe 'shell_join' do
+ it { is_expected.not_to eq(nil) }
+
+ describe 'signature validation' do
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params(['foo'], ['bar']).and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params('foo').and_raise_error(Puppet::ParseError, /is not an Array/i) }
+ end
+
+ describe 'shell argument joining' do
+ it { is_expected.to run.with_params(['foo']).and_return('foo') }
+ it { is_expected.to run.with_params(['foo', 'bar']).and_return('foo bar') }
+ it { is_expected.to run.with_params(['foo', 'bar baz']).and_return('foo bar\ baz') }
+ it { is_expected.to run.with_params(['~`!@#$', '%^&*()_+-=', '[]\{}|;\':"', ',./<>?'])
+ .and_return('\~\`\!@\#\$ \%\^\&\*\(\)_\+-\= \[\]\\\\\{\}\|\;\\\':\" ,./\<\>\?') }
+ end
+
+ describe 'stringification' do
+ it { is_expected.to run.with_params([10, false, 'foo']).and_return('10 false foo') }
+ end
+end
diff --git a/spec/functions/shell_split_spec.rb b/spec/functions/shell_split_spec.rb
new file mode 100644
index 0000000..beeb977
--- /dev/null
+++ b/spec/functions/shell_split_spec.rb
@@ -0,0 +1,24 @@
+require 'spec_helper'
+
+describe 'shell_split' do
+ it { is_expected.not_to eq(nil) }
+
+ describe 'signature validation' do
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params('foo', 'bar').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ end
+
+ describe 'stringification' do
+ it { is_expected.to run.with_params(10).and_return(['10']) }
+ it { is_expected.to run.with_params(false).and_return(['false']) }
+ end
+
+ describe 'shell line spliting' do
+ it { is_expected.to run.with_params('foo').and_return(['foo']) }
+ it { is_expected.to run.with_params('foo bar').and_return(['foo', 'bar']) }
+ it { is_expected.to run.with_params('\~\`\!@\#\$\%\^\&\*\(\)_\+-\=\[\]\\\\\{\}\|\;\\\':\",./\<\>\?')
+ .and_return(['~`!@#$%^&*()_+-=[]\{}|;\':",./<>?']) }
+ it { is_expected.to run.with_params('\~\`\!@\#\$ \%\^\&\*\(\)_\+-\= \[\]\\\\\{\}\|\;\\\':\" ,./\<\>\?')
+ .and_return(['~`!@#$', '%^&*()_+-=', '[]\{}|;\':"', ',./<>?']) }
+ end
+end
diff --git a/spec/functions/shuffle_spec.rb b/spec/functions/shuffle_spec.rb
index a62c1fb..ebc3a73 100755
--- a/spec/functions/shuffle_spec.rb
+++ b/spec/functions/shuffle_spec.rb
@@ -1,33 +1,33 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the shuffle function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+describe 'shuffle' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it {
+ pending("Current implementation ignores parameters after the first.")
+ is_expected.to run.with_params([], 'extra').and_raise_error(Puppet::ParseError, /wrong number of arguments/i)
+ }
+ it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, /Requires either array or string to work/) }
+ it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, /Requires either array or string to work/) }
+ it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, /Requires either array or string to work/) }
- it "should exist" do
- expect(Puppet::Parser::Functions.function("shuffle")).to eq("function_shuffle")
- end
-
- it "should raise a ParseError if there is less than 1 arguments" do
- expect { scope.function_shuffle([]) }.to( raise_error(Puppet::ParseError))
- end
+ context 'when running with a specific seed' do
+ # make tests deterministic
+ before(:each) { srand(2) }
- it "should shuffle a string and the result should be the same size" do
- result = scope.function_shuffle(["asdf"])
- expect(result.size).to(eq(4))
- end
+ it { is_expected.to run.with_params([]).and_return([]) }
+ it { is_expected.to run.with_params(['a']).and_return(['a']) }
+ it { is_expected.to run.with_params(['one']).and_return(['one']) }
+ it { is_expected.to run.with_params(['one', 'two', 'three']).and_return(['two', 'one', 'three']) }
+ it { is_expected.to run.with_params(['one', 'two', 'three', 'four']).and_return(['four', 'three', 'two', 'one']) }
- it "should shuffle a string but the sorted contents should still be the same" do
- result = scope.function_shuffle(["adfs"])
- expect(result.split("").sort.join("")).to(eq("adfs"))
- end
+ it { is_expected.to run.with_params('').and_return('') }
+ it { is_expected.to run.with_params('a').and_return('a') }
+ it { is_expected.to run.with_params('abc').and_return('bac') }
+ it { is_expected.to run.with_params('abcd').and_return('dcba') }
- it "should accept objects which extend String" do
- class AlsoString < String
+ context 'when using a class extending String' do
+ it { is_expected.to run.with_params(AlsoString.new('asdfghjkl')).and_return('lkhdsfajg') }
end
-
- value = AlsoString.new('asdf')
- result = scope.function_shuffle([value])
- result.size.should(eq(4))
end
end
diff --git a/spec/functions/size_spec.rb b/spec/functions/size_spec.rb
index 18eb48f..c0047ee 100755
--- a/spec/functions/size_spec.rb
+++ b/spec/functions/size_spec.rb
@@ -1,24 +1,35 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the size function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+describe 'size' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it {
+ pending("Current implementation ignores parameters after the first.")
+ is_expected.to run.with_params([], 'extra').and_raise_error(Puppet::ParseError, /wrong number of arguments/i)
+ }
+ it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, /Unknown type given/) }
+ it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, /Unknown type given/) }
+ it { is_expected.to run.with_params('1').and_raise_error(Puppet::ParseError, /Requires either string, array or hash to work/) }
+ it { is_expected.to run.with_params('1.0').and_raise_error(Puppet::ParseError, /Requires either string, array or hash to work/) }
+ it { is_expected.to run.with_params([]).and_return(0) }
+ it { is_expected.to run.with_params(['a']).and_return(1) }
+ it { is_expected.to run.with_params(['one', 'two', 'three']).and_return(3) }
+ it { is_expected.to run.with_params(['one', 'two', 'three', 'four']).and_return(4) }
- it "should exist" do
- expect(Puppet::Parser::Functions.function("size")).to eq("function_size")
- end
-
- it "should raise a ParseError if there is less than 1 arguments" do
- expect { scope.function_size([]) }.to( raise_error(Puppet::ParseError))
- end
+ it { is_expected.to run.with_params({}).and_return(0) }
+ it { is_expected.to run.with_params({'1' => '2'}).and_return(1) }
+ it { is_expected.to run.with_params({'1' => '2', '4' => '4'}).and_return(2) }
- it "should return the size of a string" do
- result = scope.function_size(["asdf"])
- expect(result).to(eq(4))
- end
+ it { is_expected.to run.with_params('').and_return(0) }
+ it { is_expected.to run.with_params('a').and_return(1) }
+ it { is_expected.to run.with_params('abc').and_return(3) }
+ it { is_expected.to run.with_params('abcd').and_return(4) }
- it "should return the size of an array" do
- result = scope.function_size([["a","b","c"]])
- expect(result).to(eq(3))
+ context 'when using a class extending String' do
+ it 'should call its size method' do
+ value = AlsoString.new('asdfghjkl')
+ value.expects(:size).returns('foo')
+ expect(subject).to run.with_params(value).and_return('foo')
+ end
end
end
diff --git a/spec/functions/sort_spec.rb b/spec/functions/sort_spec.rb
index 4c2a66c..9abd039 100755
--- a/spec/functions/sort_spec.rb
+++ b/spec/functions/sort_spec.rb
@@ -1,24 +1,24 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the sort function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- it "should exist" do
- expect(Puppet::Parser::Functions.function("sort")).to eq("function_sort")
- end
-
- it "should raise a ParseError if there is not 1 arguments" do
- expect { scope.function_sort(['','']) }.to( raise_error(Puppet::ParseError))
+describe 'sort' do
+ describe 'signature validation' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params([], 'extra').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { pending('stricter input checking'); is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, /requires string or array/) }
+ it { pending('stricter input checking'); is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, /requires string or array/) }
+ it { pending('stricter input checking'); is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, /requires string or array/) }
end
- it "should sort an array" do
- result = scope.function_sort([["a","c","b"]])
- expect(result).to(eq(['a','b','c']))
+ context 'when called with an array' do
+ it { is_expected.to run.with_params([]).and_return([]) }
+ it { is_expected.to run.with_params(['a']).and_return(['a']) }
+ it { is_expected.to run.with_params(['c', 'b', 'a']).and_return(['a', 'b', 'c']) }
end
- it "should sort a string" do
- result = scope.function_sort(["acb"])
- expect(result).to(eq('abc'))
+ context 'when called with a string' do
+ it { is_expected.to run.with_params('').and_return('') }
+ it { is_expected.to run.with_params('a').and_return('a') }
+ it { is_expected.to run.with_params('cbda').and_return('abcd') }
end
end
diff --git a/spec/functions/squeeze_spec.rb b/spec/functions/squeeze_spec.rb
index cd0eb37..7f09c30 100755
--- a/spec/functions/squeeze_spec.rb
+++ b/spec/functions/squeeze_spec.rb
@@ -1,24 +1,44 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the squeeze function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+describe 'squeeze' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params('', '', '').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params(1).and_raise_error(NoMethodError) }
+ it { is_expected.to run.with_params({}).and_raise_error(NoMethodError) }
+ it { is_expected.to run.with_params(true).and_raise_error(NoMethodError) }
- it "should exist" do
- expect(Puppet::Parser::Functions.function("squeeze")).to eq("function_squeeze")
+ context 'when squeezing a single string' do
+ it { is_expected.to run.with_params('').and_return('') }
+ it { is_expected.to run.with_params('a').and_return('a') }
+ it { is_expected.to run.with_params('aaaaaaaaa').and_return('a') }
+ it { is_expected.to run.with_params('aaaaaaaaa', 'a').and_return('a') }
+ it { is_expected.to run.with_params('aaaaaaaaabbbbbbbbbbcccccccccc', 'b-c').and_return('aaaaaaaaabc') }
end
- it "should raise a ParseError if there is less than 2 arguments" do
- expect { scope.function_squeeze([]) }.to( raise_error(Puppet::ParseError))
+ context 'when squeezing values in an array' do
+ it {
+ is_expected.to run \
+ .with_params(['', 'a', 'aaaaaaaaa', 'aaaaaaaaabbbbbbbbbbcccccccccc']) \
+ .and_return( ['', 'a', 'a', 'abc'])
+ }
+ it {
+ is_expected.to run \
+ .with_params(['', 'a', 'aaaaaaaaa', 'aaaaaaaaabbbbbbbbbbcccccccccc'], 'a') \
+ .and_return( ['', 'a', 'a', 'abbbbbbbbbbcccccccccc'])
+ }
+ it {
+ is_expected.to run \
+ .with_params(['', 'a', 'aaaaaaaaa', 'aaaaaaaaabbbbbbbbbbcccccccccc'], 'b-c') \
+ .and_return( ['', 'a', 'aaaaaaaaa', 'aaaaaaaaabc'])
+ }
end
- it "should squeeze a string" do
- result = scope.function_squeeze(["aaabbbbcccc"])
- expect(result).to(eq('abc'))
- end
-
- it "should squeeze all elements in an array" do
- result = scope.function_squeeze([["aaabbbbcccc","dddfff"]])
- expect(result).to(eq(['abc','df']))
+ context 'when using a class extending String' do
+ it 'should call its squeeze method' do
+ value = AlsoString.new('aaaaaaaaa')
+ value.expects(:squeeze).returns('foo')
+ expect(subject).to run.with_params(value).and_return('foo')
+ end
end
end
diff --git a/spec/functions/str2bool_spec.rb b/spec/functions/str2bool_spec.rb
index 1d205d7..7d8c47c 100755
--- a/spec/functions/str2bool_spec.rb
+++ b/spec/functions/str2bool_spec.rb
@@ -1,31 +1,23 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the str2bool function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+describe 'str2bool' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it {
+ pending("Current implementation ignores parameters after the first.")
+ is_expected.to run.with_params('true', 'extra').and_raise_error(Puppet::ParseError, /wrong number of arguments/i)
+ }
+ it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, /Unknown type of boolean given/) }
- it "should exist" do
- expect(Puppet::Parser::Functions.function("str2bool")).to eq("function_str2bool")
+ describe 'when testing values that mean "true"' do
+ [ 'TRUE','1', 't', 'y', 'true', 'yes', true ].each do |value|
+ it { is_expected.to run.with_params(value).and_return(true) }
+ end
end
- it "should raise a ParseError if there is less than 1 arguments" do
- expect { scope.function_str2bool([]) }.to( raise_error(Puppet::ParseError))
- end
-
- it "should convert string 'true' to true" do
- result = scope.function_str2bool(["true"])
- expect(result).to(eq(true))
- end
-
- it "should convert string 'undef' to false" do
- result = scope.function_str2bool(["undef"])
- expect(result).to(eq(false))
- end
-
- it "should return the boolean it was called with" do
- result = scope.function_str2bool([true])
- expect(result).to(eq(true))
- result = scope.function_str2bool([false])
- expect(result).to(eq(false))
+ describe 'when testing values that mean "false"' do
+ [ 'FALSE','', '0', 'f', 'n', 'false', 'no', false, 'undef', 'undefined' ].each do |value|
+ it { is_expected.to run.with_params(value).and_return(false) }
+ end
end
end
diff --git a/spec/functions/str2saltedsha512_spec.rb b/spec/functions/str2saltedsha512_spec.rb
index ab7f57f..2e1e818 100755
--- a/spec/functions/str2saltedsha512_spec.rb
+++ b/spec/functions/str2saltedsha512_spec.rb
@@ -1,45 +1,17 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the str2saltedsha512 function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+describe 'str2saltedsha512' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params('password', 2).and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, /Requires a String argument/) }
- it "should exist" do
- expect(Puppet::Parser::Functions.function("str2saltedsha512")).to eq("function_str2saltedsha512")
- end
-
- it "should raise a ParseError if there is less than 1 argument" do
- expect { scope.function_str2saltedsha512([]) }.to( raise_error(Puppet::ParseError) )
- end
-
- it "should raise a ParseError if there is more than 1 argument" do
- expect { scope.function_str2saltedsha512(['foo', 'bar', 'baz']) }.to( raise_error(Puppet::ParseError) )
- end
-
- it "should return a salted-sha512 password hash 136 characters in length" do
- result = scope.function_str2saltedsha512(["password"])
- expect(result.length).to(eq(136))
- end
-
- it "should raise an error if you pass a non-string password" do
- expect { scope.function_str2saltedsha512([1234]) }.to( raise_error(Puppet::ParseError) )
- end
-
- it "should generate a valid password" do
- # Allow the function to generate a password based on the string 'password'
- password_hash = scope.function_str2saltedsha512(["password"])
-
- # Separate the Salt and Password from the Password Hash
- salt = password_hash[0..7]
- password = password_hash[8..-1]
-
- # Convert the Salt and Password from Hex to Binary Data
- str_salt = Array(salt.lines).pack('H*')
- str_password = Array(password.lines).pack('H*')
+ context 'when running with a specific seed' do
+ # make tests deterministic
+ before(:each) { srand(2) }
- # Combine the Binary Salt with 'password' and compare the end result
- saltedpass = Digest::SHA512.digest(str_salt + 'password')
- result = (str_salt + saltedpass).unpack('H*')[0]
- expect(result).to eq(password_hash)
+ it { is_expected.to run.with_params('').and_return('0f8a612f4eeed08e47b3875d00f33c5688f7926298f2d9b5fe19d1323f910bc78b6f7b5892596d2fabaa65e7a8d99b3768c102610cf0432c4827eee01f09451e3fae4f7a') }
+ it { is_expected.to run.with_params('password').and_return('0f8a612f43134376566c5707718d600effcfb17581fc9d3fa64d7f447dfda317c174ffdb498d2c5bd5c2075dab41c9d7ada5afbdc6b55354980eb5ba61802371e6b64956') }
+ it { is_expected.to run.with_params('verylongpassword').and_return('0f8a612f7a448537540e062daa8621f9bae326ca8ccb899e1bdb10e7c218cebfceae2530b856662565fdc4d81e986fc50cfbbc46d50436610ed9429ff5e43f2c45b5d039') }
end
end
diff --git a/spec/functions/strftime_spec.rb b/spec/functions/strftime_spec.rb
index ebec54b..e76774a 100755
--- a/spec/functions/strftime_spec.rb
+++ b/spec/functions/strftime_spec.rb
@@ -1,9 +1,6 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the strftime function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
+describe 'strftime' do
it "should exist" do
expect(Puppet::Parser::Functions.function("strftime")).to eq("function_strftime")
end
diff --git a/spec/functions/strip_spec.rb b/spec/functions/strip_spec.rb
index 4ac8daf..689b6dd 100755
--- a/spec/functions/strip_spec.rb
+++ b/spec/functions/strip_spec.rb
@@ -1,27 +1,34 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the strip function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
- it "should exist" do
- expect(Puppet::Parser::Functions.function("strip")).to eq("function_strip")
- end
-
- it "should raise a ParseError if there is less than 1 arguments" do
- expect { scope.function_strip([]) }.to( raise_error(Puppet::ParseError))
- end
-
- it "should strip a string" do
- result = scope.function_strip([" ab cd "])
- expect(result).to(eq('ab cd'))
- end
-
- it "should accept objects which extend String" do
- class AlsoString < String
- end
-
- value = AlsoString.new(' as df ')
- result = scope.function_strip([value])
- result.should(eq('as df'))
- end
+describe 'strip' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it {
+ pending("Current implementation ignores parameters after the first.")
+ is_expected.to run.with_params('', '').and_raise_error(Puppet::ParseError, /wrong number of arguments/i)
+ }
+ it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, /Requires either array or string to work with/) }
+ it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, /Requires either array or string to work with/) }
+ it { is_expected.to run.with_params('').and_return('') }
+ it { is_expected.to run.with_params(' ').and_return('') }
+ it { is_expected.to run.with_params(' ').and_return('') }
+ it { is_expected.to run.with_params("\t").and_return('') }
+ it { is_expected.to run.with_params("\t ").and_return('') }
+ it { is_expected.to run.with_params('one').and_return('one') }
+ it { is_expected.to run.with_params(' one').and_return('one') }
+ it { is_expected.to run.with_params(' one').and_return('one') }
+ it { is_expected.to run.with_params("\tone").and_return('one') }
+ it { is_expected.to run.with_params("\t one").and_return('one') }
+ it { is_expected.to run.with_params('one ').and_return('one') }
+ it { is_expected.to run.with_params(' one ').and_return('one') }
+ it { is_expected.to run.with_params(' one ').and_return('one') }
+ it { is_expected.to run.with_params("\tone ").and_return('one') }
+ it { is_expected.to run.with_params("\t one ").and_return('one') }
+ it { is_expected.to run.with_params("one \t").and_return('one') }
+ it { is_expected.to run.with_params(" one \t").and_return('one') }
+ it { is_expected.to run.with_params(" one \t").and_return('one') }
+ it { is_expected.to run.with_params("\tone \t").and_return('one') }
+ it { is_expected.to run.with_params("\t one \t").and_return('one') }
+ it { is_expected.to run.with_params(' o n e ').and_return('o n e') }
+ it { is_expected.to run.with_params(AlsoString.new(' one ')).and_return('one') }
end
diff --git a/spec/functions/suffix_spec.rb b/spec/functions/suffix_spec.rb
index c7783c6..efba4ab 100755
--- a/spec/functions/suffix_spec.rb
+++ b/spec/functions/suffix_spec.rb
@@ -1,27 +1,38 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the suffix function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- it "raises a ParseError if there is less than 1 arguments" do
- expect { scope.function_suffix([]) }.to raise_error(Puppet::ParseError, /number of arguments/)
- end
-
- it "raises an error if the first argument is not an array" do
- expect {
- scope.function_suffix([Object.new])
- }.to raise_error(Puppet::ParseError, /expected first argument to be an Array/)
- end
-
- it "raises an error if the second argument is not a string" do
- expect {
- scope.function_suffix([['first', 'second'], 42])
- }.to raise_error(Puppet::ParseError, /expected second argument to be a String/)
- end
-
- it "returns a suffixed array" do
- result = scope.function_suffix([['a','b','c'], 'p'])
- expect(result).to(eq(['ap','bp','cp']))
- end
+describe 'suffix' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it {
+ pending("Current implementation ignores parameters after the second.")
+ is_expected.to run.with_params([], 'a', '').and_raise_error(Puppet::ParseError, /wrong number of arguments/i)
+ }
+ it { is_expected.to run.with_params('', '').and_raise_error(Puppet::ParseError, /expected first argument to be an Array/) }
+ it { is_expected.to run.with_params([], 2).and_raise_error(Puppet::ParseError, /expected second argument to be a String/) }
+ it { is_expected.to run.with_params([]).and_return([]) }
+ it { is_expected.to run.with_params(['one', 2]).and_return(['one', '2']) }
+ it { is_expected.to run.with_params([], '').and_return([]) }
+ it { is_expected.to run.with_params([''], '').and_return(['']) }
+ it { is_expected.to run.with_params(['one'], 'post').and_return(['onepost']) }
+ it { is_expected.to run.with_params(['one', 'two', 'three'], 'post').and_return(['onepost', 'twopost', 'threepost']) }
+ it {
+ is_expected.to run.with_params({}).and_return({})
+ }
+ it {
+ is_expected.to run.with_params({ 'key1' => 'value1', 2 => 3}).and_return({ 'key1' => 'value1', '2' => 3 })
+ }
+ it {
+ is_expected.to run.with_params({}, '').and_return({})
+ }
+ it {
+ is_expected.to run.with_params({ 'key' => 'value' }, '').and_return({ 'key' => 'value' })
+ }
+ it {
+ is_expected.to run.with_params({ 'key' => 'value' }, 'post').and_return({ 'keypost' => 'value' })
+ }
+ it {
+ is_expected.to run \
+ .with_params({ 'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3' }, 'post') \
+ .and_return({ 'key1post' => 'value1', 'key2post' => 'value2', 'key3post' => 'value3' })
+ }
end
diff --git a/spec/functions/swapcase_spec.rb b/spec/functions/swapcase_spec.rb
index 791d1df..c175a15 100755
--- a/spec/functions/swapcase_spec.rb
+++ b/spec/functions/swapcase_spec.rb
@@ -1,28 +1,40 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the swapcase function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- it "should exist" do
- expect(Puppet::Parser::Functions.function("swapcase")).to eq("function_swapcase")
- end
-
- it "should raise a ParseError if there is less than 1 arguments" do
- expect { scope.function_swapcase([]) }.to( raise_error(Puppet::ParseError))
+describe 'swapcase' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it {
+ pending("Current implementation ignores parameters after the first.")
+ is_expected.to run.with_params('a', '').and_raise_error(Puppet::ParseError, /wrong number of arguments/i)
+ }
+ it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, /Requires either array or string to work/) }
+ it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, /Requires either array or string to work/) }
+ it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, /Requires either array or string to work/) }
+ describe 'with strings as inputs' do
+ it { is_expected.to run.with_params('').and_return('') }
+ it { is_expected.to run.with_params('one').and_return('ONE') }
+ it { is_expected.to run.with_params('ONE').and_return('one') }
+ it { is_expected.to run.with_params('oNe').and_return('OnE') }
end
-
- it "should swapcase a string" do
- result = scope.function_swapcase(["aaBBccDD"])
- expect(result).to(eq('AAbbCCdd'))
+ describe 'with arrays as inputs' do
+ it { is_expected.to run.with_params([]).and_return([]) }
+ describe 'only containing strings' do
+ it { is_expected.to run.with_params(['']).and_return(['']) }
+ it { is_expected.to run.with_params(['one']).and_return(['ONE']) }
+ it { is_expected.to run.with_params(['ONE']).and_return(['one']) }
+ it { is_expected.to run.with_params(['oNe']).and_return(['OnE']) }
+ it { is_expected.to run.with_params(['one', 'ONE']).and_return(['ONE', 'one']) }
+ it { is_expected.to run.with_params(['ONE', 'OnE']).and_return(['one', 'oNe']) }
+ it { is_expected.to run.with_params(['oNe', 'one']).and_return(['OnE', 'ONE']) }
+ end
+ describe 'containing mixed types' do
+ it { is_expected.to run.with_params(['OnE', {}]).and_return(['oNe', {}]) }
+ it { is_expected.to run.with_params(['OnE', 1]).and_return(['oNe', 1]) }
+ it { is_expected.to run.with_params(['OnE', []]).and_return(['oNe', []]) }
+ it { is_expected.to run.with_params(['OnE', ['two']]).and_return(['oNe', ['two']]) }
+ end
end
-
it "should accept objects which extend String" do
- class AlsoString < String
- end
-
- value = AlsoString.new("aaBBccDD")
- result = scope.function_swapcase([value])
- result.should(eq("AAbbCCdd"))
+ is_expected.to run.with_params(AlsoString.new("OnE")).and_return('oNe')
end
end
diff --git a/spec/functions/time_spec.rb b/spec/functions/time_spec.rb
index 6e22515..d157939 100755
--- a/spec/functions/time_spec.rb
+++ b/spec/functions/time_spec.rb
@@ -1,29 +1,21 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the time function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+describe 'time' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params('a', '').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
- it "should exist" do
- expect(Puppet::Parser::Functions.function("time")).to eq("function_time")
- end
-
- it "should raise a ParseError if there is more than 2 arguments" do
- expect { scope.function_time(['','']) }.to( raise_error(Puppet::ParseError))
- end
-
- it "should return a number" do
- result = scope.function_time([])
- expect(result).to be_an(Integer)
- end
-
- it "should be higher then when I wrote this test" do
- result = scope.function_time([])
- expect(result).to(be > 1311953157)
- end
-
- it "should be lower then 1.5 trillion" do
- result = scope.function_time([])
- expect(result).to(be < 1500000000)
+ context 'when running at a specific time' do
+ before(:each) {
+ # get a value before stubbing the function
+ test_time = Time.utc(2006, 10, 13, 8, 15, 11)
+ Time.expects(:new).with().returns(test_time).once
+ }
+ it { is_expected.to run.with_params().and_return(1160727311) }
+ it { is_expected.to run.with_params('').and_return(1160727311) }
+ it { is_expected.to run.with_params([]).and_return(1160727311) }
+ it { is_expected.to run.with_params({}).and_return(1160727311) }
+ it { is_expected.to run.with_params('foo').and_return(1160727311) }
+ it { is_expected.to run.with_params('UTC').and_return(1160727311) }
+ it { is_expected.to run.with_params('America/New_York').and_return(1160727311) }
end
end
diff --git a/spec/functions/to_bytes_spec.rb b/spec/functions/to_bytes_spec.rb
index 0f6ade9..2be23ff 100755
--- a/spec/functions/to_bytes_spec.rb
+++ b/spec/functions/to_bytes_spec.rb
@@ -1,83 +1,72 @@
-#! /usr/bin/env ruby -S rspec
-
require 'spec_helper'
-describe "the to_bytes function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- it "should exist" do
- expect(Puppet::Parser::Functions.function("to_bytes")).to eq("function_to_bytes")
- end
-
- it "should raise a ParseError if there is less than 1 arguments" do
- expect { scope.function_to_bytes([]) }.to( raise_error(Puppet::ParseError))
- end
-
- it "should convert kB to B" do
- result = scope.function_to_bytes(["4 kB"])
- expect(result).to(eq(4096))
- end
-
- it "should convert MB to B" do
- result = scope.function_to_bytes(["4 MB"])
- expect(result).to(eq(4194304))
- end
-
- it "should convert GB to B" do
- result = scope.function_to_bytes(["4 GB"])
- expect(result).to(eq(4294967296))
- end
-
- it "should convert TB to B" do
- result = scope.function_to_bytes(["4 TB"])
- expect(result).to(eq(4398046511104))
- end
-
- it "should convert PB to B" do
- result = scope.function_to_bytes(["4 PB"])
- expect(result).to(eq(4503599627370496))
- end
-
- it "should convert PB to B" do
- result = scope.function_to_bytes(["4 EB"])
- expect(result).to(eq(4611686018427387904))
- end
-
- it "should work without B in unit" do
- result = scope.function_to_bytes(["4 k"])
- expect(result).to(eq(4096))
- end
-
- it "should work without a space before unit" do
- result = scope.function_to_bytes(["4k"])
- expect(result).to(eq(4096))
- end
-
- it "should work without a unit" do
- result = scope.function_to_bytes(["5678"])
- expect(result).to(eq(5678))
- end
-
- it "should convert fractions" do
- result = scope.function_to_bytes(["1.5 kB"])
- expect(result).to(eq(1536))
- end
-
- it "should convert scientific notation" do
- result = scope.function_to_bytes(["1.5e2 B"])
- expect(result).to(eq(150))
- end
-
- it "should do nothing with a positive number" do
- result = scope.function_to_bytes([5678])
- expect(result).to(eq(5678))
- end
-
- it "should should raise a ParseError if input isn't a number" do
- expect { scope.function_to_bytes(["foo"]) }.to( raise_error(Puppet::ParseError))
- end
-
- it "should should raise a ParseError if prefix is unknown" do
- expect { scope.function_to_bytes(["5 uB"]) }.to( raise_error(Puppet::ParseError))
+describe 'to_bytes' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params('1', 'extras').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params([]).and_raise_error(TypeError, /(can't convert|no implicit conversion of) Array (in)?to String/) }
+ it { is_expected.to run.with_params({}).and_raise_error(TypeError, /(can't convert|no implicit conversion of) Hash (in)?to String/) }
+ it { is_expected.to run.with_params(true).and_raise_error(TypeError, /(can't convert|no implicit conversion of) (TrueClass|true) (in)?to String/) }
+
+ describe 'when passing numbers' do
+ it { is_expected.to run.with_params(0).and_return(0) }
+ it { is_expected.to run.with_params(1).and_return(1) }
+ it { is_expected.to run.with_params(-1).and_return(-1) }
+ it { is_expected.to run.with_params(1.1).and_return(1.1) }
+ it { is_expected.to run.with_params(-1.1).and_return(-1.1) }
+ end
+
+ describe 'when passing numbers as strings' do
+ describe 'without a unit' do
+ it { is_expected.to run.with_params('1').and_return(1) }
+ it { is_expected.to run.with_params('-1').and_return(-1) }
+ # these are so wrong
+ it { is_expected.to run.with_params('1.1').and_return(1) }
+ it { is_expected.to run.with_params('-1.1').and_return(-1) }
+ end
+
+ describe 'with a unit' do
+ it { is_expected.to run.with_params('1k').and_return(1024) }
+ it { is_expected.to run.with_params('-1kB').and_return(-1024) }
+ it { is_expected.to run.with_params('1k').and_return(1024) }
+ it { is_expected.to run.with_params('1M').and_return(1024*1024) }
+ it { is_expected.to run.with_params('1G').and_return(1024*1024*1024) }
+ it { is_expected.to run.with_params('1T').and_return(1024*1024*1024*1024) }
+ it { is_expected.to run.with_params('1P').and_return(1024*1024*1024*1024*1024) }
+ it { is_expected.to run.with_params('1E').and_return(1024*1024*1024*1024*1024*1024) }
+ it { is_expected.to run.with_params('1.5e3M').and_return(1572864000) }
+
+ it { is_expected.to run.with_params('4k').and_return(4*1024) }
+ it { is_expected.to run.with_params('-4kB').and_return(4*-1024) }
+ it { is_expected.to run.with_params('4k').and_return(4*1024) }
+ it { is_expected.to run.with_params('4M').and_return(4*1024*1024) }
+ it { is_expected.to run.with_params('4G').and_return(4*1024*1024*1024) }
+ it { is_expected.to run.with_params('4T').and_return(4*1024*1024*1024*1024) }
+ it { is_expected.to run.with_params('4P').and_return(4*1024*1024*1024*1024*1024) }
+ it { is_expected.to run.with_params('4E').and_return(4*1024*1024*1024*1024*1024*1024) }
+
+ # these are so wrong
+ it { is_expected.to run.with_params('1.0001 k').and_return(1024) }
+ it { is_expected.to run.with_params('-1.0001 kB').and_return(-1024) }
+ end
+
+ describe 'with a unknown unit' do
+ it { is_expected.to run.with_params('1KB').and_raise_error(Puppet::ParseError, /Unknown prefix/) }
+ it { is_expected.to run.with_params('1K').and_raise_error(Puppet::ParseError, /Unknown prefix/) }
+ it { is_expected.to run.with_params('1mb').and_raise_error(Puppet::ParseError, /Unknown prefix/) }
+ it { is_expected.to run.with_params('1m').and_raise_error(Puppet::ParseError, /Unknown prefix/) }
+ it { is_expected.to run.with_params('1%').and_raise_error(Puppet::ParseError, /Unknown prefix/) }
+ it { is_expected.to run.with_params('1 p').and_raise_error(Puppet::ParseError, /Unknown prefix/) }
+ end
+ end
+
+ # these are so wrong
+ describe 'when passing random stuff' do
+ it { is_expected.to run.with_params('-1....1').and_return(-1) }
+ it { is_expected.to run.with_params('-1.e.e.e.1').and_return(-1) }
+ it { is_expected.to run.with_params('-1+1').and_return(-1) }
+ it { is_expected.to run.with_params('1-1').and_return(1) }
+ it { is_expected.to run.with_params('1 kaboom').and_return(1024) }
+ it { is_expected.to run.with_params('kaboom').and_return(0) }
end
end
diff --git a/spec/functions/try_get_value_spec.rb b/spec/functions/try_get_value_spec.rb
new file mode 100644
index 0000000..38c0efd
--- /dev/null
+++ b/spec/functions/try_get_value_spec.rb
@@ -0,0 +1,100 @@
+require 'spec_helper'
+
+describe 'try_get_value' do
+
+ let(:data) do
+ {
+ 'a' => {
+ 'g' => '2',
+ 'e' => [
+ 'f0',
+ 'f1',
+ {
+ 'x' => {
+ 'y' => 'z'
+ }
+ },
+ 'f3',
+ ]
+ },
+ 'b' => true,
+ 'c' => false,
+ 'd' => '1',
+ }
+ end
+
+ context 'single values' do
+ it 'should exist' do
+ is_expected.not_to eq(nil)
+ end
+
+ it 'should be able to return a single value' do
+ is_expected.to run.with_params('test').and_return('test')
+ end
+
+ it 'should use the default value if data is a single value and path is present' do
+ is_expected.to run.with_params('test', 'path', 'default').and_return('default')
+ end
+
+ it 'should return default if there is no data' do
+ is_expected.to run.with_params(nil, nil, 'default').and_return('default')
+ end
+
+ it 'should be able to use data structures as default values' do
+ is_expected.to run.with_params('test', 'path', data).and_return(data)
+ end
+ end
+
+ context 'structure values' do
+ it 'should be able to extracts a single hash value' do
+ is_expected.to run.with_params(data, 'd', 'default').and_return('1')
+ end
+
+ it 'should be able to extract a deeply nested hash value' do
+ is_expected.to run.with_params(data, 'a/g', 'default').and_return('2')
+ end
+
+ it 'should return the default value if the path is not found' do
+ is_expected.to run.with_params(data, 'missing', 'default').and_return('default')
+ end
+
+ it 'should return the default value if the path is too long' do
+ is_expected.to run.with_params(data, 'a/g/c/d', 'default').and_return('default')
+ end
+
+ it 'should support an array index in the path' do
+ is_expected.to run.with_params(data, 'a/e/1', 'default').and_return('f1')
+ end
+
+ it 'should return the default value if an array index is not a number' do
+ is_expected.to run.with_params(data, 'a/b/c', 'default').and_return('default')
+ end
+
+ it 'should return the default value if and index is out of array length' do
+ is_expected.to run.with_params(data, 'a/e/5', 'default').and_return('default')
+ end
+
+ it 'should be able to path though both arrays and hashes' do
+ is_expected.to run.with_params(data, 'a/e/2/x/y', 'default').and_return('z')
+ end
+
+ it 'should be able to return "true" value' do
+ is_expected.to run.with_params(data, 'b', 'default').and_return(true)
+ is_expected.to run.with_params(data, 'm', true).and_return(true)
+ end
+
+ it 'should be able to return "false" value' do
+ is_expected.to run.with_params(data, 'c', 'default').and_return(false)
+ is_expected.to run.with_params(data, 'm', false).and_return(false)
+ end
+
+ it 'should return "nil" if value is not found and no default value is provided' do
+ is_expected.to run.with_params(data, 'a/1').and_return(nil)
+ end
+
+ it 'should be able to use a custom path separator' do
+ is_expected.to run.with_params(data, 'a::g', 'default', '::').and_return('2')
+ is_expected.to run.with_params(data, 'a::c', 'default', '::').and_return('default')
+ end
+ end
+end
diff --git a/spec/functions/type3x_spec.rb b/spec/functions/type3x_spec.rb
index d21236a..c3eb1de 100644
--- a/spec/functions/type3x_spec.rb
+++ b/spec/functions/type3x_spec.rb
@@ -1,8 +1,6 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the type3x function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+describe 'type3x' do
it "should exist" do
expect(Puppet::Parser::Functions.function("type3x")).to eq("function_type3x")
end
diff --git a/spec/functions/type_of_spec.rb b/spec/functions/type_of_spec.rb
new file mode 100644
index 0000000..cc9ef78
--- /dev/null
+++ b/spec/functions/type_of_spec.rb
@@ -0,0 +1,25 @@
+require 'spec_helper'
+
+if ENV["FUTURE_PARSER"] == 'yes'
+ describe 'type_of' do
+ pending 'teach rspec-puppet to load future-only functions under 3.7.5' do
+ it { is_expected.not_to eq(nil) }
+ end
+ end
+end
+
+if Puppet.version.to_f >= 4.0
+ describe 'type_of' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(ArgumentError) }
+ it { is_expected.to run.with_params('', '').and_raise_error(ArgumentError) }
+
+ it 'gives the type of a string' do
+ expect(subject.call({}, 'hello world')).to be_kind_of(Puppet::Pops::Types::PStringType)
+ end
+
+ it 'gives the type of an integer' do
+ expect(subject.call({}, 5)).to be_kind_of(Puppet::Pops::Types::PIntegerType)
+ end
+ end
+end
diff --git a/spec/functions/type_spec.rb b/spec/functions/type_spec.rb
index b683fcf..4288df0 100755
--- a/spec/functions/type_spec.rb
+++ b/spec/functions/type_spec.rb
@@ -1,8 +1,6 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the type function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+describe 'type' do
it "should exist" do
expect(Puppet::Parser::Functions.function("type")).to eq("function_type")
end
diff --git a/spec/functions/union_spec.rb b/spec/functions/union_spec.rb
index 706f4cb..cfd38b6 100755
--- a/spec/functions/union_spec.rb
+++ b/spec/functions/union_spec.rb
@@ -1,19 +1,24 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the union function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- it "should exist" do
- expect(Puppet::Parser::Functions.function("union")).to eq("function_union")
- end
-
- it "should raise a ParseError if there are fewer than 2 arguments" do
- expect { scope.function_union([]) }.to( raise_error(Puppet::ParseError) )
+describe 'union' do
+ describe 'argument checking' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params('one', []).and_raise_error(Puppet::ParseError, /Every parameter must be an array/) }
+ it { is_expected.to run.with_params([], 'two').and_raise_error(Puppet::ParseError, /Every parameter must be an array/) }
+ it { is_expected.to run.with_params({}, {}).and_raise_error(Puppet::ParseError, /Every parameter must be an array/) }
end
- it "should join two arrays together" do
- result = scope.function_union([["a","b","c"],["b","c","d"]])
- expect(result).to(eq(["a","b","c","d"]))
- end
+ it { is_expected.to run.with_params([], []).and_return([]) }
+ it { is_expected.to run.with_params([], ['one']).and_return(['one']) }
+ it { is_expected.to run.with_params(['one'], []).and_return(['one']) }
+ it { is_expected.to run.with_params(['one'], ['one']).and_return(['one']) }
+ it { is_expected.to run.with_params(['one'], ['two']).and_return(['one', 'two']) }
+ it { is_expected.to run.with_params(['one', 'two', 'three'], ['two', 'three']).and_return(['one', 'two', 'three']) }
+ it { is_expected.to run.with_params(['one', 'two', 'two', 'three'], ['two', 'three']).and_return(['one', 'two', 'three']) }
+ it { is_expected.to run.with_params(['one', 'two', 'three'], ['two', 'two', 'three']).and_return(['one', 'two', 'three']) }
+ it { is_expected.to run.with_params(['one', 'two'], ['two', 'three'], ['one', 'three']).and_return(['one', 'two', 'three']) }
+ it { is_expected.to run.with_params(['one', 'two'], ['three', 'four'], ['one', 'two', 'three'], ['four']).and_return(['one', 'two', 'three', 'four']) }
+ it 'should not confuse types' do is_expected.to run.with_params(['1', '2', '3'], [1, 2]).and_return(['1', '2', '3', 1, 2]) end
end
diff --git a/spec/functions/unique_spec.rb b/spec/functions/unique_spec.rb
index 7cd3a56..24257a0 100755
--- a/spec/functions/unique_spec.rb
+++ b/spec/functions/unique_spec.rb
@@ -1,33 +1,27 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the unique function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- it "should exist" do
- expect(Puppet::Parser::Functions.function("unique")).to eq("function_unique")
- end
-
- it "should raise a ParseError if there is less than 1 arguments" do
- expect { scope.function_unique([]) }.to( raise_error(Puppet::ParseError))
+describe 'unique' do
+ describe 'signature validation' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it {
+ pending("Current implementation ignores parameters after the first.")
+ is_expected.to run.with_params([], 'extra').and_raise_error(Puppet::ParseError, /wrong number of arguments/i)
+ }
+ it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, /Requires either array or string to work/) }
+ it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, /Requires either array or string to work/) }
+ it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, /Requires either array or string to work/) }
end
- it "should remove duplicate elements in a string" do
- result = scope.function_unique(["aabbc"])
- expect(result).to(eq('abc'))
+ context 'when called with an array' do
+ it { is_expected.to run.with_params([]).and_return([]) }
+ it { is_expected.to run.with_params(['a']).and_return(['a']) }
+ it { is_expected.to run.with_params(['a', 'b', 'a']).and_return(['a', 'b']) }
end
- it "should remove duplicate elements in an array" do
- result = scope.function_unique([["a","a","b","b","c"]])
- expect(result).to(eq(['a','b','c']))
- end
-
- it "should accept objects which extend String" do
- class AlsoString < String
- end
-
- value = AlsoString.new('aabbc')
- result = scope.function_unique([value])
- result.should(eq('abc'))
+ context 'when called with a string' do
+ it { is_expected.to run.with_params('').and_return('') }
+ it { is_expected.to run.with_params('a').and_return('a') }
+ it { is_expected.to run.with_params('aaba').and_return('ab') }
end
end
diff --git a/spec/functions/unix2dos_spec.rb b/spec/functions/unix2dos_spec.rb
new file mode 100644
index 0000000..8537a26
--- /dev/null
+++ b/spec/functions/unix2dos_spec.rb
@@ -0,0 +1,40 @@
+require 'spec_helper'
+
+describe 'unix2dos' do
+ context 'Checking parameter validity' do
+ it { is_expected.not_to eq(nil) }
+ it do
+ is_expected.to run.with_params.and_raise_error(ArgumentError, /Wrong number of arguments/)
+ end
+ it do
+ is_expected.to run.with_params('one', 'two').and_raise_error(ArgumentError, /Wrong number of arguments/)
+ end
+ it do
+ is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError)
+ end
+ it do
+ is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError)
+ end
+ it do
+ is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError)
+ end
+ end
+
+ context 'Converting from unix to dos format' do
+ sample_text = "Hello\nWorld\n"
+ desired_output = "Hello\r\nWorld\r\n"
+
+ it 'should output dos format' do
+ should run.with_params(sample_text).and_return(desired_output)
+ end
+ end
+
+ context 'Converting from dos to dos format' do
+ sample_text = "Hello\r\nWorld\r\n"
+ desired_output = "Hello\r\nWorld\r\n"
+
+ it 'should output dos format' do
+ should run.with_params(sample_text).and_return(desired_output)
+ end
+ end
+end
diff --git a/spec/functions/upcase_spec.rb b/spec/functions/upcase_spec.rb
index 3cf8b05..3b7b02d 100755
--- a/spec/functions/upcase_spec.rb
+++ b/spec/functions/upcase_spec.rb
@@ -1,33 +1,26 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the upcase function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- it "should exist" do
- expect(Puppet::Parser::Functions.function("upcase")).to eq("function_upcase")
- end
-
- it "should raise a ParseError if there is less than 1 arguments" do
- expect { scope.function_upcase([]) }.to( raise_error(Puppet::ParseError))
+describe 'upcase' do
+ describe 'signature validation' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params('', '').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, /Requires an array, hash or object that responds to upcase/) }
+ it { is_expected.to run.with_params([1]).and_raise_error(Puppet::ParseError, /Requires an array, hash or object that responds to upcase/) }
end
- it "should upcase a string" do
- result = scope.function_upcase(["abc"])
- expect(result).to(eq('ABC'))
+ describe 'normal string handling' do
+ it { is_expected.to run.with_params("abc").and_return("ABC") }
+ it { is_expected.to run.with_params("Abc").and_return("ABC") }
+ it { is_expected.to run.with_params("ABC").and_return("ABC") }
end
- it "should do nothing if a string is already upcase" do
- result = scope.function_upcase(["ABC"])
- expect(result).to(eq('ABC'))
+ describe 'handling classes derived from String' do
+ it { is_expected.to run.with_params(AlsoString.new("ABC")).and_return("ABC") }
end
- it "should accept objects which extend String" do
- class AlsoString < String
- end
-
- value = AlsoString.new('abc')
- result = scope.function_upcase([value])
- result.should(eq('ABC'))
+ describe 'strings in arrays handling' do
+ it { is_expected.to run.with_params([]).and_return([]) }
+ it { is_expected.to run.with_params(["One", "twO"]).and_return(["ONE", "TWO"]) }
end
end
diff --git a/spec/functions/uriescape_spec.rb b/spec/functions/uriescape_spec.rb
index 2321e5a..f05ec08 100755
--- a/spec/functions/uriescape_spec.rb
+++ b/spec/functions/uriescape_spec.rb
@@ -1,33 +1,36 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the uriescape function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- it "should exist" do
- expect(Puppet::Parser::Functions.function("uriescape")).to eq("function_uriescape")
- end
-
- it "should raise a ParseError if there is less than 1 arguments" do
- expect { scope.function_uriescape([]) }.to( raise_error(Puppet::ParseError))
+describe 'uriescape' do
+ describe 'signature validation' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it {
+ pending("Current implementation ignores parameters after the first.")
+ is_expected.to run.with_params('', '').and_raise_error(Puppet::ParseError, /wrong number of arguments/i)
+ }
+ it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, /Requires either array or string to work/) }
+ it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, /Requires either array or string to work/) }
+ it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, /Requires either array or string to work/) }
end
- it "should uriescape a string" do
- result = scope.function_uriescape([":/?#[]@!$&'()*+,;= \"{}"])
- expect(result).to(eq(':/?%23[]@!$&\'()*+,;=%20%22%7B%7D'))
- end
-
- it "should do nothing if a string is already safe" do
- result = scope.function_uriescape(["ABCdef"])
- expect(result).to(eq('ABCdef'))
+ describe 'handling normal strings' do
+ it 'should call ruby\'s URI.escape function' do
+ URI.expects(:escape).with('uri_string').returns('escaped_uri_string').once
+ is_expected.to run.with_params('uri_string').and_return('escaped_uri_string')
+ end
end
- it "should accept objects which extend String" do
- class AlsoString < String
+ describe 'handling classes derived from String' do
+ it 'should call ruby\'s URI.escape function' do
+ uri_string = AlsoString.new('uri_string')
+ URI.expects(:escape).with(uri_string).returns('escaped_uri_string').once
+ is_expected.to run.with_params(uri_string).and_return("escaped_uri_string")
end
+ end
- value = AlsoString.new('abc')
- result = scope.function_uriescape([value])
- result.should(eq('abc'))
+ describe 'strings in arrays handling' do
+ it { is_expected.to run.with_params([]).and_return([]) }
+ it { is_expected.to run.with_params(["one}", "two"]).and_return(["one%7D", "two"]) }
+ it { is_expected.to run.with_params(["one}", 1, true, {}, "two"]).and_return(["one%7D", 1, true, {}, "two"]) }
end
end
diff --git a/spec/functions/validate_absolute_path_spec.rb b/spec/functions/validate_absolute_path_spec.rb
index 36c836b..9397da5 100755
--- a/spec/functions/validate_absolute_path_spec.rb
+++ b/spec/functions/validate_absolute_path_spec.rb
@@ -1,67 +1,43 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe Puppet::Parser::Functions.function(:validate_absolute_path) do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- # The subject of these examples is the method itself.
- subject do
- # This makes sure the function is loaded within each test
- function_name = Puppet::Parser::Functions.function(:validate_absolute_path)
- scope.method(function_name)
+describe 'validate_absolute_path' do
+ after(:all) do
+ ENV.delete('STDLIB_LOG_DEPRECATIONS')
end
- describe "Valid Paths" do
- def self.valid_paths
- %w{
- C:/
- C:\\
- C:\\WINDOWS\\System32
- C:/windows/system32
- X:/foo/bar
- X:\\foo\\bar
- /var/tmp
- /var/lib/puppet
- /var/opt/../lib/puppet
- }
- end
+ # Checking for deprecation warning
+ it 'should display a single deprecation' do
+ ENV['STDLIB_LOG_DEPRECATIONS'] = "true"
+ scope.expects(:warning).with(includes('This method is deprecated'))
+ is_expected.to run.with_params('c:/')
+ end
- context "Without Puppet::Util.absolute_path? (e.g. Puppet <= 2.6)" do
- before :each do
- # The intent here is to mock Puppet to behave like Puppet 2.6 does.
- # Puppet 2.6 does not have the absolute_path? method. This is only a
- # convenience test, stdlib should be run with the Puppet 2.6.x in the
- # $LOAD_PATH in addition to 2.7.x and master.
- Puppet::Util.expects(:respond_to?).with(:absolute_path?).returns(false)
- end
- valid_paths.each do |path|
- it "validate_absolute_path(#{path.inspect}) should not fail" do
- expect { subject.call [path] }.not_to raise_error
- end
- end
- valid_paths do
- it "validate_absolute_path(#{valid_paths.inspect}) should not fail" do
- expect { subject.call [valid_paths] }.not_to raise_error
- end
- end
- end
+ describe 'signature validation' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ end
- context "Puppet without mocking" do
- valid_paths.each do |path|
- it "validate_absolute_path(#{path.inspect}) should not fail" do
- expect { subject.call [path] }.not_to raise_error
- end
- end
- valid_paths do
- it "validate_absolute_path(#{valid_paths.inspect}) should not fail" do
- expect { subject.call [valid_paths] }.not_to raise_error
- end
- end
+ describe "valid paths handling" do
+ %w{
+ C:/
+ C:\\
+ C:\\WINDOWS\\System32
+ C:/windows/system32
+ X:/foo/bar
+ X:\\foo\\bar
+ \\\\host\\windows
+ //host/windows
+ /
+ /var/tmp
+ /var/opt/../lib/puppet
+ }.each do |path|
+ it { is_expected.to run.with_params(path) }
+ it { is_expected.to run.with_params(['/tmp', path]) }
end
end
- describe 'Invalid paths' do
- context 'Garbage inputs' do
+ describe 'invalid path handling' do
+ context 'garbage inputs' do
[
nil,
[ nil ],
@@ -70,33 +46,26 @@ describe Puppet::Parser::Functions.function(:validate_absolute_path) do
{ },
'',
].each do |path|
- it "validate_absolute_path(#{path.inspect}) should fail" do
- expect { subject.call [path] }.to raise_error Puppet::ParseError
- end
+ it { is_expected.to run.with_params(path).and_raise_error(Puppet::ParseError, /is not an absolute path/) }
+ it { is_expected.to run.with_params([path]).and_raise_error(Puppet::ParseError, /is not an absolute path/) }
+ it { is_expected.to run.with_params(['/tmp', path]).and_raise_error(Puppet::ParseError, /is not an absolute path/) }
end
end
- context 'Relative paths' do
- def self.rel_paths
- %w{
- relative1
- .
- ..
- ./foo
- ../foo
- etc/puppetlabs/puppet
- opt/puppet/bin
- }
- end
- rel_paths.each do |path|
- it "validate_absolute_path(#{path.inspect}) should fail" do
- expect { subject.call [path] }.to raise_error Puppet::ParseError
- end
- end
- rel_paths do
- it "validate_absolute_path(#{rel_paths.inspect}) should fail" do
- expect { subject.call [rel_paths] }.to raise_error Puppet::ParseError
- end
+ context 'relative paths' do
+ %w{
+ relative1
+ .
+ ..
+ ./foo
+ ../foo
+ etc/puppetlabs/puppet
+ opt/puppet/bin
+ relative\\windows
+ }.each do |path|
+ it { is_expected.to run.with_params(path).and_raise_error(Puppet::ParseError, /is not an absolute path/) }
+ it { is_expected.to run.with_params([path]).and_raise_error(Puppet::ParseError, /is not an absolute path/) }
+ it { is_expected.to run.with_params(['/tmp', path]).and_raise_error(Puppet::ParseError, /is not an absolute path/) }
end
end
end
diff --git a/spec/functions/validate_array_spec.rb b/spec/functions/validate_array_spec.rb
index 4b31cfd..409b3dc 100755
--- a/spec/functions/validate_array_spec.rb
+++ b/spec/functions/validate_array_spec.rb
@@ -1,38 +1,37 @@
-#! /usr/bin/env ruby -S rspec
-
require 'spec_helper'
-describe Puppet::Parser::Functions.function(:validate_array) do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
- describe 'when calling validate_array from puppet' do
-
- %w{ true false }.each do |the_string|
- it "should not compile when #{the_string} is a string" do
- Puppet[:code] = "validate_array('#{the_string}')"
- expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is not an Array/)
- end
+describe 'validate_array' do
- it "should not compile when #{the_string} is a bare word" do
- Puppet[:code] = "validate_array(#{the_string})"
- expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is not an Array/)
- end
+ describe 'signature validation' do
+ after(:all) do
+ ENV.delete('STDLIB_LOG_DEPRECATIONS')
end
+ it { is_expected.not_to eq(nil) }
+ # Checking for deprecation warning
+ it 'should display a single deprecation' do
+ ENV['STDLIB_LOG_DEPRECATIONS'] = "true"
+ scope.expects(:warning).with(includes('This method is deprecated'))
+ is_expected.to run.with_params([])
+ end
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
- it "should compile when multiple array arguments are passed" do
- Puppet[:code] = <<-'ENDofPUPPETcode'
- $foo = [ ]
- $bar = [ 'one', 'two' ]
- validate_array($foo, $bar)
- ENDofPUPPETcode
- scope.compiler.compile
+ describe 'valid inputs' do
+ it { is_expected.to run.with_params([]) }
+ it { is_expected.to run.with_params(['one']) }
+ it { is_expected.to run.with_params([], ['two']) }
+ it { is_expected.to run.with_params(['one'], ['two']) }
end
- it "should not compile when an undef variable is passed" do
- Puppet[:code] = <<-'ENDofPUPPETcode'
- $foo = undef
- validate_array($foo)
- ENDofPUPPETcode
- expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is not an Array/)
+ describe 'invalid inputs' do
+ it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, /is not an Array/) }
+ it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, /is not an Array/) }
+ it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, /is not an Array/) }
+ it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, /is not an Array/) }
+ it { is_expected.to run.with_params([], {}).and_raise_error(Puppet::ParseError, /is not an Array/) }
+ it { is_expected.to run.with_params([], 1).and_raise_error(Puppet::ParseError, /is not an Array/) }
+ it { is_expected.to run.with_params([], true).and_raise_error(Puppet::ParseError, /is not an Array/) }
+ it { is_expected.to run.with_params([], 'one').and_raise_error(Puppet::ParseError, /is not an Array/) }
end
end
end
+
diff --git a/spec/functions/validate_augeas_spec.rb b/spec/functions/validate_augeas_spec.rb
index c695ba2..4236649 100755
--- a/spec/functions/validate_augeas_spec.rb
+++ b/spec/functions/validate_augeas_spec.rb
@@ -1,49 +1,29 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe Puppet::Parser::Functions.function(:validate_augeas), :if => Puppet.features.augeas? do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- # The subject of these examplres is the method itself.
- subject do
- # This makes sure the function is loaded within each test
- function_name = Puppet::Parser::Functions.function(:validate_augeas)
- scope.method(function_name)
- end
-
- context 'Using Puppet::Parser::Scope.new' do
-
- describe 'Garbage inputs' do
- inputs = [
- [ nil ],
- [ [ nil ] ],
- [ { 'foo' => 'bar' } ],
- [ { } ],
- [ '' ],
- [ "one", "one", "MSG to User", "4th arg" ],
- ]
-
- inputs.each do |input|
- it "validate_augeas(#{input.inspect}) should fail" do
- expect { subject.call [input] }.to raise_error Puppet::ParseError
- end
- end
+describe 'validate_augeas' do
+ unless Puppet.features.augeas?
+ skip "ruby-augeas not installed"
+ else
+ describe 'signature validation' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params('').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params('', '', [], '', 'extra').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params('one', 'one', 'MSG to User', '4th arg').and_raise_error(NoMethodError) }
end
- describe 'Valid inputs' do
+ describe 'valid inputs' do
inputs = [
[ "root:x:0:0:root:/root:/bin/bash\n", 'Passwd.lns' ],
[ "proc /proc proc nodev,noexec,nosuid 0 0\n", 'Fstab.lns'],
]
inputs.each do |input|
- it "validate_augeas(#{input.inspect}) should not fail" do
- expect { subject.call input }.not_to raise_error
- end
+ it { is_expected.to run.with_params(*input) }
end
end
- describe "Valid inputs which should raise an exception without a message" do
+ describe 'valid inputs which fail augeas validation' do
# The intent here is to make sure valid inputs raise exceptions when they
# don't specify an error message to display. This is the behvior in
# 2.2.x and prior.
@@ -53,14 +33,12 @@ describe Puppet::Parser::Functions.function(:validate_augeas), :if => Puppet.fea
]
inputs.each do |input|
- it "validate_augeas(#{input.inspect}) should fail" do
- expect { subject.call input }.to raise_error /validate_augeas.*?matched less than it should/
- end
+ it { is_expected.to run.with_params(*input).and_raise_error(Puppet::ParseError, /validate_augeas.*?matched less than it should/) }
end
end
- describe "Nicer Error Messages" do
- # The intent here is to make sure the function returns the 3rd argument
+ describe "when specifying nice error messages" do
+ # The intent here is to make sure the function returns the 4th argument
# in the exception thrown
inputs = [
[ "root:x:0:0:root\n", 'Passwd.lns', [], 'Failed to validate passwd content' ],
@@ -68,35 +46,29 @@ describe Puppet::Parser::Functions.function(:validate_augeas), :if => Puppet.fea
]
inputs.each do |input|
- it "validate_augeas(#{input.inspect}) should fail" do
- expect { subject.call input }.to raise_error /#{input[2]}/
- end
+ it { is_expected.to run.with_params(*input).and_raise_error(Puppet::ParseError, /#{input[3]}/) }
end
end
- describe "Passing simple unit tests" do
+ describe "matching additional tests" do
inputs = [
[ "root:x:0:0:root:/root:/bin/bash\n", 'Passwd.lns', ['$file/foobar']],
[ "root:x:0:0:root:/root:/bin/bash\n", 'Passwd.lns', ['$file/root/shell[.="/bin/sh"]', 'foobar']],
]
inputs.each do |input|
- it "validate_augeas(#{input.inspect}) should fail" do
- expect { subject.call input }.not_to raise_error
- end
+ it { is_expected.to run.with_params(*input) }
end
end
- describe "Failing simple unit tests" do
+ describe "failing additional tests" do
inputs = [
[ "foobar:x:0:0:root:/root:/bin/bash\n", 'Passwd.lns', ['$file/foobar']],
[ "root:x:0:0:root:/root:/bin/sh\n", 'Passwd.lns', ['$file/root/shell[.="/bin/sh"]', 'foobar']],
]
inputs.each do |input|
- it "validate_augeas(#{input.inspect}) should fail" do
- expect { subject.call input }.to raise_error /testing path/
- end
+ it { is_expected.to run.with_params(*input).and_raise_error(Puppet::ParseError, /testing path/) }
end
end
end
diff --git a/spec/functions/validate_bool_spec.rb b/spec/functions/validate_bool_spec.rb
index a352d3b..3074d88 100755
--- a/spec/functions/validate_bool_spec.rb
+++ b/spec/functions/validate_bool_spec.rb
@@ -1,51 +1,36 @@
-#! /usr/bin/env ruby -S rspec
-
require 'spec_helper'
-describe Puppet::Parser::Functions.function(:validate_bool) do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
- describe 'when calling validate_bool from puppet' do
-
- %w{ true false }.each do |the_string|
-
- it "should not compile when #{the_string} is a string" do
- Puppet[:code] = "validate_bool('#{the_string}')"
- expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is not a boolean/)
- end
-
- it "should compile when #{the_string} is a bare word" do
- Puppet[:code] = "validate_bool(#{the_string})"
- scope.compiler.compile
- end
-
- end
+describe 'validate_bool' do
+ after(:all) do
+ ENV.delete('STDLIB_LOG_DEPRECATIONS')
+ end
- it "should not compile when an arbitrary string is passed" do
- Puppet[:code] = 'validate_bool("jeff and dan are awesome")'
- expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is not a boolean/)
- end
+ # Checking for deprecation warning
+ it 'should display a single deprecation' do
+ ENV['STDLIB_LOG_DEPRECATIONS'] = "true"
+ scope.expects(:warning).with(includes('This method is deprecated'))
+ is_expected.to run.with_params(true)
+ end
- it "should not compile when no arguments are passed" do
- Puppet[:code] = 'validate_bool()'
- expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /wrong number of arguments/)
- end
+ describe 'signature validation' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ end
- it "should compile when multiple boolean arguments are passed" do
- Puppet[:code] = <<-'ENDofPUPPETcode'
- $foo = true
- $bar = false
- validate_bool($foo, $bar, true, false)
- ENDofPUPPETcode
- scope.compiler.compile
- end
+ describe 'acceptable values' do
+ it { is_expected.to run.with_params(true) }
+ it { is_expected.to run.with_params(false) }
+ it { is_expected.to run.with_params(true, false, false, true) }
+ end
- it "should compile when multiple boolean arguments are passed" do
- Puppet[:code] = <<-'ENDofPUPPETcode'
- $foo = true
- $bar = false
- validate_bool($foo, $bar, true, false, 'jeff')
- ENDofPUPPETcode
- expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is not a boolean/)
- end
+ describe 'validation failures' do
+ it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, /is not a boolean/) }
+ it { is_expected.to run.with_params(true, 'one').and_raise_error(Puppet::ParseError, /is not a boolean/) }
+ it { is_expected.to run.with_params('one', false).and_raise_error(Puppet::ParseError, /is not a boolean/) }
+ it { is_expected.to run.with_params("true").and_raise_error(Puppet::ParseError, /is not a boolean/) }
+ it { is_expected.to run.with_params("false").and_raise_error(Puppet::ParseError, /is not a boolean/) }
+ it { is_expected.to run.with_params(true, "false").and_raise_error(Puppet::ParseError, /is not a boolean/) }
+ it { is_expected.to run.with_params("true", false).and_raise_error(Puppet::ParseError, /is not a boolean/) }
+ it { is_expected.to run.with_params("true", false, false, false, false, false).and_raise_error(Puppet::ParseError, /is not a boolean/) }
end
end
diff --git a/spec/functions/validate_cmd_spec.rb b/spec/functions/validate_cmd_spec.rb
index 7cb9782..ab0cbc9 100755
--- a/spec/functions/validate_cmd_spec.rb
+++ b/spec/functions/validate_cmd_spec.rb
@@ -1,85 +1,35 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-TESTEXE = File.exists?('/usr/bin/test') ? '/usr/bin/test' : '/bin/test'
-TOUCHEXE = File.exists?('/usr/bin/touch') ? '/usr/bin/touch' : '/bin/touch'
-
-describe Puppet::Parser::Functions.function(:validate_cmd) do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- subject do
- function_name = Puppet::Parser::Functions.function(:validate_cmd)
- scope.method(function_name)
+describe 'validate_cmd' do
+ let(:touch) { File.exists?('/usr/bin/touch') ? '/usr/bin/touch' : '/bin/touch' }
+
+ describe 'signature validation' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params('').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params('', '', '', 'extra').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it {
+ pending('should implement stricter type checking')
+ is_expected.to run.with_params([], '', '').and_raise_error(Puppet::ParseError, /content must be a string/)
+ }
+ it {
+ pending('should implement stricter type checking')
+ is_expected.to run.with_params('', [], '').and_raise_error(Puppet::ParseError, /checkscript must be a string/)
+ }
+ it {
+ pending('should implement stricter type checking')
+ is_expected.to run.with_params('', '', []).and_raise_error(Puppet::ParseError, /custom error message must be a string/)
+ }
end
- context 'with no % placeholder' do
- describe "with an explicit failure message" do
- it "prints the failure message on error" do
- expect {
- subject.call ['', '/bin/false', 'failure message!']
- }.to raise_error Puppet::ParseError, /failure message!/
- end
- end
-
- describe "on validation failure" do
- it "includes the command error output" do
- expect {
- subject.call ['', "#{TOUCHEXE} /cant/touch/this"]
- }.to raise_error Puppet::ParseError, /(cannot touch|o such file or)/
- end
-
- it "includes the command return value" do
- expect {
- subject.call ['', '/cant/run/this']
- }.to raise_error Puppet::ParseError, /returned 1\b/
- end
- end
-
- describe "when performing actual validation" do
- it "can positively validate file content" do
- expect { subject.call ["non-empty", "#{TESTEXE} -s"] }.to_not raise_error
- end
-
- it "can negatively validate file content" do
- expect {
- subject.call ["", "#{TESTEXE} -s"]
- }.to raise_error Puppet::ParseError, /failed to validate.*test -s/
- end
+ context 'when validation fails' do
+ context 'with % placeholder' do
+ it { is_expected.to run.with_params('', "#{touch} % /no/such/file").and_raise_error(Puppet::ParseError, /Execution of '#{touch} \S+ \/no\/such\/file' returned 1:.*(cannot touch|o such file or)/) }
+ it { is_expected.to run.with_params('', "#{touch} % /no/such/file", 'custom error').and_raise_error(Puppet::ParseError, /custom error/) }
end
- end
-
- context 'with % placeholder' do
- describe "with an explicit failure message" do
- it "prints the failure message on error" do
- expect {
- subject.call ['', '/bin/false % -f', 'failure message!']
- }.to raise_error Puppet::ParseError, /failure message!/
- end
- end
- describe "on validation failure" do
- it "includes the command error output" do
- expect {
- subject.call ['', "#{TOUCHEXE} /cant/touch/this"]
- }.to raise_error Puppet::ParseError, /(cannot touch|o such file or)/
- end
-
- it "includes the command return value" do
- expect {
- subject.call ['', '/cant/run/this % -z']
- }.to raise_error Puppet::ParseError, /Execution of '\/cant\/run\/this .+ -z' returned 1/
- end
- end
-
- describe "when performing actual validation" do
- it "can positively validate file content" do
- expect { subject.call ["non-empty", "#{TESTEXE} -s %"] }.to_not raise_error
- end
-
- it "can negatively validate file content" do
- expect {
- subject.call ["", "#{TESTEXE} -s %"]
- }.to raise_error Puppet::ParseError, /failed to validate.*test -s/
- end
+ context 'without % placeholder' do
+ it { is_expected.to run.with_params('', "#{touch} /no/such/file").and_raise_error(Puppet::ParseError, /Execution of '#{touch} \/no\/such\/file \S+' returned 1:.*(cannot touch|o such file or)/) }
+ it { is_expected.to run.with_params('', "#{touch} /no/such/file", 'custom error').and_raise_error(Puppet::ParseError, /custom error/) }
end
end
end
diff --git a/spec/functions/validate_email_address_spec.rb b/spec/functions/validate_email_address_spec.rb
new file mode 100644
index 0000000..7628383
--- /dev/null
+++ b/spec/functions/validate_email_address_spec.rb
@@ -0,0 +1,23 @@
+require 'spec_helper'
+
+describe 'validate_email_address' do
+ describe 'signature validation' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ end
+
+ describe 'valid inputs' do
+ it { is_expected.to run.with_params('bob@gmail.com') }
+ it { is_expected.to run.with_params('alice+puppetlabs.com@gmail.com') }
+ end
+
+ describe 'invalid inputs' do
+ it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, /is not a string/) }
+ it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, /is not a string/) }
+ it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, /is not a string/) }
+ it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, /is not a valid email/) }
+ it { is_expected.to run.with_params('bob@gmail.com', {}).and_raise_error(Puppet::ParseError, /is not a string/) }
+ it { is_expected.to run.with_params('bob@gmail.com', true).and_raise_error(Puppet::ParseError, /is not a string/) }
+ it { is_expected.to run.with_params('bob@gmail.com', 'one').and_raise_error(Puppet::ParseError, /is not a valid email/) }
+ end
+end
diff --git a/spec/functions/validate_hash_spec.rb b/spec/functions/validate_hash_spec.rb
index a0c35c2..7533abe 100755
--- a/spec/functions/validate_hash_spec.rb
+++ b/spec/functions/validate_hash_spec.rb
@@ -1,43 +1,38 @@
-#! /usr/bin/env ruby -S rspec
-
require 'spec_helper'
-describe Puppet::Parser::Functions.function(:validate_hash) do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- describe 'when calling validate_hash from puppet' do
+describe 'validate_hash' do
+ describe 'signature validation' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
- %w{ true false }.each do |the_string|
-
- it "should not compile when #{the_string} is a string" do
- Puppet[:code] = "validate_hash('#{the_string}')"
- expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is not a Hash/)
+ describe 'check for deprecation warning' do
+ after(:all) do
+ ENV.delete('STDLIB_LOG_DEPRECATIONS')
end
-
- it "should not compile when #{the_string} is a bare word" do
- Puppet[:code] = "validate_hash(#{the_string})"
- expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is not a Hash/)
+ # Checking for deprecation warning
+ it 'should display a single deprecation' do
+ ENV['STDLIB_LOG_DEPRECATIONS'] = "true"
+ scope.expects(:warning).with(includes('This method is deprecated'))
+ is_expected.to run.with_params({'key' => 'value'})
end
-
end
- it "should compile when multiple hash arguments are passed" do
- Puppet[:code] = <<-'ENDofPUPPETcode'
- $foo = {}
- $bar = { 'one' => 'two' }
- validate_hash($foo, $bar)
- ENDofPUPPETcode
- scope.compiler.compile
+ describe 'valid inputs' do
+ it { is_expected.to run.with_params({}) }
+ it { is_expected.to run.with_params({'key' => 'value'}) }
+ it { is_expected.to run.with_params({}, {'key' => 'value'}) }
+ it { is_expected.to run.with_params({'key1' => 'value1'}, {'key2' => 'value2'}) }
end
- it "should not compile when an undef variable is passed" do
- Puppet[:code] = <<-'ENDofPUPPETcode'
- $foo = undef
- validate_hash($foo)
- ENDofPUPPETcode
- expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is not a Hash/)
+ describe 'invalid inputs' do
+ it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError, /is not a Hash/) }
+ it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, /is not a Hash/) }
+ it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, /is not a Hash/) }
+ it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, /is not a Hash/) }
+ it { is_expected.to run.with_params({}, []).and_raise_error(Puppet::ParseError, /is not a Hash/) }
+ it { is_expected.to run.with_params({}, 1).and_raise_error(Puppet::ParseError, /is not a Hash/) }
+ it { is_expected.to run.with_params({}, true).and_raise_error(Puppet::ParseError, /is not a Hash/) }
+ it { is_expected.to run.with_params({}, 'one').and_raise_error(Puppet::ParseError, /is not a Hash/) }
end
-
end
-
end
diff --git a/spec/functions/validate_integer_spec.rb b/spec/functions/validate_integer_spec.rb
new file mode 100755
index 0000000..6558d00
--- /dev/null
+++ b/spec/functions/validate_integer_spec.rb
@@ -0,0 +1,101 @@
+require 'spec_helper'
+
+describe 'validate_integer' do
+ after(:all) do
+ ENV.delete('STDLIB_LOG_DEPRECATIONS')
+ end
+
+ # Checking for deprecation warning
+ it 'should display a single deprecation' do
+ ENV['STDLIB_LOG_DEPRECATIONS'] = "true"
+ scope.expects(:warning).with(includes('This method is deprecated'))
+ is_expected.to run.with_params(3)
+ end
+
+ describe 'signature validation' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params(1, 2, 3, 4).and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+
+ [ true, 'true', false, 'false', 'iAmAString', '1test', '1 test', 'test 1', 'test 1 test', 7.0, -7.0, {}, { 'key' => 'value' }, { 1=> 2 }, '', :undef , 'x'].each do |invalid|
+ it { is_expected.to run.with_params(invalid).and_raise_error(Puppet::ParseError, /to be an Integer/) }
+ it { is_expected.to run.with_params(invalid, 10).and_raise_error(Puppet::ParseError, /to be an Integer/) }
+ it { is_expected.to run.with_params(invalid, 10, -10).and_raise_error(Puppet::ParseError, /to be an Integer/) }
+ it { is_expected.to run.with_params([0, 1, 2, invalid, 3, 4], 10, -10).and_raise_error(Puppet::ParseError, /to be an Integer/) }
+ end
+
+ context 'when running on modern rubies', :unless => RUBY_VERSION == '1.8.7' do
+ it { is_expected.to run.with_params([0, 1, 2, {1=>2}, 3, 4], 10, -10).and_raise_error(Puppet::ParseError, /to be an Integer/) }
+ end
+
+ context 'when running on ruby, which munges hashes weirdly', :if => RUBY_VERSION == '1.8.7' do
+ it { is_expected.to run.with_params([0, 1, 2, {1=>2}, 3, 4], 10, -10).and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params([0, 1, 2, {0=>2}, 3, 4], 10, -10).and_raise_error(Puppet::ParseError) }
+ end
+
+ it { is_expected.to run.with_params(1, '').and_raise_error(Puppet::ParseError, /to be unset or an Integer/) }
+ it { is_expected.to run.with_params(1, 2, '').and_raise_error(Puppet::ParseError, /to be unset or an Integer/) }
+ it { is_expected.to run.with_params(1, 2, 3).and_raise_error(Puppet::ParseError, /second argument to be larger than third argument/) }
+ end
+
+ context 'with no range constraints' do
+ it { is_expected.to run.with_params(1) }
+ it { is_expected.to run.with_params(-1) }
+ it { is_expected.to run.with_params('1') }
+ it { is_expected.to run.with_params('-1') }
+ it { is_expected.to run.with_params([1, 2, 3, 4]) }
+ it { is_expected.to run.with_params([1, '2', '3', 4]) }
+ end
+
+ context "with a maximum limit of 10" do
+ describe 'rejects numbers greater than the limit' do
+ it { is_expected.to run.with_params(11, 10).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
+ it { is_expected.to run.with_params(100, 10).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
+ it { is_expected.to run.with_params(2**65, 10).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
+ it { is_expected.to run.with_params([1,2,10,100], 10).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
+ end
+
+ describe 'accepts numbers less or equal to the limit' do
+ it { is_expected.to run.with_params(10, 10) }
+ it { is_expected.to run.with_params(1, 10) }
+ it { is_expected.to run.with_params(-1, 10) }
+ it { is_expected.to run.with_params('1', 10) }
+ it { is_expected.to run.with_params('-1', 10) }
+ it { is_expected.to run.with_params([1, 2, 3, 4], 10) }
+ it { is_expected.to run.with_params([1, '2', '3', 4], 10) }
+ end
+
+ context "with a minimum limit of -10" do
+ describe 'rejects numbers greater than the upper limit' do
+ it { is_expected.to run.with_params(11, 10, -10).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
+ it { is_expected.to run.with_params(100, 10, -10).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
+ it { is_expected.to run.with_params(2**65, 10, -10).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
+ it { is_expected.to run.with_params([1,2,10,100], 10, -10).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
+ end
+
+ describe 'rejects numbers smaller than the lower limit' do
+ it { is_expected.to run.with_params(-11, 10, -10).and_raise_error(Puppet::ParseError, /to be greater or equal/) }
+ it { is_expected.to run.with_params(-100, 10, -10).and_raise_error(Puppet::ParseError, /to be greater or equal/) }
+ it { is_expected.to run.with_params(-2**65, 10, -10).and_raise_error(Puppet::ParseError, /to be greater or equal/) }
+ it { is_expected.to run.with_params([-10, 1,2,10,-100], 10, -10).and_raise_error(Puppet::ParseError, /to be greater or equal/) }
+ end
+
+ describe 'accepts numbers between and including the limits' do
+ it { is_expected.to run.with_params(10, 10, -10) }
+ it { is_expected.to run.with_params(-10, 10, -10) }
+ it { is_expected.to run.with_params(1, 10, -10) }
+ it { is_expected.to run.with_params(-1, 10, -10) }
+ it { is_expected.to run.with_params('1', 10, -10) }
+ it { is_expected.to run.with_params('-1', 10, -10) }
+ it { is_expected.to run.with_params([1, 2, 3, 4], 10, -10) }
+ it { is_expected.to run.with_params([1, '2', '3', 4], 10, -10) }
+ end
+ end
+ end
+
+ it { is_expected.to run.with_params(10, 10, 10) }
+
+ describe 'empty upper limit is interpreted as infinity' do
+ it { is_expected.to run.with_params(11, '', 10) }
+ end
+end
diff --git a/spec/functions/validate_ip_address_spec.rb b/spec/functions/validate_ip_address_spec.rb
new file mode 100644
index 0000000..0414f5e
--- /dev/null
+++ b/spec/functions/validate_ip_address_spec.rb
@@ -0,0 +1,65 @@
+require 'spec_helper'
+
+describe 'validate_ip_address' do
+
+ describe 'signature validation' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ end
+
+ describe 'valid inputs' do
+ it { is_expected.to run.with_params('0.0.0.0') }
+ it { is_expected.to run.with_params('8.8.8.8') }
+ it { is_expected.to run.with_params('127.0.0.1') }
+ it { is_expected.to run.with_params('10.10.10.10') }
+ it { is_expected.to run.with_params('194.232.104.150') }
+ it { is_expected.to run.with_params('244.24.24.24') }
+ it { is_expected.to run.with_params('255.255.255.255') }
+ it { is_expected.to run.with_params('1.2.3.4', '5.6.7.8') }
+ it { is_expected.to run.with_params('3ffe:0505:0002::') }
+ it { is_expected.to run.with_params('3ffe:0505:0002::', '3ffe:0505:0002::2') }
+ it { is_expected.to run.with_params('::1/64') }
+ it { is_expected.to run.with_params('fe80::a00:27ff:fe94:44d6/64') }
+
+ context 'Checking for deprecation warning', if: Puppet.version.to_f < 4.0 do
+ after(:all) do
+ ENV.delete('STDLIB_LOG_DEPRECATIONS')
+ end
+ # Checking for deprecation warning, which should only be provoked when the env variable for it is set.
+ it 'should display a single deprecation' do
+ ENV['STDLIB_LOG_DEPRECATIONS'] = "true"
+ scope.expects(:warning).with(includes('This method is deprecated'))
+ is_expected.to run.with_params('1.2.3.4')
+ end
+ it 'should display no warning for deprecation' do
+ ENV['STDLIB_LOG_DEPRECATIONS'] = "false"
+ scope.expects(:warning).with(includes('This method is deprecated')).never
+ is_expected.to run.with_params('1.2.3.4')
+ end
+ end
+
+ context 'with netmasks' do
+ it { is_expected.to run.with_params('8.8.8.8/0') }
+ it { is_expected.to run.with_params('8.8.8.8/16') }
+ it { is_expected.to run.with_params('8.8.8.8/32') }
+ it { is_expected.to run.with_params('8.8.8.8/255.255.0.0') }
+ end
+ end
+
+ describe 'invalid inputs' do
+ it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, /is not a string/) }
+ it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, /is not a string/) }
+ it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, /is not a string/) }
+ it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, /is not a valid IP/) }
+ it { is_expected.to run.with_params('0.0.0').and_raise_error(Puppet::ParseError, /is not a valid IP/) }
+ it { is_expected.to run.with_params('0.0.0.256').and_raise_error(Puppet::ParseError, /is not a valid IP/) }
+ it { is_expected.to run.with_params('0.0.0.0.0').and_raise_error(Puppet::ParseError, /is not a valid IP/) }
+ it { is_expected.to run.with_params('1.2.3.4', {}).and_raise_error(Puppet::ParseError, /is not a string/) }
+ it { is_expected.to run.with_params('1.2.3.4', 1).and_raise_error(Puppet::ParseError, /is not a string/) }
+ it { is_expected.to run.with_params('1.2.3.4', true).and_raise_error(Puppet::ParseError, /is not a string/) }
+ it { is_expected.to run.with_params('1.2.3.4', 'one').and_raise_error(Puppet::ParseError, /is not a valid IP/) }
+ it { is_expected.to run.with_params('::1', {}).and_raise_error(Puppet::ParseError, /is not a string/) }
+ it { is_expected.to run.with_params('::1', true).and_raise_error(Puppet::ParseError, /is not a string/) }
+ it { is_expected.to run.with_params('::1', 'one').and_raise_error(Puppet::ParseError, /is not a valid IP/) }
+ end
+end
diff --git a/spec/functions/validate_ipv4_address_spec.rb b/spec/functions/validate_ipv4_address_spec.rb
index 45401a4..6e4ca05 100755
--- a/spec/functions/validate_ipv4_address_spec.rb
+++ b/spec/functions/validate_ipv4_address_spec.rb
@@ -1,64 +1,41 @@
-#! /usr/bin/env ruby -S rspec
+require 'spec_helper'
-require "spec_helper"
+describe 'validate_ipv4_address' do
-describe Puppet::Parser::Functions.function(:validate_ipv4_address) do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- describe "when calling validate_ipv4_address from puppet" do
- describe "when given IPv4 address strings" do
- it "should compile with one argument" do
- Puppet[:code] = "validate_ipv4_address('1.2.3.4')"
- scope.compiler.compile
- end
+ describe 'signature validation' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ end
- it "should compile with multiple arguments" do
- Puppet[:code] = "validate_ipv4_address('1.2.3.4', '5.6.7.8')"
- scope.compiler.compile
- end
+ context 'Checking for deprecation warning', if: Puppet.version.to_f < 4.0 do
+ after(:all) do
+ ENV.delete('STDLIB_LOG_DEPRECATIONS')
end
-
- describe "when given an IPv6 address" do
- it "should not compile" do
- Puppet[:code] = "validate_ipv4_address('3ffe:505')"
- expect {
- scope.compiler.compile
- }.to raise_error(Puppet::ParseError, /not a valid IPv4 address/)
- end
+ # Checking for deprecation warning, which should only be provoked when the env variable for it is set.
+ it 'should display a single deprecation' do
+ ENV['STDLIB_LOG_DEPRECATIONS'] = "true"
+ scope.expects(:warning).with(includes('This method is deprecated'))
+ is_expected.to run.with_params(SharedData::IPV4_PATTERNS.first)
end
-
- describe "when given other strings" do
- it "should not compile" do
- Puppet[:code] = "validate_ipv4_address('hello', 'world')"
- expect {
- scope.compiler.compile
- }.to raise_error(Puppet::ParseError, /not a valid IPv4 address/)
- end
+ it 'should display no warning for deprecation' do
+ ENV['STDLIB_LOG_DEPRECATIONS'] = "false"
+ scope.expects(:warning).with(includes('This method is deprecated')).never
+ is_expected.to run.with_params(SharedData::IPV4_PATTERNS.first)
end
+ end
- describe "when given numbers" do
- it "should not compile" do
- Puppet[:code] = "validate_ipv4_address(1, 2)"
- expect {
- scope.compiler.compile
- }.to raise_error(Puppet::ParseError, /is not a valid IPv4 address/)
- end
- end
+ SharedData::IPV4_PATTERNS.each do |value|
+ it { is_expected.to run.with_params(value) }
+ end
- describe "when given booleans" do
- it "should not compile" do
- Puppet[:code] = "validate_ipv4_address(true, false)"
- expect {
- scope.compiler.compile
- }.to raise_error(Puppet::ParseError, /is not a string/)
- end
- end
+ SharedData::IPV4_NEGATIVE_PATTERNS.each do |value|
+ it { is_expected.to run.with_params(value).and_raise_error(Puppet::ParseError, /is not a valid IPv4/) }
+ end
- it "should not compile when no arguments are passed" do
- Puppet[:code] = "validate_ipv4_address()"
- expect {
- scope.compiler.compile
- }.to raise_error(Puppet::ParseError, /wrong number of arguments/)
+ describe 'invalid inputs' do
+ [ {}, [], 1, true ].each do |invalid|
+ it { is_expected.to run.with_params(invalid).and_raise_error(Puppet::ParseError, /is not a string/) }
+ it { is_expected.to run.with_params(SharedData::IPV4_PATTERNS.first, invalid).and_raise_error(Puppet::ParseError, /is not a string/) }
end
end
end
diff --git a/spec/functions/validate_ipv6_address_spec.rb b/spec/functions/validate_ipv6_address_spec.rb
index a839d90..78810d4 100755
--- a/spec/functions/validate_ipv6_address_spec.rb
+++ b/spec/functions/validate_ipv6_address_spec.rb
@@ -1,67 +1,50 @@
-#! /usr/bin/env ruby -S rspec
+require 'spec_helper'
-require "spec_helper"
-
-describe Puppet::Parser::Functions.function(:validate_ipv6_address) do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- describe "when calling validate_ipv6_address from puppet" do
- describe "when given IPv6 address strings" do
- it "should compile with one argument" do
- Puppet[:code] = "validate_ipv6_address('3ffe:0505:0002::')"
- scope.compiler.compile
- end
+describe 'validate_ipv6_address' do
+
+ describe 'signature validation' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ end
- it "should compile with multiple arguments" do
- Puppet[:code] = "validate_ipv6_address('3ffe:0505:0002::', '3ffe:0505:0001::')"
- scope.compiler.compile
- end
+ context 'Checking for deprecation warning', if: Puppet.version.to_f < 4.0 do
+ after(:all) do
+ ENV.delete('STDLIB_LOG_DEPRECATIONS')
end
-
- describe "when given an ipv4 address" do
- it "should not compile" do
- Puppet[:code] = "validate_ipv6_address('1.2.3.4')"
- expect {
- scope.compiler.compile
- }.to raise_error(Puppet::ParseError, /not a valid IPv6 address/)
- end
+ # Checking for deprecation warning, which should only be provoked when the env variable for it is set.
+ it 'should display a single deprecation' do
+ ENV['STDLIB_LOG_DEPRECATIONS'] = "true"
+ scope.expects(:warning).with(includes('This method is deprecated'))
+ is_expected.to run.with_params('3ffe:0505:0002::')
end
-
- describe "when given other strings" do
- it "should not compile" do
- Puppet[:code] = "validate_ipv6_address('hello', 'world')"
- expect {
- scope.compiler.compile
- }.to raise_error(Puppet::ParseError, /not a valid IPv6 address/)
- end
+ it 'should display no warning for deprecation' do
+ ENV['STDLIB_LOG_DEPRECATIONS'] = "false"
+ scope.expects(:warning).with(includes('This method is deprecated')).never
+ is_expected.to run.with_params('3ffe:0505:0002::')
end
+ end
- # 1.8.7 is EOL'd and also absolutely insane about ipv6
- unless RUBY_VERSION == '1.8.7'
- describe "when given numbers" do
- it "should not compile" do
- Puppet[:code] = "validate_ipv6_address(1, 2)"
- expect {
- scope.compiler.compile
- }.to raise_error(Puppet::ParseError, /not a valid IPv6 address/)
- end
- end
- end
-
- describe "when given booleans" do
- it "should not compile" do
- Puppet[:code] = "validate_ipv6_address(true, false)"
- expect {
- scope.compiler.compile
- }.to raise_error(Puppet::ParseError, /is not a string/)
- end
- end
+ describe 'valid inputs' do
+ it { is_expected.to run.with_params('3ffe:0505:0002::') }
+ it { is_expected.to run.with_params('3ffe:0505:0002::', '3ffe:0505:0002::2') }
+ it { is_expected.to run.with_params('::1/64') }
+ it { is_expected.to run.with_params('fe80::a00:27ff:fe94:44d6/64') }
+ end
- it "should not compile when no arguments are passed" do
- Puppet[:code] = "validate_ipv6_address()"
- expect {
- scope.compiler.compile
- }.to raise_error(Puppet::ParseError, /wrong number of arguments/)
+ describe 'invalid inputs' do
+ it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, /is not a string/) }
+ it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, /is not a string/) }
+ it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, /is not a valid IPv6/) }
+ it { is_expected.to run.with_params('0.0.0').and_raise_error(Puppet::ParseError, /is not a valid IPv6/) }
+ it { is_expected.to run.with_params('0.0.0.256').and_raise_error(Puppet::ParseError, /is not a valid IPv6/) }
+ it { is_expected.to run.with_params('0.0.0.0.0').and_raise_error(Puppet::ParseError, /is not a valid IPv6/) }
+ it { is_expected.to run.with_params('affe:beef').and_raise_error(Puppet::ParseError, /is not a valid IPv6/) }
+ it { is_expected.to run.with_params('::1', {}).and_raise_error(Puppet::ParseError, /is not a string/) }
+ it { is_expected.to run.with_params('::1', true).and_raise_error(Puppet::ParseError, /is not a string/) }
+ it { is_expected.to run.with_params('::1', 'one').and_raise_error(Puppet::ParseError, /is not a valid IPv6/) }
+ context 'unless running on ruby 1.8.7', :if => RUBY_VERSION != '1.8.7' do
+ it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, /is not a string/) }
+ it { is_expected.to run.with_params('::1', 1).and_raise_error(Puppet::ParseError, /is not a string/) }
end
end
end
diff --git a/spec/functions/validate_legacy_spec.rb b/spec/functions/validate_legacy_spec.rb
new file mode 100644
index 0000000..50cb317
--- /dev/null
+++ b/spec/functions/validate_legacy_spec.rb
@@ -0,0 +1,68 @@
+require 'spec_helper'
+
+if Puppet.version.to_f >= 4.0
+ describe 'validate_legacy' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params.and_raise_error(ArgumentError) }
+
+ describe 'when passing the type assertion and passing the previous validation' do
+ before do
+ scope.expects(:function_validate_foo).with([5]).once
+ Puppet.expects(:notice).never
+ end
+ it 'passes without notice' do
+ is_expected.to run.with_params('Integer', 'validate_foo', 5)
+ end
+ end
+
+ describe 'when passing the type assertion and failing the previous validation' do
+ before do
+ scope.expects(:function_validate_foo).with([5]).raises(Puppet::ParseError, 'foo').once
+ Puppet.expects(:notice).with(includes('Accepting previously invalid value for target type'))
+ end
+ it 'passes with a notice about newly accepted value' do
+ is_expected.to run.with_params('Integer', 'validate_foo', 5)
+ end
+ end
+
+ describe 'when failing the type assertion and passing the previous validation' do
+ before do
+ scope.expects(:function_validate_foo).with(['5']).once
+ subject.func.expects(:call_function).with('deprecation', 'validate_legacy', includes('Integer')).once
+ end
+ it 'passes with a deprecation message' do
+ is_expected.to run.with_params('Integer', 'validate_foo', '5')
+ end
+ end
+
+ describe 'when failing the type assertion and failing the previous validation' do
+ before do
+ scope.expects(:function_validate_foo).with(['5']).raises(Puppet::ParseError, 'foo').once
+ subject.func.expects(:call_function).with('fail', includes('Integer')).once
+ end
+ it 'fails with a helpful message' do
+ is_expected.to run.with_params('Integer', 'validate_foo', '5')
+ end
+ end
+
+ describe 'when passing in undef' do
+ before do
+ scope.expects(:function_validate_foo).with([:undef]).once
+ Puppet.expects(:notice).never
+ end
+ it 'works' do
+ is_expected.to run.with_params('Optional[Integer]', 'validate_foo', :undef)
+ end
+ end
+
+ describe 'when passing in multiple arguments' do
+ before do
+ scope.expects(:function_validate_foo).with([:undef, 1, 'foo']).once
+ Puppet.expects(:notice).never
+ end
+ it 'passes with a deprecation message' do
+ is_expected.to run.with_params('Optional[Integer]', 'validate_foo', :undef, 1, 'foo')
+ end
+ end
+ end
+end
diff --git a/spec/functions/validate_numeric_spec.rb b/spec/functions/validate_numeric_spec.rb
new file mode 100755
index 0000000..4c0e24d
--- /dev/null
+++ b/spec/functions/validate_numeric_spec.rb
@@ -0,0 +1,100 @@
+require 'spec_helper'
+
+describe 'validate_numeric' do
+ after(:all) do
+ ENV.delete('STDLIB_LOG_DEPRECATIONS')
+ end
+
+ # Checking for deprecation warning
+ it 'should display a single deprecation' do
+ ENV['STDLIB_LOG_DEPRECATIONS'] = "true"
+ scope.expects(:warning).with(includes('This method is deprecated'))
+ is_expected.to run.with_params(3)
+ end
+
+ describe 'signature validation' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params(1, 2, 3, 4).and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+
+ [ true, 'true', false, 'false', 'iAmAString', '1test', '1 test', 'test 1', 'test 1 test', {}, { 'key' => 'value' }, { 1=> 2 }, '', :undef , 'x'].each do |invalid|
+ it { is_expected.to run.with_params(invalid).and_raise_error(Puppet::ParseError, /to be a Numeric/) }
+ it { is_expected.to run.with_params(invalid, 10.0).and_raise_error(Puppet::ParseError, /to be a Numeric/) }
+ it { is_expected.to run.with_params(invalid, 10.0, -10.0).and_raise_error(Puppet::ParseError, /to be a Numeric/) }
+ end
+
+ context 'when running on modern rubies', :unless => RUBY_VERSION == '1.8.7' do
+ it { is_expected.to run.with_params([0, 1, 2, {1=>2}, 3, 4], 10, -10).and_raise_error(Puppet::ParseError, /to be a Numeric/) }
+ end
+
+ context 'when running on ruby, which munges hashes weirdly', :if => RUBY_VERSION == '1.8.7' do
+ it { is_expected.to run.with_params([0, 1, 2, {1=>2}, 3, 4], 10, -10).and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params([0, 1, 2, {0=>2}, 3, 4], 10, -10).and_raise_error(Puppet::ParseError) }
+ end
+
+ it { is_expected.to run.with_params(1, '').and_raise_error(Puppet::ParseError, /to be unset or a Numeric/) }
+ it { is_expected.to run.with_params(1, 2, '').and_raise_error(Puppet::ParseError, /to be unset or a Numeric/) }
+ it { is_expected.to run.with_params(1, 2, 3).and_raise_error(Puppet::ParseError, /second argument to be larger than third argument/) }
+ end
+
+ context 'with no range constraints' do
+ it { is_expected.to run.with_params(1) }
+ it { is_expected.to run.with_params(-1) }
+ it { is_expected.to run.with_params('1') }
+ it { is_expected.to run.with_params('-1') }
+ it { is_expected.to run.with_params([1, 2, 3, 4]) }
+ it { is_expected.to run.with_params([1, '2', '3', 4]) }
+ end
+
+ context "with a maximum limit of 10.0" do
+ describe 'rejects numbers greater than the limit' do
+ it { is_expected.to run.with_params(11, 10.0).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
+ it { is_expected.to run.with_params(100, 10.0).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
+ it { is_expected.to run.with_params(2**65, 10.0).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
+ it { is_expected.to run.with_params([1,2,10.0,100], 10.0).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
+ end
+
+ describe 'accepts numbers less or equal to the limit' do
+ it { is_expected.to run.with_params(10.0, 10.0) }
+ it { is_expected.to run.with_params(1, 10.0) }
+ it { is_expected.to run.with_params(-1, 10.0) }
+ it { is_expected.to run.with_params('1', 10.0) }
+ it { is_expected.to run.with_params('-1', 10.0) }
+ it { is_expected.to run.with_params([1, 2, 3, 4], 10.0) }
+ it { is_expected.to run.with_params([1, '2', '3', 4], 10.0) }
+ end
+
+ context "with a minimum limit of -10.0" do
+ describe 'rejects numbers greater than the upper limit' do
+ it { is_expected.to run.with_params(11, 10.0, -10.0).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
+ it { is_expected.to run.with_params(100, 10.0, -10.0).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
+ it { is_expected.to run.with_params(2**65, 10.0, -10.0).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
+ it { is_expected.to run.with_params([1,2,10.0,100], 10.0, -10.0).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
+ end
+
+ describe 'rejects numbers smaller than the lower limit' do
+ it { is_expected.to run.with_params(-11, 10.0, -10.0).and_raise_error(Puppet::ParseError, /to be greater or equal/) }
+ it { is_expected.to run.with_params(-100, 10.0, -10.0).and_raise_error(Puppet::ParseError, /to be greater or equal/) }
+ it { is_expected.to run.with_params(-2**65, 10.0, -10.0).and_raise_error(Puppet::ParseError, /to be greater or equal/) }
+ it { is_expected.to run.with_params([-10.0, 1,2,10.0,-100], 10.0, -10.0).and_raise_error(Puppet::ParseError, /to be greater or equal/) }
+ end
+
+ describe 'accepts numbers between and including the limits' do
+ it { is_expected.to run.with_params(10.0, 10.0, -10.0) }
+ it { is_expected.to run.with_params(-10.0, 10.0, -10.0) }
+ it { is_expected.to run.with_params(1, 10.0, -10.0) }
+ it { is_expected.to run.with_params(-1, 10.0, -10.0) }
+ it { is_expected.to run.with_params('1', 10.0, -10.0) }
+ it { is_expected.to run.with_params('-1', 10.0, -10.0) }
+ it { is_expected.to run.with_params([1, 2, 3, 4], 10.0, -10.0) }
+ it { is_expected.to run.with_params([1, '2', '3', 4], 10.0, -10.0) }
+ end
+ end
+ end
+
+ it { is_expected.to run.with_params(10.0, 10.0, 10.0) }
+
+ describe 'empty upper limit is interpreted as infinity' do
+ it { is_expected.to run.with_params(11, '', 10.0) }
+ end
+end
diff --git a/spec/functions/validate_re_spec.rb b/spec/functions/validate_re_spec.rb
index d29988b..3531182 100755
--- a/spec/functions/validate_re_spec.rb
+++ b/spec/functions/validate_re_spec.rb
@@ -1,75 +1,54 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe Puppet::Parser::Functions.function(:validate_re) do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- # The subject of these examplres is the method itself.
- subject do
- # This makes sure the function is loaded within each test
- function_name = Puppet::Parser::Functions.function(:validate_re)
- scope.method(function_name)
+describe 'validate_re' do
+ after(:all) do
+ ENV.delete('STDLIB_LOG_DEPRECATIONS')
end
- context 'Using Puppet::Parser::Scope.new' do
-
- describe 'Garbage inputs' do
- inputs = [
- [ nil ],
- [ [ nil ] ],
- [ { 'foo' => 'bar' } ],
- [ { } ],
- [ '' ],
- [ "one", "one", "MSG to User", "4th arg" ],
- ]
-
- inputs.each do |input|
- it "validate_re(#{input.inspect}) should fail" do
- expect { subject.call [input] }.to raise_error Puppet::ParseError
- end
- end
- end
-
- describe 'Valid inputs' do
- inputs = [
- [ '/full/path/to/something', '^/full' ],
- [ '/full/path/to/something', 'full' ],
- [ '/full/path/to/something', ['full', 'absent'] ],
- [ '/full/path/to/something', ['full', 'absent'], 'Message to the user' ],
- ]
-
- inputs.each do |input|
- it "validate_re(#{input.inspect}) should not fail" do
- expect { subject.call input }.not_to raise_error
- end
- end
- end
- describe "Valid inputs which should raise an exception without a message" do
- # The intent here is to make sure valid inputs raise exceptions when they
- # don't specify an error message to display. This is the behvior in
- # 2.2.x and prior.
- inputs = [
- [ "hello", [ "bye", "later", "adios" ] ],
- [ "greetings", "salutations" ],
- ]
+ # Checking for deprecation warning
+ it 'should display a single deprecation' do
+ ENV['STDLIB_LOG_DEPRECATIONS'] = "true"
+ scope.expects(:warning).with(includes('This method is deprecated'))
+ is_expected.to run.with_params('', '')
+ end
- inputs.each do |input|
- it "validate_re(#{input.inspect}) should fail" do
- expect { subject.call input }.to raise_error /validate_re.*?does not match/
- end
- end
+ describe 'signature validation' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params('').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params('', '', '', 'extra').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+
+ describe 'valid inputs' do
+ it { is_expected.to run.with_params('', '') }
+ it { is_expected.to run.with_params('', ['']) }
+ it { is_expected.to run.with_params('', [''], 'custom error') }
+ it { is_expected.to run.with_params('one', '^one') }
+ it { is_expected.to run.with_params('one', [ '^one', '^two' ]) }
+ it { is_expected.to run.with_params('one', [ '^one', '^two' ], 'custom error') }
end
- describe "Nicer Error Messages" do
- # The intent here is to make sure the function returns the 3rd argument
- # in the exception thrown
- inputs = [
- [ "hello", [ "bye", "later", "adios" ], "MSG to User" ],
- [ "greetings", "salutations", "Error, greetings does not match salutations" ],
- ]
- inputs.each do |input|
- it "validate_re(#{input.inspect}) should fail" do
- expect { subject.call input }.to raise_error /#{input[2]}/
+ describe 'invalid inputs' do
+ it { is_expected.to run.with_params('', []).and_raise_error(Puppet::ParseError, /does not match/) }
+ it { is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError, /does not match/) }
+ it { is_expected.to run.with_params('', 'two').and_raise_error(Puppet::ParseError, /does not match/) }
+ it { is_expected.to run.with_params('', ['two']).and_raise_error(Puppet::ParseError, /does not match/) }
+ it { is_expected.to run.with_params('', ['two'], 'custom error').and_raise_error(Puppet::ParseError, /custom error/) }
+ it { is_expected.to run.with_params('notone', '^one').and_raise_error(Puppet::ParseError, /does not match/) }
+ it { is_expected.to run.with_params('notone', [ '^one', '^two' ]).and_raise_error(Puppet::ParseError, /does not match/) }
+ it { is_expected.to run.with_params('notone', [ '^one', '^two' ], 'custom error').and_raise_error(Puppet::ParseError, /custom error/) }
+
+ describe 'non-string inputs' do
+ [
+ 1, # Fixnum
+ 3.14, # Float
+ nil, # NilClass
+ true, # TrueClass
+ false, # FalseClass
+ ["10"], # Array
+ :key, # Symbol
+ {:key=>"val"}, # Hash
+ ].each do |input|
+ it { is_expected.to run.with_params(input, '.*').and_raise_error(Puppet::ParseError, /needs to be a String/) }
end
end
end
diff --git a/spec/functions/validate_slength_spec.rb b/spec/functions/validate_slength_spec.rb
index e23f61a..e4162de 100755
--- a/spec/functions/validate_slength_spec.rb
+++ b/spec/functions/validate_slength_spec.rb
@@ -1,67 +1,72 @@
-#! /usr/bin/env ruby -S rspec
-
require 'spec_helper'
-describe "the validate_slength function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- it "should exist" do
- expect(Puppet::Parser::Functions.function("validate_slength")).to eq("function_validate_slength")
+describe 'validate_slength' do
+ after(:all) do
+ ENV.delete('STDLIB_LOG_DEPRECATIONS')
end
- describe "validating the input argument types" do
- it "raises an error if there are less than two arguments" do
- expect { scope.function_validate_slength([]) }.to raise_error Puppet::ParseError, /Wrong number of arguments/
- end
-
- it "raises an error if there are more than three arguments" do
- expect { scope.function_validate_slength(['input', 1, 2, 3]) }.to raise_error Puppet::ParseError, /Wrong number of arguments/
- end
-
- it "raises an error if the first argument is not a string" do
- expect { scope.function_validate_slength([Object.new, 2, 1]) }.to raise_error Puppet::ParseError, /Expected first argument.*got .*Object/
- end
+ # Checking for deprecation warning
+ it 'should display a single deprecation' do
+ ENV['STDLIB_LOG_DEPRECATIONS'] = "true"
+ scope.expects(:warning).with(includes('This method is deprecated'))
+ is_expected.to run.with_params('1234567890', 10)
+ end
+
+ describe 'signature validation' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params('').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params('', 2, 3, 'extra').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params('', '').and_raise_error(Puppet::ParseError, /second argument to be a positive Numeric/) }
+ it { is_expected.to run.with_params('', -1).and_raise_error(Puppet::ParseError, /second argument to be a positive Numeric/) }
+ it { is_expected.to run.with_params('', 1, '').and_raise_error(Puppet::ParseError, /third argument to be unset or a positive Numeric/) }
+ it { is_expected.to run.with_params('', 1, -1).and_raise_error(Puppet::ParseError, /third argument to be unset or a positive Numeric/) }
+ it { is_expected.to run.with_params('', 1, 2).and_raise_error(Puppet::ParseError, /argument to be equal to or larger than third argument/) }
+ end
- it "raises an error if the second argument cannot be cast to an Integer" do
- expect { scope.function_validate_slength(['input', Object.new]) }.to raise_error Puppet::ParseError, /Expected second argument.*got .*Object/
+ context "with a maximum length of 10" do
+ describe 'rejects strings longer than the limit' do
+ it { is_expected.to run.with_params('1234567890a', 10).and_raise_error(Puppet::ParseError, /Expected length/) }
+ it { is_expected.to run.with_params('1234567890abcdef', 10).and_raise_error(Puppet::ParseError, /Expected length/) }
+ it { is_expected.to run.with_params([ 'one', '1234567890abcdef' ], 10).and_raise_error(Puppet::ParseError, /Expected length/) }
end
- it "raises an error if the third argument cannot be cast to an Integer" do
- expect { scope.function_validate_slength(['input', 1, Object.new]) }.to raise_error Puppet::ParseError, /Expected third argument.*got .*Object/
+ describe 'accepts strings shorter or equal to the limit' do
+ it { is_expected.to run.with_params('1234567890', 10) }
+ it { is_expected.to run.with_params('12345', 10) }
+ it { is_expected.to run.with_params([ 'one', 'two' ], 10) }
end
- it "raises an error if the second argument is smaller than the third argument" do
- expect { scope.function_validate_slength(['input', 1, 2]) }.to raise_error Puppet::ParseError, /Expected second argument to be larger than third argument/
- end
- end
-
- describe "validating the input string length" do
- describe "when the input is a string" do
- it "fails validation if the string is larger than the max length" do
- expect { scope.function_validate_slength(['input', 1]) }.to raise_error Puppet::ParseError, /Expected length .* between 0 and 1, was 5/
- end
-
- it "fails validation if the string is less than the min length" do
- expect { scope.function_validate_slength(['input', 10, 6]) }.to raise_error Puppet::ParseError, /Expected length .* between 6 and 10, was 5/
- end
-
- it "doesn't raise an error if the string is under the max length" do
- scope.function_validate_slength(['input', 10])
+ context "with a minimum length of 5" do
+ describe 'rejects strings longer than the upper limit' do
+ it { is_expected.to run.with_params('1234567890a', 10, 5).and_raise_error(Puppet::ParseError, /Expected length/) }
+ it { is_expected.to run.with_params('1234567890abcdef', 10, 5).and_raise_error(Puppet::ParseError, /Expected length/) }
end
- it "doesn't raise an error if the string is equal to the max length" do
- scope.function_validate_slength(['input', 5])
+ describe 'rejects numbers shorter than the lower limit' do
+ it { is_expected.to run.with_params('one', 10, 5).and_raise_error(Puppet::ParseError, /Expected length/) }
+ it { is_expected.to run.with_params(['12345678', 'two'], 10, 5).and_raise_error(Puppet::ParseError, /Expected length/) }
end
- it "doesn't raise an error if the string is equal to the min length" do
- scope.function_validate_slength(['input', 10, 5])
+ describe 'accepts strings of length between and including the limits' do
+ it { is_expected.to run.with_params('12345', 10, 5) }
+ it { is_expected.to run.with_params('123456', 10, 5) }
+ it { is_expected.to run.with_params('1234567', 10, 5) }
+ it { is_expected.to run.with_params('12345678', 10, 5) }
+ it { is_expected.to run.with_params('123456789', 10, 5) }
+ it { is_expected.to run.with_params('1234567890', 10, 5) }
+ it { is_expected.to run.with_params(['1233456', '12345678'], 10, 5) }
end
end
+ end
- describe "when the input is an array" do
- it "fails validation if one of the array elements is not a string" do
- expect { scope.function_validate_slength([["a", "b", Object.new], 2]) }.to raise_error Puppet::ParseError, /Expected element at array position 2 .*String, got .*Object/
- end
- end
+ describe 'corner cases' do
+ it { pending('this should work'); is_expected.to run.with_params('', 0, 0) }
+ it { is_expected.to run.with_params('1234567890', 10, 10) }
+ end
+
+ describe 'empty upper limit is interpreted as infinity' do
+ it { pending('not implemented'); is_expected.to run.with_params('1234567890ab', '', 10) }
+ it { pending('not implemented'); is_expected.to run.with_params('12345678', '', 10).and_raise_error(Puppet::ParseError, /Expected length/) }
end
end
diff --git a/spec/functions/validate_string_spec.rb b/spec/functions/validate_string_spec.rb
index 3b4fb3e..0907ede 100755
--- a/spec/functions/validate_string_spec.rb
+++ b/spec/functions/validate_string_spec.rb
@@ -1,60 +1,33 @@
-#! /usr/bin/env ruby -S rspec
-
require 'spec_helper'
-describe Puppet::Parser::Functions.function(:validate_string) do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- describe 'when calling validate_string from puppet' do
-
- %w{ foo bar baz }.each do |the_string|
-
- it "should compile when #{the_string} is a string" do
- Puppet[:code] = "validate_string('#{the_string}')"
- scope.compiler.compile
- end
-
- it "should compile when #{the_string} is a bare word" do
- Puppet[:code] = "validate_string(#{the_string})"
- scope.compiler.compile
- end
-
- end
-
- %w{ true false }.each do |the_string|
- it "should compile when #{the_string} is a string" do
- Puppet[:code] = "validate_string('#{the_string}')"
- scope.compiler.compile
- end
-
- it "should not compile when #{the_string} is a bare word" do
- Puppet[:code] = "validate_string(#{the_string})"
- expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is not a string/)
- end
- end
+describe 'validate_string' do
+ after(:all) do
+ ENV.delete('STDLIB_LOG_DEPRECATIONS')
+ end
+
+ # Checking for deprecation warning
+ it 'should display a single deprecation' do
+ ENV['STDLIB_LOG_DEPRECATIONS'] = "true"
+ scope.expects(:warning).with(includes('This method is deprecated'))
+ is_expected.to run.with_params('', '')
+ end
- it "should compile when multiple string arguments are passed" do
- Puppet[:code] = <<-'ENDofPUPPETcode'
- $foo = ''
- $bar = 'two'
- validate_string($foo, $bar)
- ENDofPUPPETcode
- scope.compiler.compile
- end
+ describe 'signature validation' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
- it "should compile when an explicitly undef variable is passed (NOTE THIS MAY NOT BE DESIRABLE)" do
- Puppet[:code] = <<-'ENDofPUPPETcode'
- $foo = undef
- validate_string($foo)
- ENDofPUPPETcode
- scope.compiler.compile
+ describe 'valid inputs' do
+ it { is_expected.to run.with_params('') }
+ it { is_expected.to run.with_params(nil) }
+ it { is_expected.to run.with_params('one') }
+ it { is_expected.to run.with_params('one', 'two') }
end
- it "should compile when an undefined variable is passed (NOTE THIS MAY NOT BE DESIRABLE)" do
- Puppet[:code] = <<-'ENDofPUPPETcode'
- validate_string($foobarbazishouldnotexist)
- ENDofPUPPETcode
- scope.compiler.compile
+ describe 'invalid inputs' do
+ it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError, /is not a string/) }
+ it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, /is not a string/) }
+ it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, /is not a string/) }
+ it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, /is not a string/) }
end
end
end
diff --git a/spec/functions/validate_x509_rsa_key_pair_spec.rb b/spec/functions/validate_x509_rsa_key_pair_spec.rb
new file mode 100755
index 0000000..eb63310
--- /dev/null
+++ b/spec/functions/validate_x509_rsa_key_pair_spec.rb
@@ -0,0 +1,180 @@
+require 'spec_helper'
+
+describe 'validate_x509_rsa_key_pair' do
+
+ let(:valid_cert) do
+ <<EOS
+-----BEGIN CERTIFICATE-----
+MIIC9jCCAeCgAwIBAgIRAK11n3X7aypJ7FPM8UFyAeowCwYJKoZIhvcNAQELMBIx
+EDAOBgNVBAoTB0FjbWUgQ28wHhcNMTUxMTIzMjIzOTU4WhcNMTYxMTIyMjIzOTU4
+WjASMRAwDgYDVQQKEwdBY21lIENvMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIB
+CgKCAQEAz9bY/piKahD10AiJSfbI2A8NG5UwRz0r9T/WfvNVdhgrsGFgNQjvpUoZ
+nNJpQIHBbgMOiXqfATFjJl5FjEkSf7GUHohlGVls9MX2JmVvknzsiitd75H/EJd+
+N+k915lix8Vqmj8d1CTlbF/8tEjzANI67Vqw5QTuqebO7rkIUvRg6yiRfSo75FK1
+RinCJyl++kmleBwQZBInQyg95GvJ5JTqMzBs67DeeyzskDhTeTePRYVF2NwL8QzY
+htvLIBERTNsyU5i7nkxY5ptUwgFUwd93LH4Q19tPqL5C5RZqXxhE51thOOwafm+a
+W/cRkqYqV+tv+j1jJ3WICyF1JNW0BQIDAQABo0swSTAOBgNVHQ8BAf8EBAMCAKAw
+EwYDVR0lBAwwCgYIKwYBBQUHAwEwDAYDVR0TAQH/BAIwADAUBgNVHREEDTALggls
+b2NhbGhvc3QwCwYJKoZIhvcNAQELA4IBAQAzRo0hpVTrFQZLIXpwvKwZVGvJdCkV
+P95DTsSk/VTGV+/YtxrRqks++hJZnctm2PbnTsCAoIP3AMx+vicCKiKrxvpsLU8/
++6cowUbcuGMdSQktwDqbAgEhQlLsETll06w1D/KC+ejOc4+LRn3GQcEyGDtMk/EX
+IeAvBZHr4/kVXWnfo6kzCLcku1f8yE/yDEFClZe9XV1Lk/s+3YfXVtNnMJJ1giZI
+QVOe6CkmuQq+4AtIeW8aLkvlfp632jag1F77a1y+L268koKkj0hBMrtcErVQaxmq
+xym0+soR4Tk4pTIGckeFglrLxkP2JpM/yTwSEAVlmG9vgTliYKyR0uMl
+-----END CERTIFICATE-----
+EOS
+ end
+
+ let(:valid_key) do
+ <<EOS
+-----BEGIN RSA PRIVATE KEY-----
+MIIEogIBAAKCAQEAz9bY/piKahD10AiJSfbI2A8NG5UwRz0r9T/WfvNVdhgrsGFg
+NQjvpUoZnNJpQIHBbgMOiXqfATFjJl5FjEkSf7GUHohlGVls9MX2JmVvknzsiitd
+75H/EJd+N+k915lix8Vqmj8d1CTlbF/8tEjzANI67Vqw5QTuqebO7rkIUvRg6yiR
+fSo75FK1RinCJyl++kmleBwQZBInQyg95GvJ5JTqMzBs67DeeyzskDhTeTePRYVF
+2NwL8QzYhtvLIBERTNsyU5i7nkxY5ptUwgFUwd93LH4Q19tPqL5C5RZqXxhE51th
+OOwafm+aW/cRkqYqV+tv+j1jJ3WICyF1JNW0BQIDAQABAoIBADAiZ/r+xP+vkd5u
+O61/lCBFzBlZQecdybJw6HJaVK6XBndA9hESUr4LHUdui6W+51ddKd65IV4bXAUk
+zCKjQb+FFvLDT/bA+TTvLATUdTSN7hJJ3OWBAHuNOlQklof6JCB0Hi4+89+P8/pX
+eKUgR/cmuTMDT/iaXdPHeqFbBQyA1ZpQFRjN5LyyJMS/9FkywuNc5wlpsArtc51T
+gIKENUZCuPhosR+kMFc2iuTNvqZWPhvouSrmhi2O6nSqV+oy0+irlqSpCF2GsCI8
+72TtLpq94Grrq0BEH5avouV+Lp4k83vO65OKCQKUFQlxz3Xkxm2U3J7KzxqnRtM3
+/b+cJ/kCgYEA6/yOnaEYhH/7ijhZbPn8RujXZ5VGJXKJqIuaPiHMmHVS5p1j6Bah
+2PcnqJA2IlLs3UloN+ziAxAIH6KCBiwlQ/uPBNMMaJsIjPNBEy8axjndKhKUpidg
+R0OJ7RQqMShOJ8akrSfWdPtXC/GBuwCYE//t77GgZaIMO3FcT9EKA48CgYEA4Xcx
+Fia0Jg9iyAhNmUOXI6hWcGENavMx01+x7XFhbnMjIKTZevFfTnTkrX6HyLXyGtMU
+gHOn+k4PE/purI4ARrKO8m5wYEKqSIt4dBMTkIXXirfQjXgfjR8E4T/aPe5fOFZo
+7OYuxLRtzmG1C2sW4txwKAKX1LaWcVx/RLSttSsCgYBbcj8Brk+F6OJcqYFdzXGJ
+OOlf5mSMVlopyg83THmwCqbZXtw8L6kAHqZrl5airmfDSJLuOQlMDoZXW+3u3mSC
+d5TwVahVUN57YDgzaumBLyMZDqIz0MZqVy23hTzkV64Rk9R0lR9xrYQJyMhw4sYL
+2f0mCTsSpzz+O+t9so+i2QKBgEC38gMlwPhb2kMI/x1LZYr6uzUu5qcYf+jowy4h
+KZKGwkKQj0zXFEB1FV8nvtpCP+irRmtIx6L13SYi8LnfWPzyLE4ynVdES5TfVAgd
+obQOdzx+XwL8xDHCAaiWp5K3ZeXKB/xYZnxYPlzLdyh76Ond1OPnOqX4c16+6llS
+c7pZAoGATd9NckT0XtXLEsF3IraDivq8dP6bccX2DNfS8UeEvRRrRwpFpSRrmuGb
+jbG4yzoIX4RjQfj/z48hwhJB+cKiN9WwcPsFXtHe7v3F6BRwK0JUfrCiXad8/SGZ
+KAf7Dfqi608zBdnPWHacre2Y35gPHB00nFQOLS6u46aBNSq07YA=
+-----END RSA PRIVATE KEY-----
+EOS
+ end
+
+ let(:another_valid_key) do
+ <<EOS
+-----BEGIN RSA PRIVATE KEY-----
+MIIEpAIBAAKCAQEAoISxYJBTPAeAzFnm+lE/ljLlmGal2Xr3vwZKkvJiuKA/m4QJ
+0ZNdtkBSDOVuG2dXVv6W4sChRtsCdvuVe7bjTYvlU8TWM3VEJDL9l9cRXScxxlKQ
+Xwb35y1yV35NJfaK/jzm9KcErtQQs1RxvGlWRaohmLM8uQcuhjZfMsSlQoHQD5LX
+sbPtk82RPyxYc1dj2vsaoi1VvuP2+jv4xLQOmNJY1bT5GTurqiltmxEtWhNNmGg0
+2wtK00ifqLVO5HNc3gXQCDM2M99Sbmn1YtbrgsU9xMYfcPmvQvb+YoKskyoqck+c
+HR//hi7vslbxABrny15LBkEfRc4TickphSGYXwIDAQABAoIBAATEzGw8/WwMIQRx
+K06GeWgh7PZBHm4+m/ud2TtSXiJ0CE+7dXs3cJJIiOd/LW08/bhE6gCkjmYHfaRB
+Ryicv1X/cPmzIFX5BuQ4a5ZGOmrVDkKBE27vSxAgJoR46RvWnjx9XLMp/xaekDxz
+psldK8X4DvV1ZbltgDFWji947hvyqUtHdKnkQnc5j7aCIFJf9GMfzaeeDPMaL8WF
+mVL4iy9EAOjNOHBshZj/OHyU5FbJ8ROwZQlCOiLCdFegftSIXt8EYDnjB3BdsALH
+N6hquqrD7xDKyRbTD0K7lqxUubuMwTQpi61jZD8TBTXEPyFVAnoMpXkc0Y+np40A
+YiIsR+kCgYEAyrc4Bh6fb9gt49IXGXOSRZ5i5+TmJho4kzIONrJ7Ndclwx9wzHfh
+eGBodWaw5CxxQGMf4vEiaZrpAiSFeDffBLR+Wa2TFE5aWkdYkR34maDjO00m4PE1
+S+YsZoGw7rGmmj+KS4qv2T26FEHtUI+F31RC1FPohLsQ22Jbn1ORipsCgYEAyrYB
+J2Ncf2DlX1C0GfxyUHQOTNl0V5gpGvpbZ0WmWksumYz2kSGOAJkxuDKd9mKVlAcz
+czmN+OOetuHTNqds2JJKKJy6hJbgCdd9aho3dId5Xs4oh4YwuFQiG8R/bJZfTlXo
+99Qr02L7MmDWYLmrR3BA/93UPeorHPtjqSaYU40CgYEAtmGfWwokIglaSDVVqQVs
+3YwBqmcrla5TpkMLvLRZ2/fktqfL4Xod9iKu+Klajv9ZKTfFkXWno2HHL7FSD/Yc
+hWwqnV5oDIXuDnlQOse/SeERb+IbD5iUfePpoJQgbrCQlwiB0TNGwOojR2SFMczf
+Ai4aLlQLx5dSND9K9Y7HS+8CgYEAixlHQ2r4LuQjoTs0ytwi6TgqE+vn3K+qDTwc
+eoods7oBWRaUn1RCKAD3UClToZ1WfMRQNtIYrOAsqdveXpOWqioAP0wE5TTOuZIo
+GiWxRgIsc7TNtOmNBv+chCdbNP0emxdyjJUIGb7DFnfCw47EjHnn8Guc13uXaATN
+B2ZXgoUCgYAGa13P0ggUf5BMJpBd8S08jKRyvZb1CDXcUCuGtk2yEx45ern9U5WY
+zJ13E5z9MKKO8nkGBqrRfjJa8Xhxk4HKNFuzHEet5lvNE7IKCF4YQRb0ZBhnb/78
++4ZKjFki1RrWRNSw9TdvrK6qaDKgTtCTtfRVXAYQXUgq7lSFOTtL3A==
+-----END RSA PRIVATE KEY-----
+EOS
+ end
+
+ let(:valid_cert_but_indented) do
+ valid_cert.gsub(/^/, ' ')
+ end
+
+ let(:valid_key_but_indented) do
+ valid_key.gsub(/^/, ' ')
+ end
+
+ let(:malformed_cert) do
+ truncate_middle(valid_cert)
+ end
+
+ let(:malformed_key) do
+ truncate_middle(valid_key)
+ end
+
+ let(:bad_cert) do
+ 'foo'
+ end
+
+ let(:bad_key) do
+ 'bar'
+ end
+
+ context 'function signature validation' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params(0, 1, 2, 3).and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ end
+
+ context 'valid input' do
+ describe 'valid certificate and key' do
+ it { is_expected.to run.with_params(valid_cert, valid_key) }
+ end
+ end
+
+ context 'bad input' do
+ describe 'valid certificate, valid but indented key' do
+ it { is_expected.to run.with_params(valid_cert, valid_key_but_indented).and_raise_error(Puppet::ParseError, /Not a valid RSA key/) }
+ end
+
+ describe 'valid certificate, malformed key' do
+ it { is_expected.to run.with_params(valid_cert, malformed_key).and_raise_error(Puppet::ParseError, /Not a valid RSA key/) }
+ end
+
+ describe 'valid certificate, bad key' do
+ it { is_expected.to run.with_params(valid_cert, bad_key).and_raise_error(Puppet::ParseError, /Not a valid RSA key/) }
+ end
+
+ describe 'valid but indented certificate, valid key' do
+ it { is_expected.to run.with_params(valid_cert_but_indented, valid_key).and_raise_error(Puppet::ParseError, /Not a valid x509 certificate/) }
+ end
+
+ describe 'malformed certificate, valid key' do
+ it { is_expected.to run.with_params(malformed_cert, valid_key).and_raise_error(Puppet::ParseError, /Not a valid x509 certificate/) }
+ end
+
+ describe 'bad certificate, valid key' do
+ it { is_expected.to run.with_params(bad_cert, valid_key).and_raise_error(Puppet::ParseError, /Not a valid x509 certificate/) }
+ end
+
+ describe 'validate certificate and key; certficate not signed by key' do
+ it { is_expected.to run.with_params(valid_cert, another_valid_key).and_raise_error(Puppet::ParseError, /Certificate signature does not match supplied key/) }
+ end
+
+ describe 'valid cert and key but arguments in wrong order' do
+ it { is_expected.to run.with_params(valid_key, valid_cert).and_raise_error(Puppet::ParseError, /Not a valid x509 certificate/) }
+ end
+
+ describe 'non-string arguments' do
+ it { is_expected.to run.with_params({}, {}).and_raise_error(Puppet::ParseError, /is not a string/) }
+ it { is_expected.to run.with_params(1, 1).and_raise_error(Puppet::ParseError, /is not a string/) }
+ it { is_expected.to run.with_params(true, true).and_raise_error(Puppet::ParseError, /is not a string/) }
+ it { is_expected.to run.with_params("foo", {}).and_raise_error(Puppet::ParseError, /is not a string/) }
+ it { is_expected.to run.with_params(1, "bar").and_raise_error(Puppet::ParseError, /is not a string/) }
+ it { is_expected.to run.with_params("baz", true).and_raise_error(Puppet::ParseError, /is not a string/) }
+ end
+ end
+
+ def truncate_middle(string)
+ chars_to_truncate = 48
+ middle = (string.length / 2).floor
+ start_pos = middle - (chars_to_truncate / 2)
+ end_pos = middle + (chars_to_truncate / 2)
+
+ string[start_pos...end_pos] = ''
+ return string
+ end
+end
diff --git a/spec/functions/values_at_spec.rb b/spec/functions/values_at_spec.rb
index 86e3c31..a8348f3 100755
--- a/spec/functions/values_at_spec.rb
+++ b/spec/functions/values_at_spec.rb
@@ -1,38 +1,49 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the values_at function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- it "should exist" do
- expect(Puppet::Parser::Functions.function("values_at")).to eq("function_values_at")
- end
-
- it "should raise a ParseError if there is less than 1 arguments" do
- expect { scope.function_values_at([]) }.to( raise_error(Puppet::ParseError))
- end
-
- it "should raise a ParseError if you try to use a range where stop is greater then start" do
- expect { scope.function_values_at([['a','b'],["3-1"]]) }.to( raise_error(Puppet::ParseError))
- end
-
- it "should return a value at from an array" do
- result = scope.function_values_at([['a','b','c'],"1"])
- expect(result).to(eq(['b']))
- end
-
- it "should return a value at from an array when passed a range" do
- result = scope.function_values_at([['a','b','c'],"0-1"])
- expect(result).to(eq(['a','b']))
+describe 'values_at' do
+ describe 'signature validation' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it {
+ pending("Current implementation ignores parameters after the first two.")
+ is_expected.to run.with_params([], 0, 1).and_raise_error(Puppet::ParseError, /wrong number of arguments/i)
+ }
+ it { is_expected.to run.with_params('', 1).and_raise_error(Puppet::ParseError, /Requires array/i) }
+ it { is_expected.to run.with_params({}, 1).and_raise_error(Puppet::ParseError, /Requires array/i) }
+ it { is_expected.to run.with_params(true, 1).and_raise_error(Puppet::ParseError, /Requires array/i) }
+ it { is_expected.to run.with_params(1, 1).and_raise_error(Puppet::ParseError, /Requires array/i) }
+ it { is_expected.to run.with_params([0,1,2], 'two').and_raise_error(Puppet::ParseError, /Unknown format of given index/) }
+ it { is_expected.to run.with_params([0,1,2], []).and_raise_error(Puppet::ParseError, /provide at least one positive index/) }
+ it { is_expected.to run.with_params([0,1,2], '-1-1').and_raise_error(Puppet::ParseError, /Unknown format of given index/) }
+ it { is_expected.to run.with_params([0,1,2], '2-1').and_raise_error(Puppet::ParseError, /Stop index in given indices range is smaller than the start index/) }
end
- it "should return chosen values from an array when passed number of indexes" do
- result = scope.function_values_at([['a','b','c'],["0","2"]])
- expect(result).to(eq(['a','c']))
+ context 'when requesting a single item' do
+ it { is_expected.to run.with_params([0, 1, 2], -1).and_raise_error(Puppet::ParseError, /Unknown format of given index/) }
+ it { is_expected.to run.with_params([0, 1, 2], 0).and_return([0]) }
+ it { is_expected.to run.with_params([0, 1, 2], 1).and_return([1]) }
+ it { is_expected.to run.with_params([0, 1, 2], [1]).and_return([1]) }
+ it { is_expected.to run.with_params([0, 1, 2], '1').and_return([1]) }
+ it { is_expected.to run.with_params([0, 1, 2], '1-1').and_return([1]) }
+ it { is_expected.to run.with_params([0, 1, 2], 2).and_return([2]) }
+ it { is_expected.to run.with_params([0, 1, 2], 3).and_raise_error(Puppet::ParseError, /index exceeds array size/) }
end
- it "should return chosen values from an array when passed ranges and multiple indexes" do
- result = scope.function_values_at([['a','b','c','d','e','f','g'],["0","2","4-5"]])
- expect(result).to(eq(['a','c','e','f']))
+ context 'when requesting multiple items' do
+ it { is_expected.to run.with_params([0, 1, 2], [1, -1]).and_raise_error(Puppet::ParseError, /Unknown format of given index/) }
+ it { is_expected.to run.with_params([0, 1, 2], [0, 2]).and_return([0, 2]) }
+ it { is_expected.to run.with_params([0, 1, 2], ['0-2', 1, 2]).and_return([0, 1, 2, 1, 2]) }
+ it { is_expected.to run.with_params([0, 1, 2], [3, 2]).and_raise_error(Puppet::ParseError, /index exceeds array size/) }
+
+ describe 'different range syntaxes' do
+ it { is_expected.to run.with_params([0, 1, 2], '0-2').and_return([0, 1, 2]) }
+ it { is_expected.to run.with_params([0, 1, 2], '0..2').and_return([0, 1, 2]) }
+ it { is_expected.to run.with_params([0, 1, 2], '0...2').and_return([0, 1]) }
+ it {
+ pending('fix this bounds check')
+ is_expected.to run.with_params([0, 1, 2], '0...3').and_return([0, 1, 2])
+ }
+ end
end
end
diff --git a/spec/functions/values_spec.rb b/spec/functions/values_spec.rb
index 08d21b0..4abf0bd 100755
--- a/spec/functions/values_spec.rb
+++ b/spec/functions/values_spec.rb
@@ -1,31 +1,19 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the values function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- it "should exist" do
- expect(Puppet::Parser::Functions.function("values")).to eq("function_values")
- end
-
- it "should raise a ParseError if there is less than 1 arguments" do
- expect { scope.function_values([]) }.to( raise_error(Puppet::ParseError))
- end
-
- it "should return values from a hash" do
- result = scope.function_values([{'a'=>'1','b'=>'2','c'=>'3'}])
- # =~ is the RSpec::Matchers::MatchArray matcher.
- # A.K.A. "array with same elements" (multiset) matching
- expect(result).to match_array(%w{ 1 2 3 })
- end
-
- it "should return a multiset" do
- result = scope.function_values([{'a'=>'1','b'=>'3','c'=>'3'}])
- expect(result).to match_array(%w{ 1 3 3 })
- expect(result).not_to match_array(%w{ 1 3 })
- end
-
- it "should raise a ParseError unless a Hash is provided" do
- expect { scope.function_values([['a','b','c']]) }.to( raise_error(Puppet::ParseError))
+describe 'values' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it {
+ pending("Current implementation ignores parameters after the first.")
+ is_expected.to run.with_params({}, 'extra').and_raise_error(Puppet::ParseError, /wrong number of arguments/i)
+ }
+ it { is_expected.to run.with_params('').and_raise_error(Puppet::ParseError, /Requires hash to work with/) }
+ it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, /Requires hash to work with/) }
+ it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError, /Requires hash to work with/) }
+ it { is_expected.to run.with_params({}).and_return([]) }
+ it { is_expected.to run.with_params({ 'key' => 'value' }).and_return(['value']) }
+ it 'should return the array of values' do
+ result = subject.call([{ 'key1' => 'value1', 'key2' => 'value2', 'duplicate_value_key' => 'value2' }])
+ expect(result).to match_array(['value1', 'value2', 'value2'])
end
end
diff --git a/spec/functions/zip_spec.rb b/spec/functions/zip_spec.rb
index f265fce..abca7ee 100755
--- a/spec/functions/zip_spec.rb
+++ b/spec/functions/zip_spec.rb
@@ -1,31 +1,15 @@
-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe "the zip function" do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- it "should raise a ParseError if there is less than 1 arguments" do
- expect { scope.function_zip([]) }.to( raise_error(Puppet::ParseError))
- end
-
- it "should be able to zip an array" do
- result = scope.function_zip([['1','2','3'],['4','5','6']])
- expect(result).to(eq([["1", "4"], ["2", "5"], ["3", "6"]]))
- result = scope.function_zip([['1','2','3'],['4','5','6'], false])
- result.should(eq([["1", "4"], ["2", "5"], ["3", "6"]]))
- end
-
- it "should be able to zip an array and flatten" do
- result = scope.function_zip([['1','2','3'],['4','5','6'], true])
- result.should(eq(["1", "4", "2", "5", "3", "6"]))
- end
-
- it "should accept objects which extend String for the second argument" do
- class AlsoString < String
- end
-
- value = AlsoString.new('false')
- result = scope.function_zip([['1','2','3'],['4','5','6'],value])
- result.should(eq([["1", "4"], ["2", "5"], ["3", "6"]]))
- end
+describe 'zip' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it {
+ pending("Current implementation ignores parameters after the third.")
+ is_expected.to run.with_params([], [], true, []).and_raise_error(Puppet::ParseError, /wrong number of arguments/i)
+ }
+ it { is_expected.to run.with_params([], []).and_return([]) }
+ it { is_expected.to run.with_params([1,2,3], [4,5,6]).and_return([[1,4], [2,5], [3,6]]) }
+ it { is_expected.to run.with_params([1,2,3], [4,5,6], false).and_return([[1,4], [2,5], [3,6]]) }
+ it { is_expected.to run.with_params([1,2,3], [4,5,6], true).and_return([1, 4, 2, 5, 3, 6]) }
end