Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(PUP-11515) Negative Lookbehind Regex Causes Duplicate Node #9420

Merged
merged 1 commit into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion lib/puppet/resource/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ class Puppet::Resource::Type
DOUBLE_COLON = '::'
EMPTY_ARRAY = [].freeze

LOOKAROUND_OPERATORS = {
"(" => 'LP',
"?" => "QU",
"<" => "LT",
">" => "GT",
"!" => "EX",
"=" => "EQ",
")" => 'RP'
}.freeze

attr_accessor :file, :line, :doc, :code, :parent, :resource_type_collection, :override
attr_reader :namespace, :arguments, :behaves_like, :module_name

Expand Down Expand Up @@ -196,7 +206,11 @@ def instantiate_resource(scope, resource)

def name
if type == :node && name_is_regex?
"__node_regexp__#{@name.source.downcase.gsub(/[^-\w:.]/, '').sub(/^\.+/, '')}"
# Normalize lookarround regex patthern
internal_name = @name.source.downcase.gsub(/\(\?[^)]*\)/) do |str|
str.gsub(/./) { |ch| LOOKAROUND_OPERATORS[ch] || ch }
end
"__node_regexp__#{internal_name.gsub(/[^-\w:.]/, '').sub(/^\.+/, '')}"
else
@name
end
Expand Down
9 changes: 9 additions & 0 deletions spec/integration/parser/node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ class foo {
end.not_to raise_error
end

it 'does not raise an error with 2 regex node names are the same due to lookarround pattern' do
expect do
compile_to_catalog(<<-MANIFEST, Puppet::Node.new("async"))
node /(?<!a)sync/ { }
node /async/ { }
MANIFEST
end.not_to raise_error
imaqsood marked this conversation as resolved.
Show resolved Hide resolved
end

it 'errors when two nodes with regexes collide after some regex syntax is removed' do
expect do
compile_to_catalog(<<-MANIFEST)
Expand Down