Skip to content

Enable Rubocop Rule to Freeze Constants#10340

Merged
mitchellhenke merged 17 commits intomainfrom
mitchellhenke/frozen-constants
Apr 4, 2024
Merged

Enable Rubocop Rule to Freeze Constants#10340
mitchellhenke merged 17 commits intomainfrom
mitchellhenke/frozen-constants

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
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.

@mitchellhenke mitchellhenke force-pushed the mitchellhenke/frozen-constants branch from 37762a0 to 3d345ad Compare March 29, 2024 17:27
@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
Copy link
Contributor Author

@mitchellhenke mitchellhenke Apr 3, 2024

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.

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.

Mitchell Henke 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
@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.

2 participants