Skip to content

Commit a18d90e

Browse files
committed
Don't pollute Object with test helper methods
Defining methods on toplevel changes the behavior of all Ruby Objects which of course includes the test target, and thus that will spoil the meaning of the whole testing. This is something that testing libraries or helpers should never do. These methods are just called from testing methods, so we can just move them under AR::TestCase as its instance methods.
1 parent dce7c1c commit a18d90e

File tree

2 files changed

+85
-85
lines changed

2 files changed

+85
-85
lines changed

activerecord/test/cases/helper.rb

Lines changed: 0 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -93,81 +93,6 @@ def supports_text_column_with_default?
9393
end
9494
end
9595

96-
def with_env_tz(new_tz = "US/Eastern")
97-
old_tz, ENV["TZ"] = ENV["TZ"], new_tz
98-
yield
99-
ensure
100-
old_tz ? ENV["TZ"] = old_tz : ENV.delete("TZ")
101-
end
102-
103-
def with_timezone_config(cfg)
104-
verify_default_timezone_config
105-
106-
old_default_zone = ActiveRecord.default_timezone
107-
old_awareness = ActiveRecord::Base.time_zone_aware_attributes
108-
old_aware_types = ActiveRecord::Base.time_zone_aware_types
109-
old_zone = Time.zone
110-
111-
if cfg.has_key?(:default)
112-
ActiveRecord.default_timezone = cfg[:default]
113-
end
114-
if cfg.has_key?(:aware_attributes)
115-
ActiveRecord::Base.time_zone_aware_attributes = cfg[:aware_attributes]
116-
end
117-
if cfg.has_key?(:aware_types)
118-
ActiveRecord::Base.time_zone_aware_types = cfg[:aware_types]
119-
end
120-
if cfg.has_key?(:zone)
121-
Time.zone = cfg[:zone]
122-
end
123-
yield
124-
ensure
125-
ActiveRecord.default_timezone = old_default_zone
126-
ActiveRecord::Base.time_zone_aware_attributes = old_awareness
127-
ActiveRecord::Base.time_zone_aware_types = old_aware_types
128-
Time.zone = old_zone
129-
end
130-
131-
# This method makes sure that tests don't leak global state related to time zones.
132-
EXPECTED_ZONE = nil
133-
EXPECTED_DEFAULT_TIMEZONE = :utc
134-
EXPECTED_AWARE_TYPES = [:datetime, :time]
135-
EXPECTED_TIME_ZONE_AWARE_ATTRIBUTES = false
136-
def verify_default_timezone_config
137-
if Time.zone != EXPECTED_ZONE
138-
$stderr.puts <<-MSG
139-
\n#{self}
140-
Global state `Time.zone` was leaked.
141-
Expected: #{EXPECTED_ZONE}
142-
Got: #{Time.zone}
143-
MSG
144-
end
145-
if ActiveRecord.default_timezone != EXPECTED_DEFAULT_TIMEZONE
146-
$stderr.puts <<-MSG
147-
\n#{self}
148-
Global state `ActiveRecord.default_timezone` was leaked.
149-
Expected: #{EXPECTED_DEFAULT_TIMEZONE}
150-
Got: #{ActiveRecord.default_timezone}
151-
MSG
152-
end
153-
if ActiveRecord::Base.time_zone_aware_attributes != EXPECTED_TIME_ZONE_AWARE_ATTRIBUTES
154-
$stderr.puts <<-MSG
155-
\n#{self}
156-
Global state `ActiveRecord::Base.time_zone_aware_attributes` was leaked.
157-
Expected: #{EXPECTED_TIME_ZONE_AWARE_ATTRIBUTES}
158-
Got: #{ActiveRecord::Base.time_zone_aware_attributes}
159-
MSG
160-
end
161-
if ActiveRecord::Base.time_zone_aware_types != EXPECTED_AWARE_TYPES
162-
$stderr.puts <<-MSG
163-
\n#{self}
164-
Global state `ActiveRecord::Base.time_zone_aware_types` was leaked.
165-
Expected: #{EXPECTED_AWARE_TYPES}
166-
Got: #{ActiveRecord::Base.time_zone_aware_types}
167-
MSG
168-
end
169-
end
170-
17196
def enable_extension!(extension, connection)
17297
return false unless connection.supports_extensions?
17398
return connection.reconnect! if connection.extension_enabled?(extension)
@@ -185,16 +110,6 @@ def disable_extension!(extension, connection)
185110
connection.reconnect!
186111
end
187112

