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

Enable Rubocop Rule to Freeze Constants #10340

Merged
merged 17 commits into from
Apr 4, 2024

Conversation

mitchellhenke
Copy link
Contributor

🛠 Summary of changes

Testing the waters on this a bit. Ruby constants warn when re-assigned, but are still mutable. Short illustration of the potential issue:

irb(local):001:0> C = {}
{}
irb(local):002:0> C[:a] = 1
1
irb(local):003:0> C
{:a=>1}

This PR enables the Style/MutableConstant Rubocop rule in it's default configuration. We could experimentally enable it in strict mode to try to freeze non-literal constants.

@mitchellhenke mitchellhenke force-pushed the mitchellhenke/frozen-constants branch 2 times, most recently from 2ba0e63 to 37762a0 Compare March 29, 2024 17:19
@unused_keys = config_map.keys - config.written_env.keys
@key_types = config.key_types.freeze
@unused_keys = (config_map.keys - config.written_env.keys).freeze
config.written_env.freeze
Copy link
Contributor

Choose a reason for hiding this comment

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

have taken advantage of testing things in the Rails conosle by modifing IdentityConfig.store.foo = :bar, I'm sure I could work around it but I would skip this if given the option

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, in development? We could only enable in test/prod as an alternative too.

Copy link
Contributor

Choose a reason for hiding this comment

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

😬 no in staging, to test API keys, etc

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In testing this, that functionality will still work since it depends on the struct itself being frozen, which this doesn't do.

spec/support/saml_auth_helper.rb Outdated Show resolved Hide resolved
@mitchellhenke mitchellhenke force-pushed the mitchellhenke/frozen-constants branch 2 times, most recently from dc093b2 to 983ca3c Compare March 30, 2024 13:36
@mitchellhenke mitchellhenke force-pushed the mitchellhenke/frozen-constants branch 2 times, most recently from d4af174 to 3209637 Compare April 2, 2024 18:57
@mitchellhenke mitchellhenke force-pushed the mitchellhenke/frozen-constants branch from d781305 to 8ccdcb9 Compare April 2, 2024 20:47
@mitchellhenke mitchellhenke force-pushed the mitchellhenke/frozen-constants branch from 1f51f6a to 0713b8d Compare April 3, 2024 17:23
@mitchellhenke mitchellhenke marked this pull request as ready for review April 3, 2024 19:49
@mitchellhenke mitchellhenke force-pushed the mitchellhenke/frozen-constants branch 2 times, most recently from b091ec7 to c964099 Compare April 3, 2024 21:08
RSpec.describe AbTests do
def reload_ab_test_initializer!
# undefine the AB tests instances so we can re-initialize them with different config values
AbTests.constants.each do |const_name|
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These tests are difficult to refactor as frozen constants, though it is in theory possible. The tests are mostly tests covering the behavior already tested in spec/lib/ab_test_bucket_spec.rb.


expect(result).to eq(doc_auth_vendor)
end
end

context 'with a discriminator that hashes inside the test group' do
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Similar to https://github.com/18F/identity-idp/pull/10340/files#r1550503287, this test group is mostly testing the bucket class rather than the implementation of it in DocAuthRouter. The test above for the nil discriminator is unique to the DocAuthRouter and won't be removed.

mitchellhenke and others added 7 commits April 4, 2024 08:31
changelog: Internal, Performance, Freeze constants

Co-authored-by: Zach Margolis <zachary.margolis@gsa.gov>
…ter.rb

Co-authored-by: Zach Margolis <zachmargolis@users.noreply.github.com>
Co-authored-by: Zach Margolis <zachmargolis@users.noreply.github.com>
@mitchellhenke mitchellhenke force-pushed the mitchellhenke/frozen-constants branch from c964099 to 69f5bc6 Compare April 4, 2024 13:31
@mitchellhenke mitchellhenke merged commit 80e2800 into main Apr 4, 2024
2 checks passed
@mitchellhenke mitchellhenke deleted the mitchellhenke/frozen-constants branch April 4, 2024 13:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants