-
Notifications
You must be signed in to change notification settings - Fork 18
Fix Ensure check methods can't modify tokens array
#233
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
require 'spec_helper' | ||
|
||
class DummyCheckPlugin < PuppetLint::CheckPlugin | ||
def check | ||
# Since we're calling `tokens` from a `check` method, we should get our own Array object. | ||
# If we add an extra token to it, PuppetLint::Data.tokens should remain unchanged. | ||
tokens << :extra_token | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When returning a frozen array it will raise a |
||
end | ||
|
||
def fix | ||
tokens << :fix_token | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this is supposed to be a valid API because there's also the |
||
end | ||
end | ||
|
||
describe PuppetLint::CheckPlugin do | ||
before(:each) do | ||
PuppetLint::Data.tokens = [:token1, :token2, :token3] | ||
end | ||
|
||
it 'returns a duplicate of the token array when called from check' do | ||
plugin = DummyCheckPlugin.new | ||
|
||
plugin.check | ||
|
||
# Verify that the global token array remains unchanged. | ||
expect(PuppetLint::Data.tokens).to eq([:token1, :token2, :token3]) | ||
end | ||
|
||
it 'other methods can modify the tokens array' do | ||
plugin = DummyCheckPlugin.new | ||
|
||
plugin.fix | ||
|
||
expect(PuppetLint::Data.tokens).to eq([:token1, :token2, :token3, :fix_token]) | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had a crazy thought: rather than duplicating, would it make sense to freeze it? https://rubyapi.org/o/array#method-i-freeze says it will not do anything if already frozen, so should be cheap and you can't modify it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There wasn't much information I could find about to why this was originally introduced, but other methods (such as
fix
) definitely do need to be able to modify the array.