Skip to content

Commit 467cf48

Browse files
johnnyshieldsjamis
andauthored
MONGOID-5661 [Monkey Patch Removal] Add test which checks for monkey patches (#5711)
* Add script to check monkey patches * Update monkey_patches_spec.rb * Update monkey_patches_spec.rb --------- Co-authored-by: Jamis Buck <[email protected]>
1 parent 6491384 commit 467cf48

File tree

2 files changed

+208
-0
lines changed

2 files changed

+208
-0
lines changed

spec/mongoid/criteria/queryable/extensions/symbol_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
end
1414
end
1515

16+
after do
17+
Symbol.undef_method(:fubar)
18+
end
19+
1620
let(:fubar) do
1721
:testing.fubar
1822
end

spec/mongoid/monkey_patches_spec.rb

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
# @note This test ensures that we do not inadvertently introduce new monkey patches
6+
# to Mongoid. Existing monkey patch methods which are marked with +Mongoid.deprecated+
7+
# are excluded from this test.
8+
RSpec.describe('Do not add monkey patches') do # rubocop:disable RSpec/DescribeClass
9+
classes = [
10+
Object,
11+
Array,
12+
BigDecimal,
13+
Date,
14+
DateTime,
15+
FalseClass,
16+
Float,
17+
Hash,
18+
Integer,
19+
Module,
20+
NilClass,
21+
Range,
22+
Regexp,
23+
Set,
24+
String,
25+
Symbol,
26+
Time,
27+
TrueClass,
28+
ActiveSupport::TimeWithZone,
29+
BSON::Binary,
30+
BSON::Decimal128,
31+
BSON::ObjectId,
32+
BSON::Regexp,
33+
BSON::Regexp::Raw
34+
]
35+
36+
expected_instance_methods = {
37+
Object => %i[
38+
__add__
39+
__add_from_array__
40+
__array__
41+
__deep_copy__
42+
__evolve_object_id__
43+
__expand_complex__
44+
__intersect__
45+
__intersect_from_array__
46+
__intersect_from_object__
47+
__mongoize_object_id__
48+
__mongoize_time__
49+
__union__
50+
__union_from_object__
51+
ivar
52+
mongoize
53+
numeric?
54+
remove_ivar
55+
resizable?
56+
substitutable
57+
],
58+
Array => %i[
59+
__evolve_date__
60+
__evolve_time__
61+
__sort_option__
62+
__sort_pair__
63+
delete_one
64+
],
65+
Date => %i[
66+
__evolve_date__
67+
__evolve_time__
68+
],
69+
DateTime => %i[
70+
__evolve_date__
71+
__evolve_time__
72+
],
73+
FalseClass => %i[is_a?],
74+
Float => %i[
75+
__evolve_date__
76+
__evolve_time__
77+
],
78+
Hash => %i[
79+
__sort_option__
80+
],
81+
Integer => %i[
82+
__evolve_date__
83+
__evolve_time__
84+
],
85+
Module => %i[
86+
re_define_method
87+
],
88+
NilClass => %i[
89+
__evolve_date__
90+
__evolve_time__
91+
__expanded__
92+
__override__
93+
collectionize
94+
],
95+
Range => %i[
96+
__evolve_date__
97+
__evolve_range__
98+
__evolve_time__
99+
],
100+
String => %i[
101+
__evolve_date__
102+
__evolve_time__
103+
__expr_part__
104+
__mongo_expression__
105+
__sort_option__
106+
before_type_cast?
107+
collectionize
108+
reader
109+
valid_method_name?
110+
writer?
111+
],
112+
Symbol => %i[
113+
__expr_part__
114+
add_to_set
115+
all
116+
asc
117+
ascending
118+
avg
119+
desc
120+
descending
121+
elem_match
122+
eq
123+
exists
124+
first
125+
gt
126+
gte
127+
in
128+
intersects_line
129+
intersects_point
130+
intersects_polygon
131+
last
132+
lt
133+
lte
134+
max
135+
min
136+
mod
137+
ne
138+
near
139+
near_sphere
140+
nin
141+
not
142+
push
143+
sum
144+
with_size
145+
with_type
146+
within_box
147+
within_polygon
148+
],
149+
TrueClass => %i[is_a?],
150+
Time => %i[
151+
__evolve_date__
152+
__evolve_time__
153+
],
154+
ActiveSupport::TimeWithZone => %i[
155+
__evolve_date__
156+
__evolve_time__
157+
_bson_to_i
158+
],
159+
BSON::Decimal128 => %i[
160+
__evolve_decimal128__
161+
]
162+
}.each_value(&:sort!)
163+
164+
expected_class_methods = {
165+
Object => %i[
166+
demongoize
167+
evolve
168+
re_define_method
169+
],
170+
Float => %i[__numeric__],
171+
Integer => %i[__numeric__],
172+
String => %i[__expr_part__],
173+
Symbol => %i[add_key]
174+
}.each_value(&:sort!)
175+
176+
def mongoid_method?(method)
177+
method.source_location&.first&.include?('/lib/mongoid/')
178+
end
179+
180+
def added_instance_methods(klass)
181+
methods = klass.instance_methods.select { |m| mongoid_method?(klass.instance_method(m)) }
182+
methods -= added_instance_methods(Object) unless klass == Object
183+
methods.sort
184+
end
185+
186+
def added_class_methods(klass)
187+
methods = klass.methods.select { |m| mongoid_method?(klass.method(m)) }
188+
methods -= added_instance_methods(Object)
189+
methods -= added_class_methods(Object) unless klass == Object
190+
methods.sort
191+
end
192+
193+
classes.each do |klass|
194+
context klass.name do
195+
it 'adds no unexpected instance methods' do
196+
expect(added_instance_methods(klass)).to eq(expected_instance_methods[klass] || [])
197+
end
198+
199+
it 'adds no unexpected class methods' do
200+
expect(added_class_methods(klass)).to eq(expected_class_methods[klass] || [])
201+
end
202+
end
203+
end
204+
end

0 commit comments

Comments
 (0)