diff options
author | Yves Senn <yves.senn@gmail.com> | 2013-10-25 06:10:11 -0700 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2013-10-25 06:10:11 -0700 |
commit | 2dc579baf4045fc6bcc38a2fe00d5fd19431201f (patch) | |
tree | 677910129b09dbf604993c5d4ec2834fd73d0d98 /activerecord/test/cases/helper.rb | |
parent | 3c3ffac0117a9a3ce4de1f84a7a3eb7a35d016a8 (diff) | |
parent | abe91cb3bf1b408a23958e14a68077e56a5a3b84 (diff) | |
download | rails-2dc579baf4045fc6bcc38a2fe00d5fd19431201f.tar.gz rails-2dc579baf4045fc6bcc38a2fe00d5fd19431201f.tar.bz2 rails-2dc579baf4045fc6bcc38a2fe00d5fd19431201f.zip |
Merge pull request #12633 from senny/no_more_time_zone_leaks
prevent global timezone state from leaking out of test cases.
Diffstat (limited to 'activerecord/test/cases/helper.rb')
-rw-r--r-- | activerecord/test/cases/helper.rb | 50 |
1 files changed, 47 insertions, 3 deletions
diff --git a/activerecord/test/cases/helper.rb b/activerecord/test/cases/helper.rb index 739e2b2f19..7b38fcf529 100644 --- a/activerecord/test/cases/helper.rb +++ b/activerecord/test/cases/helper.rb @@ -49,11 +49,55 @@ ensure old_tz ? ENV['TZ'] = old_tz : ENV.delete('TZ') end -def with_active_record_default_timezone(zone) - old_zone, ActiveRecord::Base.default_timezone = ActiveRecord::Base.default_timezone, zone +def with_timezone_config(cfg) + verify_default_timezone_config + + old_default_zone = ActiveRecord::Base.default_timezone + old_awareness = ActiveRecord::Base.time_zone_aware_attributes + old_zone = Time.zone + + if cfg.has_key?(:default) + ActiveRecord::Base.default_timezone = cfg[:default] + end + if cfg.has_key?(:aware_attributes) + ActiveRecord::Base.time_zone_aware_attributes = cfg[:aware_attributes] + end + if cfg.has_key?(:zone) + Time.zone = cfg[:zone] + end yield ensure - ActiveRecord::Base.default_timezone = old_zone + ActiveRecord::Base.default_timezone = old_default_zone + ActiveRecord::Base.time_zone_aware_attributes = old_awareness + Time.zone = old_zone +end + +# This method makes sure that tests don't leak global state related to time zones. +EXPECTED_ZONE = nil +EXPECTED_DEFAULT_TIMEZONE = :utc +EXPECTED_TIME_ZONE_AWARE_ATTRIBUTES = false +def verify_default_timezone_config + if Time.zone != EXPECTED_ZONE + raise <<-MSG + Global state `Time.zone` was leaked. + Expected: #{EXPECTED_ZONE} + Got: #{Time.zone} + MSG + end + if ActiveRecord::Base.default_timezone != EXPECTED_DEFAULT_TIMEZONE + raise <<-MSG + Global state `ActiveRecord::Base.default_timezone` was leaked. + Expected: #{EXPECTED_DEFAULT_TIMEZONE} + Got: #{ActiveRecord::Base.default_timezone} + MSG + end + if ActiveRecord::Base.time_zone_aware_attributes != EXPECTED_TIME_ZONE_AWARE_ATTRIBUTES + raise <<-MSG + Global state `ActiveRecord::Base.time_zone_aware_attributes` was leaked. + Expected: #{EXPECTED_TIME_ZONE_AWARE_ATTRIBUTES} + Got: #{ActiveRecord::Base.time_zone_aware_attributes} + MSG + end end unless ENV['FIXTURE_DEBUG'] |