From f28550e78996f908b076cdc9aebcbe584c777cd0 Mon Sep 17 00:00:00 2001 From: Erik Dalén Date: Wed, 10 Apr 2013 14:38:14 +0200 Subject: Add a count function Similar to the ruby count method on arrays. --- lib/puppet/parser/functions/count.rb | 22 ++++++++++++++++++++ spec/unit/puppet/parser/functions/count_spec.rb | 27 +++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 lib/puppet/parser/functions/count.rb create mode 100644 spec/unit/puppet/parser/functions/count_spec.rb diff --git a/lib/puppet/parser/functions/count.rb b/lib/puppet/parser/functions/count.rb new file mode 100644 index 0000000..52de1b8 --- /dev/null +++ b/lib/puppet/parser/functions/count.rb @@ -0,0 +1,22 @@ +module Puppet::Parser::Functions + newfunction(:count, :type => :rvalue, :arity => -2, :doc => <<-EOS +Takes an array as first argument and an optional second argument. +Count the number of elements in array that matches second argument. +If called with only an array it counts the number of elements that are not nil/undef. + EOS + ) do |args| + + if (args.size > 2) then + raise(ArgumentError, "count(): Wrong number of arguments "+ + "given #{args.size} for 1 or 2.") + end + + collection, item = args + + if item then + collection.count item + else + collection.count { |obj| obj != nil && obj != :undef && obj != '' } + end + end +end diff --git a/spec/unit/puppet/parser/functions/count_spec.rb b/spec/unit/puppet/parser/functions/count_spec.rb new file mode 100644 index 0000000..5e4a8b9 --- /dev/null +++ b/spec/unit/puppet/parser/functions/count_spec.rb @@ -0,0 +1,27 @@ +#! /usr/bin/env ruby -S rspec + +require 'spec_helper' + +describe "the count function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + Puppet::Parser::Functions.function("count").should == "function_count" + end + + it "should raise a ArgumentError if there is more than 2 arguments" do + lambda { scope.function_count(['foo', 'bar', 'baz']) }.should( raise_error(ArgumentError)) + end + + it "should be able to count arrays" do + scope.function_count([["1","2","3"]]).should(eq(3)) + end + + it "should be able to count matching elements in arrays" do + scope.function_count([["1", "2", "2"], "2"]).should(eq(2)) + end + + it "should not count :undef, nil or empty strings" do + scope.function_count([["foo","bar",:undef,nil,""]]).should(eq(2)) + end +end -- cgit v1.2.3 From 435226abfdf001bf2475be56d77027c638e93b70 Mon Sep 17 00:00:00 2001 From: Jeff McCune Date: Wed, 10 Apr 2013 14:33:08 -0700 Subject: (maint) Add the behavior for count() with arrays and hashes Without this patch the expected behavior of the count() function when dealing with an out of bound array index and with a hash key that does not exist is implicitly encoded in the spec examples. This is a problem because the expected behavior is not clear for something similar to the following example: node default { $ary = [ 1, 2, 3 ] $ary_undef = $ary[100] $hsh = { 'one' => 1 } $hsh_undef = $hsh['dne'] $count = count(['hi', $ary_undef, $hsh_undef]) notice "Count is ${count}" } This patch addresses the problem by making the expected behavior explicit in the examples. --- spec/unit/puppet/parser/functions/count_spec.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/spec/unit/puppet/parser/functions/count_spec.rb b/spec/unit/puppet/parser/functions/count_spec.rb index 5e4a8b9..2453815 100644 --- a/spec/unit/puppet/parser/functions/count_spec.rb +++ b/spec/unit/puppet/parser/functions/count_spec.rb @@ -21,7 +21,11 @@ describe "the count function" do scope.function_count([["1", "2", "2"], "2"]).should(eq(2)) end - it "should not count :undef, nil or empty strings" do - scope.function_count([["foo","bar",:undef,nil,""]]).should(eq(2)) + it "should not count nil or empty strings" do + scope.function_count([["foo","bar",nil,""]]).should(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 end -- cgit v1.2.3 From e81a45ee0085597dad5067cada94b05cf11c6c0c Mon Sep 17 00:00:00 2001 From: Jeff McCune Date: Wed, 10 Apr 2013 14:35:42 -0700 Subject: (maint) Make stdlib usable as a Ruby GEM Without this patch it is inconvenient to use the functions included in stdlib in a development setting. The Puppet modulepath must be explicitly set for the functions to be automatically loaded. This patch addresses the problem by adding a gem specification and dependency Gemfile. This makes it possible to directly use stdlib and all of the components it depends upon, like so: $ bundle install --path .bundle/gems/ $ bundle exec puppet apply -e 'notice count([1, 2, 3])' The first command will install all of the dependencies, including Puppet and Facter, into the local project directory. The second command will make stdlib avaialable as a Gem, which will be picked up by Puppet since (#7788) was merged into Puppet in the 3.0 release. --- .gemspec | 31 +++++++++++++++++++++++++++++++ Gemfile | 1 + 2 files changed, 32 insertions(+) create mode 100644 .gemspec diff --git a/.gemspec b/.gemspec new file mode 100644 index 0000000..805e170 --- /dev/null +++ b/.gemspec @@ -0,0 +1,31 @@ +# +# -*- encoding: utf-8 -*- + +Gem::Specification.new do |s| + s.name = "puppetmodule-stdlib" + + s.version = "3.2.0" + + s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= + s.authors = ["Puppet Labs"] + s.date = "2013-04-10" + s.description = "Puppet Labs Standard Library module" + s.email = "support@puppetlabs.com" + s.executables = [] + s.files = [] + s.homepage = "https://github.com/puppetlabs/puppetlabs-stdlib" + s.rdoc_options = ["--title", "Puppet Standard Library Module", "--main", "README.markdown", "--line-numbers"] + s.require_paths = ["lib"] + s.rubyforge_project = "puppetmodule_stdlib" + s.rubygems_version = "1.8.24" + s.summary = "This module provides a standard library of resources for developing Puppet Modules." + + if s.respond_to? :specification_version then + s.specification_version = 3 + + if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then + else + end + else + end +end diff --git a/Gemfile b/Gemfile index 1026074..442dd9d 100644 --- a/Gemfile +++ b/Gemfile @@ -6,6 +6,7 @@ end group :development, :test do gem 'rake' + gem 'puppetmodule-stdlib', ">= 1.0.0", :path => File.expand_path("..", __FILE__) gem 'rspec', "~> 2.11.0", :require => false gem 'mocha', "~> 0.10.5", :require => false gem 'puppetlabs_spec_helper', :require => false -- cgit v1.2.3