Skip to content

Commit

Permalink
fix: adjust global_function to allow defining functions
Browse files Browse the repository at this point in the history
  • Loading branch information
shieldwed committed Oct 5, 2023
1 parent 19e2226 commit 1ce05e5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
18 changes: 13 additions & 5 deletions lib/puppet-lint/plugins/check_global_definition.rb
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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
Expand All @@ -56,6 +58,12 @@ def check_for_global_resources
include PuppetLintGlobalDefinionCheck

def check
check_for_global_token(:FUNCTION_NAME)
check_for_global_token(:FUNCTION_NAME) do |token|
if !token.prev_code_token.nil? && token.prev_code_token.type == :FUNCTION
next
else
next "#{token.value} #{token.next_code_token.value}"
end
end
end
end
22 changes: 22 additions & 0 deletions spec/puppet-lint/plugins/check_global_function_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 1ce05e5

Please sign in to comment.