-
Notifications
You must be signed in to change notification settings - Fork 21.3k
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 exception in AS::Timezone.all when any tzinfo data is missing #32613
Conversation
Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @kaspth (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review. Please see the contribution instructions for more information. |
@@ -1,3 +1,8 @@ | |||
* Fix bug where `ActiveSupport::Timezone.all` would fail when tzinfo data for | |||
any timezone defined in `ActiveSupport::MAPPING` is missing. |
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.
How would this happen in real world? All zones defined in that constant we know exists.
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.
They do exist if the tzdata
system package is current, but when it is not TZInfo
does not know anything about them and raises an exception itself. Before 078421b AS::Timezone.all
just omitted those timezones.
end | ||
klass.clear | ||
yield klass | ||
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.
Replace this method with this:
def with_tz_mappings(mappings)
old_mappings = ActiveSupport::TimeZone::MAPPING.dup
ActiveSupport::TimeZone.clear
ActiveSupport::TimeZone::MAPPING.clear
ActiveSupport::TimeZone::MAPPING.merge!(mappings)
yield
ensure
ActiveSupport::TimeZone.clear
ActiveSupport::TimeZone::MAPPING.clear
ActiveSupport::TimeZone::MAPPING.merge!(old_mappings)
end
and use as explained above.
activesupport/test/time_zone_test.rb
Outdated
end | ||
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.
While creating a subclass works as a way of reproducing the error it doesn't really reflect real-life usage so I'd prefer a test that more accurately reflects the error which we can do by changing the mappings for this test using the helper method in the comment below. With that you can then change the test to this:
def test_all_not_raises_exception_with_mizzing_tzinfo_data
mappings = {
"Puerto Rico" => "America/Unknown",
"Pittsburgh" => "America/New_York"
}
with_tz_mappings(mappings) do
assert_nil ActiveSupport::TimeZone["Puerto Rico"]
assert_nil ActiveSupport::TimeZone[-9]
assert_nothing_raised do
ActiveSupport::TimeZone.all
end
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.
Thanks, I wasn't particular happy with creating the subclass and now realize that I created the test from the wrong "perspective".
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 think the change is 👍 we just need to change the tests as I've outlined, thanks.
@pixeltrix Thanks for the feedback, should I squash the commits into one? |
Yes |
427cd27
to
45ed001
Compare
Before this change missing timezone data for any of the time zones defined in `ActiveSupport::Timezone::MAPPING` caused a `comparison of NilClass with ActiveSupport::TimeZone failed` exception. Attempting to get a timezone by passing a number/duration to `[]` or calling `all` directly will try to sort sort the values of `zones_map`. Those values are initialized by the return value of `create(zonename)` which returns `nil` if `TZInfo` is unable to find the timezone information. In our case the exception was triggered by an outdated tzdata package which did not include information for the "recently" added time zones. Before 078421b `zones_map` only returned the information that have been loaded into `@lazy_zone_map` which ignored time zones for which the data could not be loaded, this change restores the previous behaviour.
45ed001
to
7d25b65
Compare
@dsander thanks! 👍 |
Fix exception in AS::Timezone.all when any tzinfo data is missing
Fix name of the test added by #32613
Fix name of the test added by #32613
Before this change missing timezone data for any of the time zones defined in
ActiveSupport::Timezone::MAPPING
caused acomparison of NilClass with ActiveSupport::TimeZone failed
exception.Attempting to get a timezone by passing a number/duration to
[]
or callingall
directly will try to sort sort the values ofzones_map
. Those values are initialized by the return value ofcreate(zonename)
which returnsnil
ifTZInfo
is unable to find the timezone information.In our case the exception was triggered by an outdated tzdata package which did not include information for the "recently" added time zones.
Before 078421b
zones_map
only returned the information that have been loaded into@lazy_zone_map
which ignored time zones for which the data could not be loaded, this change restores the previous behaviour.