forked from thedarkone/rails-dev-boost
/
rails_development_boost_test.rb
222 lines (179 loc) · 5.49 KB
/
rails_development_boost_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
require 'test/unit'
require 'mocha'
require 'stub_environment'
require 'rails_development_boost'
RailsDevelopmentBoost.apply!
class RailsDevelopmentBoostTest < Test::Unit::TestCase
def test_single_removal
load_from "single_removal"
assert_same_object_id('A') { reload! }
assert_different_object_id('A') { reload! { update("a.rb") } }
assert_different_object_id('A') { reload! { update("a.rb") } }
assert_same_object_id('A') { reload! }
assert_same_object_id('B') do
assert_different_object_id('A') do
reload! do
update("a.rb")
end
end
end
end
def test_subclass_update_cascade
load_from "subclass"
assert_different_object_id 'A', 'B' do
assert_same_object_id 'C' do
reload! do
update("a.rb")
end
end
end
end
def test_nested_constant_update_cascade
load_from "deep_nesting"
assert_different_object_id 'A::B::C::D', 'A::B::C', 'A::B', 'A' do
reload! do
update("a.rb")
end
end
end
def test_mixin_update_cascade
load_from "mixins"
assert_different_object_id 'Mixin', 'Client' do
reload! do
update("mixin.rb")
end
end
assert Client.public_method_defined?('from_mixin') # sanity check
# Simulate a change in the mixin file
reload! do
update("mixin.rb")
end
Deps.load_paths.unshift("#{@constant_dir}/update")
assert !Client.public_method_defined?('from_mixin')
assert Client.public_method_defined?('from_mixin_update')
end
def test_prevention_of_removal_cycle
load_from "double_removal"
# Failure of this test = SystemStackError: stack level too deep
assert_different_object_id 'Ns::M', 'Ns::C', 'Ns' do
reload! do
update("ns/m.rb")
end
end
end
def test_nested_mixins
load_from "nested_mixins"
assert_different_object_id 'Ma::Mb::Mc', 'Ma::Mb', 'Ma' do
assert_different_object_id 'Oa::Ob::Oc', 'Oa::Ob', 'Oa' do
assert_same_object_id 'B::C' do
reload! do
update("ma/mb/mc.rb")
end
end
end
end
end
def test_singleton_mixins
load_from "singleton_mixins"
assert_different_object_id 'A' do
reload! do
update("b.rb")
end
end
assert_same_object_id 'B' do
reload! do
update("a.rb")
end
end
end
def test_consistency_of_activerecord_registry
load_from "active_record"
fetch_registered_ar_subclasses = lambda do
ActiveRecord::Base.instance_eval { subclasses }.sort_by(&:name)
end
# Load initial version of the models
assert_equal [Comment, Message, Other, Post], fetch_registered_ar_subclasses[]
# AR::Base subclass tree is updated
assert_different_object_id 'Message', 'Post', 'Comment' do
assert_same_object_id 'Other' do
reload! do
update("message.rb")
end
end
end
assert_equal [Comment, Message, Other, Post], fetch_registered_ar_subclasses[]
# Create initial references to reflection classes
assert_equal Comment, Post.new.comments.new.class
# Reflections are updated
assert_same_object_id 'Post' do
assert_different_object_id 'Comment' do
reload! do
update("comment.rb")
end
end
end
assert_equal Comment, Post.new.comments.new.class
end
protected
CONSTANT_DIR = "#{File.dirname(__FILE__)}/constants".freeze
CONSTANT_FILES = Dir.chdir(CONSTANT_DIR) { Dir.glob("**/*.rb") }.freeze
Deps = ActiveSupport::Dependencies
def setup
# Cleanup
clean_up! "setup"
@constant_dir = CONSTANT_DIR
# Configuration
Deps.load_paths = [CONSTANT_DIR]
Deps.logger = Logger.new(STDERR)
Deps.log_activity = false
# Stub mtimes
CONSTANT_FILES.each { |file| stub_mtime(file) }
end
def teardown
clean_up! "teardown"
end
private
def load_from(root)
@constant_dir = "#{CONSTANT_DIR}/#{root}"
Deps.load_paths = [@constant_dir]
end
def update(path)
stub_mtime(path, File.mtime("#{@constant_dir}/#{path}") + 1)
end
def stub_mtime(path, time=1)
File.stubs(:mtime).with("#{@constant_dir}/#{path}").returns time
end
def reload!
ActionController::Dispatcher.new.cleanup_application
yield if block_given?
ActionController::Dispatcher.new.reload_application
end
def clean_up!(stage)
message = "#{stage} dependency cleanup of <#{@method_name}> failed"
Deps.clear
Deps.history.clear
assert_equal([], Deps.constants_being_removed, message)
assert_equal([], Deps.module_cache, message)
assert_equal(Set.new, Deps.loaded, message)
assert_equal({}, Deps.file_map, message)
assert_equal([], Deps.autoloaded_constants, message)
end
def assert_same_object_id(*expressions, &block)
each_object_id_diff(block, expressions) do |expr, before, after|
assert_equal(before, after, "<#{expr}.object_id> has changed")
end
end
def assert_different_object_id(*expressions, &block)
each_object_id_diff(block, expressions) do |expr, before, after|
assert_not_equal(before, after, "<#{expr}.object_id> has remained the same")
end
end
def each_object_id_diff(alter, expressions)
ids_before = expressions.map { |expr| [expr, eval("#{expr}.object_id")] }
alter.call
ids_before.each do |expr, before|
after = eval("#{expr}.object_id")
yield expr, before, after
end
end
end