From b4602ac59dcc4de8f39552e3d6951dd15c9b361e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Tarti=C3=A8re?= Date: Thu, 4 May 2023 09:29:13 -1000 Subject: [PATCH] Rewrite validate_email_address() as a Puppet 4.x function The 3.x function rely on is_email_address() which is deprecated. Rewrite it using the more modern puppet 4.x function to rely on data types for better parameters validation. --- .../functions/validate_email_address.rb | 31 +++++++++++++ .../functions/validate_email_address.rb | 45 ------------------- spec/functions/validate_email_address_spec.rb | 16 +++---- 3 files changed, 39 insertions(+), 53 deletions(-) create mode 100644 lib/puppet/functions/validate_email_address.rb delete mode 100644 lib/puppet/parser/functions/validate_email_address.rb diff --git a/lib/puppet/functions/validate_email_address.rb b/lib/puppet/functions/validate_email_address.rb new file mode 100644 index 000000000..fb0376b15 --- /dev/null +++ b/lib/puppet/functions/validate_email_address.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +# @summary +# Validate that all values passed are valid email addresses. +# Fail compilation if any value fails this check. +Puppet::Functions.create_function(:validate_email_address) do + # @param values An e-mail address or an array of e-mail addresses to check + # + # @return [Undef] + # Fail compilation if any value fails this check. + # + # @example Passing examples + # $my_email = "waldo@gmail.com" + # validate_email_address($my_email) + # validate_email_address("bob@gmail.com", "alice@gmail.com", $my_email) + # + # @example Failing examples (causing compilation to abort) + # $some_array = [ 'bad_email@/d/efdf.com' ] + # validate_email_address($some_array) + dispatch :validate_email_address do + repeated_param 'Stdlib::Email', :values + end + + def validate_email_address(*args) + assert_arg_count(args) + end + + def assert_arg_count(args) + raise(ArgumentError, 'validate_email_address(): Wrong number of arguments need at least one') if args.empty? + end +end diff --git a/lib/puppet/parser/functions/validate_email_address.rb b/lib/puppet/parser/functions/validate_email_address.rb deleted file mode 100644 index ae847b591..000000000 --- a/lib/puppet/parser/functions/validate_email_address.rb +++ /dev/null @@ -1,45 +0,0 @@ -# frozen_string_literal: true - -# -# validate_email_address.rb -# -module Puppet::Parser::Functions - newfunction(:validate_email_address, doc: <<-DOC - @summary - Validate that all values passed are valid email addresses. - Fail compilation if any value fails this check. - - @return - Fail compilation if any value fails this check. - - @example **Usage** - - The following values will pass: - - $my_email = "waldo@gmail.com" - validate_email_address($my_email) - validate_email_address("bob@gmail.com", "alice@gmail.com", $my_email) - - The following values will fail, causing compilation to abort: - - $some_array = [ 'bad_email@/d/efdf.com' ] - validate_email_address($some_array) - DOC - ) do |args| - rescuable_exceptions = [ArgumentError] - - if args.empty? - raise Puppet::ParseError, "validate_email_address(): wrong number of arguments (#{args.length}; must be > 0)" - end - - args.each do |arg| - raise Puppet::ParseError, "#{arg.inspect} is not a string." unless arg.is_a?(String) - - begin - raise Puppet::ParseError, "#{arg.inspect} is not a valid email address" unless function_is_email_address([arg]) - rescue *rescuable_exceptions - raise Puppet::ParseError, "#{arg.inspect} is not a valid email address" - end - end - end -end diff --git a/spec/functions/validate_email_address_spec.rb b/spec/functions/validate_email_address_spec.rb index 9d87061bb..28d1e9100 100644 --- a/spec/functions/validate_email_address_spec.rb +++ b/spec/functions/validate_email_address_spec.rb @@ -5,7 +5,7 @@ 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, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{wrong number of arguments}i) } end describe 'valid inputs' do @@ -14,12 +14,12 @@ end describe 'invalid inputs' do - it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, %r{is not a string}) } - it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, %r{is not a string}) } - it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, %r{is not a string}) } - it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, %r{is not a valid email}) } - it { is_expected.to run.with_params('bob@gmail.com', {}).and_raise_error(Puppet::ParseError, %r{is not a string}) } - it { is_expected.to run.with_params('bob@gmail.com', true).and_raise_error(Puppet::ParseError, %r{is not a string}) } - it { is_expected.to run.with_params('bob@gmail.com', 'one').and_raise_error(Puppet::ParseError, %r{is not a valid email}) } + it { is_expected.to run.with_params({}).and_raise_error(ArgumentError, %r{got Hash}) } + it { is_expected.to run.with_params(1).and_raise_error(ArgumentError, %r{got Integer}) } + it { is_expected.to run.with_params(true).and_raise_error(ArgumentError, %r{got Boolean}) } + it { is_expected.to run.with_params('one').and_raise_error(ArgumentError, %r{got 'one'}) } + it { is_expected.to run.with_params('bob@gmail.com', {}).and_raise_error(ArgumentError, %r{got Hash}) } + it { is_expected.to run.with_params('bob@gmail.com', true).and_raise_error(ArgumentError, %r{got Boolean}) } + it { is_expected.to run.with_params('bob@gmail.com', 'one').and_raise_error(ArgumentError, %r{got 'one'}) } end end