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

Fix deprecation warning about performing a regex comparison on a hash #2293

Merged
merged 1 commit into from
Sep 8, 2022

Conversation

smokris
Copy link
Contributor

@smokris smokris commented Aug 11, 2022

I'm getting warnings like this for each puppet agent run:

templates/vhost/_require.erb:3: warning: deprecated Object#=~ is called on Hash; it always returns nil

This PR changes the logic to only performing the regex comparison when the requires value is a string.

@smokris smokris requested a review from a team as a code owner August 11, 2022 20:51
Copy link
Collaborator

@ekohl ekohl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you have the same problem on line 24?

@smokris
Copy link
Contributor Author

smokris commented Aug 14, 2022

Don't you have the same problem on line 24?

Hmm, good question. Here's line 24:

<%- if !(_item['require'] && _item['require'] != '') && _item['require'] !~ /unmanaged/i && !(_item['auth_require']) -%>

When _item['require'] is a hash, !(_item['require'] && _item['require'] != '') evaluates to false, so the other clauses are not evaluated, and thus it doesn't emit the "deprecated Object#=~ is called on Hash" warning.

@david22swan
Copy link
Member

@ekohl Are you good with the above explanation?? ^^^

Copy link
Collaborator

@ekohl ekohl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you're right and the whole _item['require'] !~ /unmanaged/i is redundant. It's complex.

If I'm reading it right, this is equal:

<%- if (!_item['require'] || _item['require'] == '') && _item['require'] !~ /unmanaged/i && !(_item['auth_require']) -%>

Now if you read that form, it must be either undef or ''. Both of those are never /unmanaged/i so it's redundant, thus equal to

<%- if (!_item['require'] || _item['require'] == '') && !(_item['auth_require']) -%>

Once you read that, it can actually become an elsif for the block above so it becomes:

  <%- if _item['auth_require'] -%>
    Require <%= _item['auth_require'] %>
  <%- elsif !_item['require'] || _item['require'] == '' -%>
    Require all granted
  <%- end -%>

If my logic there is correct, I think we don't need to modify it now.

@ekohl
Copy link
Collaborator

ekohl commented Sep 7, 2022

If my logic there is correct, I think we don't need to modify it now.

Just to clarify: if _require is nil then it does nil !~ /unmanaged/i, but that doesn't raise any deprecation warning.

Submitted my suggestion as a separate PR: #2303

@david22swan david22swan merged commit 61d1943 into puppetlabs:main Sep 8, 2022
@david22swan
Copy link
Member

Ok, everything looks good to merge then
Thanks for putting in the work

@smokris smokris deleted the fix-deprecated-regex-on-hash branch September 8, 2022 13:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants