rails/activesupport/test/time_zone_test_helpers.rb
Dominik Sander 7d25b651fa Fix exception in AS::Timezone.all when any tzinfo data is missing
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 078421bacba178eac6a8e607b16f3f4511c5d72f `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.
2018-04-18 21:45:13 +02:00

40 lines
967 B
Ruby

# frozen_string_literal: true
module TimeZoneTestHelpers
def with_tz_default(tz = nil)
old_tz = Time.zone
Time.zone = tz
yield
ensure
Time.zone = old_tz
end
def with_env_tz(new_tz = "US/Eastern")
old_tz, ENV["TZ"] = ENV["TZ"], new_tz
yield
ensure
old_tz ? ENV["TZ"] = old_tz : ENV.delete("TZ")
end
def with_preserve_timezone(value)
old_preserve_tz = ActiveSupport.to_time_preserves_timezone
ActiveSupport.to_time_preserves_timezone = value
yield
ensure
ActiveSupport.to_time_preserves_timezone = old_preserve_tz
end
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
end