188-
def clean_up_connection_handler
189-
handler = ActiveRecord::Base.connection_handler
190-
handler.instance_variable_get(:@connection_name_to_pool_manager).each do |owner, pool_manager|
191-
pool_manager.role_names.each do |role_name|
192-
next if role_name == ActiveRecord::Base.default_role
193-
pool_manager.remove_role(role_name)
194-
end
195-
end
196-
end
197-
198113
def load_schema
199114
# silence verbose schema loading
200115
original_stdout = $stdout

activerecord/test/cases/test_case.rb

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,91 @@ def with_postgresql_datetime_type(type)
132132
adapter.datetime_type = datetime_type_was
133133
adapter.remove_instance_variable(:@native_database_types) if adapter.instance_variable_defined?(:@native_database_types)
134134
end
135+
136+
def with_env_tz(new_tz = "US/Eastern")
137+
old_tz, ENV["TZ"] = ENV["TZ"], new_tz
138+
yield
139+
ensure
140+
old_tz ? ENV["TZ"] = old_tz : ENV.delete("TZ")
141+
end
142+
143+
def with_timezone_config(cfg)
144+
verify_default_timezone_config
145+
146+
old_default_zone = ActiveRecord.default_timezone
147+
old_awareness = ActiveRecord::Base.time_zone_aware_attributes
148+
old_aware_types = ActiveRecord::Base.time_zone_aware_types
149+
old_zone = Time.zone
150+
151+
if cfg.has_key?(:default)
152+
ActiveRecord.default_timezone = cfg[:default]
153+
end
154+
if cfg.has_key?(:aware_attributes)
155+
ActiveRecord::Base.time_zone_aware_attributes = cfg[:aware_attributes]
156+
end
157+
if cfg.has_key?(:aware_types)
158+
ActiveRecord::Base.time_zone_aware_types = cfg[:aware_types]
159+
end
160+
if cfg.has_key?(:zone)
161+
Time.zone = cfg[:zone]
162+
end
163+
yield
164+
ensure
165+
ActiveRecord.default_timezone = old_default_zone
166+
ActiveRecord::Base.time_zone_aware_attributes = old_awareness
167+
ActiveRecord::Base.time_zone_aware_types = old_aware_types
168+
Time.zone = old_zone
169+
end
170+
171+
# This method makes sure that tests don't leak global state related to time zones.
172+
EXPECTED_ZONE = nil
173+
EXPECTED_DEFAULT_TIMEZONE = :utc
174+
EXPECTED_AWARE_TYPES = [:datetime, :time]
175+
EXPECTED_TIME_ZONE_AWARE_ATTRIBUTES = false
176+
def verify_default_timezone_config
177+
if Time.zone != EXPECTED_ZONE
178+
$stderr.puts <<-MSG
179+
\n#{self}
180+
Global state `Time.zone` was leaked.
181+
Expected: #{EXPECTED_ZONE}
182+
Got: #{Time.zone}
183+
MSG
184+
end
185+
if ActiveRecord.default_timezone != EXPECTED_DEFAULT_TIMEZONE
186+
$stderr.puts <<-MSG
187+
\n#{self}
188+
Global state `ActiveRecord.default_timezone` was leaked.
189+
Expected: #{EXPECTED_DEFAULT_TIMEZONE}
190+
Got: #{ActiveRecord.default_timezone}
191+
MSG
192+
end
193+
if ActiveRecord::Base.time_zone_aware_attributes != EXPECTED_TIME_ZONE_AWARE_ATTRIBUTES
194+
$stderr.puts <<-MSG
195+
\n#{self}
196+
Global state `ActiveRecord::Base.time_zone_aware_attributes` was leaked.
197+
Expected: #{EXPECTED_TIME_ZONE_AWARE_ATTRIBUTES}
198+
Got: #{ActiveRecord::Base.time_zone_aware_attributes}
199+
MSG
200+
end
201+
if ActiveRecord::Base.time_zone_aware_types != EXPECTED_AWARE_TYPES
202+
$stderr.puts <<-MSG
203+
\n#{self}
204+
Global state `ActiveRecord::Base.time_zone_aware_types` was leaked.
205+
Expected: #{EXPECTED_AWARE_TYPES}
206+
Got: #{ActiveRecord::Base.time_zone_aware_types}
207+
MSG
208+
end
209+
end
210+
211+
def clean_up_connection_handler
212+
handler = ActiveRecord::Base.connection_handler
213+
handler.instance_variable_get(:@connection_name_to_pool_manager).each do |owner, pool_manager|
214+
pool_manager.role_names.each do |role_name|
215+
next if role_name == ActiveRecord::Base.default_role
216+
pool_manager.remove_role(role_name)
217+
end
218+
end
219+
end
135220
end
136221

137222
class PostgreSQLTestCase < TestCase

0 commit comments

Comments
 (0)