From ca9cdb7c5c1e65e2934cf621677094b00a2d31ff Mon Sep 17 00:00:00 2001 From: Daniel Wilhelm Date: Thu, 5 Oct 2023 14:26:28 +0000 Subject: [PATCH 1/2] refactor: simplify token loop --- lib/puppet-lint/plugins/check_global_definition.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/puppet-lint/plugins/check_global_definition.rb b/lib/puppet-lint/plugins/check_global_definition.rb index 934af4b..cd90abc 100644 --- a/lib/puppet-lint/plugins/check_global_definition.rb +++ b/lib/puppet-lint/plugins/check_global_definition.rb @@ -2,7 +2,7 @@ module PuppetLintGlobalDefinionCheck private def check_for_global_token(type, value = nil) - global_tokens.each_with_index do |token, i| + global_tokens.each do |token| next unless token.type == type next unless value.nil? || token.value == value From 516d93c72a8ad2520d6134b6dbd7d8089c655b82 Mon Sep 17 00:00:00 2001 From: Daniel Wilhelm Date: Thu, 5 Oct 2023 14:55:33 +0000 Subject: [PATCH 2/2] fix: adjust global_function to allow defining functions --- .../plugins/check_global_definition.rb | 14 +++++++----- .../plugins/check_global_function_spec.rb | 22 +++++++++++++++++++ 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/lib/puppet-lint/plugins/check_global_definition.rb b/lib/puppet-lint/plugins/check_global_definition.rb index cd90abc..070dcaf 100644 --- a/lib/puppet-lint/plugins/check_global_definition.rb +++ b/lib/puppet-lint/plugins/check_global_definition.rb @@ -1,12 +1,12 @@ module PuppetLintGlobalDefinionCheck private - def check_for_global_token(type, value = nil) + def check_for_global_token(type) global_tokens.each do |token| next unless token.type == type - next unless value.nil? || token.value == value - message = value.nil? ? token.value : "#{token.value} #{token.next_code_token.value}" + message = yield(token) + next unless message notify :error, message: "definition #{message} in global space", @@ -37,7 +37,9 @@ def secure_ranges def check check_for_global_resources - check_for_global_token(:NAME, "include") + check_for_global_token(:NAME) do |token| + "#{token.value} #{token.next_code_token.value}" if token.value == "include" + end end def check_for_global_resources @@ -56,6 +58,8 @@ def check_for_global_resources include PuppetLintGlobalDefinionCheck def check - check_for_global_token(:FUNCTION_NAME) + check_for_global_token(:FUNCTION_NAME) do |token| + "#{token.value} #{token.next_code_token.value}" unless !token.prev_code_token.nil? && token.prev_code_token.type == :FUNCTION + end end end diff --git a/spec/puppet-lint/plugins/check_global_function_spec.rb b/spec/puppet-lint/plugins/check_global_function_spec.rb index 2954c31..0b69eca 100644 --- a/spec/puppet-lint/plugins/check_global_function_spec.rb +++ b/spec/puppet-lint/plugins/check_global_function_spec.rb @@ -30,4 +30,26 @@ class test { expect(problems).to have(1).problems end end + + context "module function definition" do + let(:code) do + <<-EOS + # @summary function to clean hash of undef and empty values + function nine_networkinterfaces::delete_empty_values( + Hash $hash, + ) >> Hash { + $hash.filter |$key, $value| { + case $value { + Collection: { $value =~ NotUndef and !$value.empty } + default: { $value =~ NotUndef } + } + } + } + EOS + end + + it "should not detect any problems" do + expect(problems).to have(0).problems + end + end end