-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
debugger_spec.rb
298 lines (252 loc) · 8.47 KB
/
debugger_spec.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Lint::Debugger, :config do
context 'with the DebuggerMethods configuration' do
let(:cop_config) { { 'DebuggerMethods' => %w[custom_debugger] } }
it 'does not register an offense for a byebug call' do
expect_no_offenses(<<~RUBY)
byebug
RUBY
end
it 'registers an offense for a `custom_debugger` call' do
expect_offense(<<~RUBY)
custom_debugger
^^^^^^^^^^^^^^^ Remove debugger entry point `custom_debugger`.
RUBY
end
context 'nested custom configurations' do
let(:cop_config) { { 'DebuggerMethods' => { 'Custom' => %w[custom_debugger] } } }
it 'registers an offense for a `custom_debugger call' do
expect_offense(<<~RUBY)
custom_debugger
^^^^^^^^^^^^^^^ Remove debugger entry point `custom_debugger`.
RUBY
end
end
end
context 'built-in methods' do
it 'registers an offense for a irb binding call' do
expect_offense(<<~RUBY)
binding.irb
^^^^^^^^^^^ Remove debugger entry point `binding.irb`.
RUBY
end
it 'registers an offense for a binding.irb with Kernel call' do
expect_offense(<<~RUBY)
Kernel.binding.irb
^^^^^^^^^^^^^^^^^^ Remove debugger entry point `Kernel.binding.irb`.
RUBY
end
end
context 'byebug' do
it 'registers an offense for a byebug call' do
expect_offense(<<~RUBY)
byebug
^^^^^^ Remove debugger entry point `byebug`.
RUBY
end
it 'registers an offense for a byebug with an argument call' do
expect_offense(<<~RUBY)
byebug foo
^^^^^^^^^^ Remove debugger entry point `byebug foo`.
RUBY
end
it 'registers an offense for a Kernel.byebug call' do
expect_offense(<<~RUBY)
Kernel.byebug
^^^^^^^^^^^^^ Remove debugger entry point `Kernel.byebug`.
RUBY
end
it 'registers an offense for a remote_byebug call' do
expect_offense(<<~RUBY)
remote_byebug
^^^^^^^^^^^^^ Remove debugger entry point `remote_byebug`.
RUBY
end
it 'registers an offense for a Kernel.remote_byebug call' do
expect_offense(<<~RUBY)
Kernel.remote_byebug
^^^^^^^^^^^^^^^^^^^^ Remove debugger entry point `Kernel.remote_byebug`.
RUBY
end
end
context 'capybara' do
it 'registers an offense for save_and_open_page' do
expect_offense(<<~RUBY)
save_and_open_page
^^^^^^^^^^^^^^^^^^ Remove debugger entry point `save_and_open_page`.
RUBY
end
it 'registers an offense for save_and_open_screenshot' do
expect_offense(<<~RUBY)
save_and_open_screenshot
^^^^^^^^^^^^^^^^^^^^^^^^ Remove debugger entry point `save_and_open_screenshot`.
RUBY
end
context 'with an argument' do
it 'registers an offense for save_and_open_page' do
expect_offense(<<~RUBY)
save_and_open_page foo
^^^^^^^^^^^^^^^^^^^^^^ Remove debugger entry point `save_and_open_page foo`.
RUBY
end
it 'registers an offense for save_and_open_screenshot' do
expect_offense(<<~RUBY)
save_and_open_screenshot foo
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Remove debugger entry point `save_and_open_screenshot foo`.
RUBY
end
end
end
context 'pry' do
it 'registers an offense for a pry binding call' do
expect_offense(<<~RUBY)
binding.pry
^^^^^^^^^^^ Remove debugger entry point `binding.pry`.
RUBY
end
it 'registers an offense for a remote_pry binding call' do
expect_offense(<<~RUBY)
binding.remote_pry
^^^^^^^^^^^^^^^^^^ Remove debugger entry point `binding.remote_pry`.
RUBY
end
it 'registers an offense for a pry_remote binding call' do
expect_offense(<<~RUBY)
binding.pry_remote
^^^^^^^^^^^^^^^^^^ Remove debugger entry point `binding.pry_remote`.
RUBY
end
it 'registers an offense for a pry binding with an argument call' do
expect_offense(<<~RUBY)
binding.pry foo
^^^^^^^^^^^^^^^ Remove debugger entry point `binding.pry foo`.
RUBY
end
it 'registers an offense for a remote_pry binding with an argument call' do
expect_offense(<<~RUBY)
binding.remote_pry foo
^^^^^^^^^^^^^^^^^^^^^^ Remove debugger entry point `binding.remote_pry foo`.
RUBY
end
it 'registers an offense for a pry_remote binding with an argument call' do
expect_offense(<<~RUBY)
binding.pry_remote foo
^^^^^^^^^^^^^^^^^^^^^^ Remove debugger entry point `binding.pry_remote foo`.
RUBY
end
it 'registers an offense for a binding.pry with Kernel call' do
expect_offense(<<~RUBY)
Kernel.binding.pry
^^^^^^^^^^^^^^^^^^ Remove debugger entry point `Kernel.binding.pry`.
RUBY
end
it 'registers an offense for a Pry.rescue call' do
expect_offense(<<~RUBY)
def method
Pry.rescue { puts 1 }
^^^^^^^^^^ Remove debugger entry point `Pry.rescue`.
::Pry.rescue { puts 1 }
^^^^^^^^^^^^ Remove debugger entry point `::Pry.rescue`.
end
RUBY
end
it 'does not register an offense for a `pry` call without binding' do
expect_no_offenses('pry')
end
it 'does not register an offense for a `rescue` call without Pry' do
expect_no_offenses(<<~RUBY)
begin
rescue StandardError
end
RUBY
end
end
context 'rails' do
it 'registers an offense for a debugger call' do
expect_offense(<<~RUBY)
debugger
^^^^^^^^ Remove debugger entry point `debugger`.
RUBY
end
it 'registers an offense for a debugger with an argument call' do
expect_offense(<<~RUBY)
debugger foo
^^^^^^^^^^^^ Remove debugger entry point `debugger foo`.
RUBY
end
it 'registers an offense for a debugger with Kernel call' do
expect_offense(<<~RUBY)
Kernel.debugger
^^^^^^^^^^^^^^^ Remove debugger entry point `Kernel.debugger`.
RUBY
end
it 'registers an offense for a debugger with ::Kernel call' do
expect_offense(<<~RUBY)
::Kernel.debugger
^^^^^^^^^^^^^^^^^ Remove debugger entry point `::Kernel.debugger`.
RUBY
end
end
context 'RubyJard' do
it 'registers an offense for a jard call' do
expect_offense(<<~RUBY)
jard
^^^^ Remove debugger entry point `jard`.
RUBY
end
end
context 'web console' do
it 'registers an offense for a `binding.console` call' do
expect_offense(<<~RUBY)
binding.console
^^^^^^^^^^^^^^^ Remove debugger entry point `binding.console`.
RUBY
end
it 'does not register an offense for `console` without a receiver' do
expect_no_offenses('console')
end
end
it 'does not register an offense for a binding method that is not disallowed' do
expect_no_offenses('binding.pirate')
end
%w[debugger byebug console pry remote_pry pry_remote irb save_and_open_page
save_and_open_screenshot remote_byebug].each do |src|
it "does not register an offense for a #{src} in comments" do
expect_no_offenses(<<~RUBY)
# #{src}
# Kernel.#{src}
RUBY
end
it "does not register an offense for a #{src} method" do
expect_no_offenses("code.#{src}")
end
end
context 'when a method group is disabled with nil' do
let!(:old_pry_config) { cur_cop_config['DebuggerMethods']['Pry'] }
before { cur_cop_config['DebuggerMethods']['Pry'] = nil }
after { cur_cop_config['DebuggerMethods']['Pry'] = old_pry_config }
it 'does not register an offense for a Pry debugger call' do
expect_no_offenses('binding.pry')
end
it 'does register an offense for another group' do
expect_offense(<<~RUBY)
binding.irb
^^^^^^^^^^^ Remove debugger entry point `binding.irb`.
RUBY
end
end
context 'when a method group is disabled with false' do
let!(:old_pry_config) { cur_cop_config['DebuggerMethods']['Pry'] }
before { cur_cop_config['DebuggerMethods']['Pry'] = false }
after { cur_cop_config['DebuggerMethods']['Pry'] = old_pry_config }
it 'does not register an offense for a Pry debugger call' do
expect_no_offenses('binding.pry')
end
it 'does register an offense for another group' do
expect_offense(<<~RUBY)
binding.irb
^^^^^^^^^^^ Remove debugger entry point `binding.irb`.
RUBY
end
end
end