-
-
Notifications
You must be signed in to change notification settings - Fork 269
/
file_path_spec.rb
310 lines (262 loc) · 9.91 KB
/
file_path_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
299
300
301
302
303
304
305
306
307
308
309
310
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::RSpec::FilePath do
it 'registers an offense for a bad path' do
expect_offense(<<-RUBY, 'wrong_path_foo_spec.rb')
describe MyClass, 'foo' do; end
^^^^^^^^^^^^^^^^^^^^^^^ Spec path should end with `my_class*foo*_spec.rb`.
RUBY
end
it 'registers an offense for a bad path for all kinds of example groups' do
expect_offense(<<-RUBY, 'wrong_path_foo_spec.rb')
example_group MyClass, 'foo' do; end
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Spec path should end with `my_class*foo*_spec.rb`.
RUBY
end
it 'registers an offense for a wrong class but a correct method' do
expect_offense(<<-RUBY, 'wrong_class_foo_spec.rb')
describe MyClass, '#foo' do; end
^^^^^^^^^^^^^^^^^^^^^^^^ Spec path should end with `my_class*foo*_spec.rb`.
RUBY
end
it 'registers an offense for a repeated .rb' do
expect_offense(<<-RUBY, 'my_class/foo_spec.rb.rb')
describe MyClass, '#foo' do; end
^^^^^^^^^^^^^^^^^^^^^^^^ Spec path should end with `my_class*foo*_spec.rb`.
RUBY
end
it 'registers an offense for a file missing a .rb' do
expect_offense(<<-RUBY, 'my_class/foo_specorb')
describe MyClass, '#foo' do; end
^^^^^^^^^^^^^^^^^^^^^^^^ Spec path should end with `my_class*foo*_spec.rb`.
RUBY
end
it 'registers an offense for a wrong class and highlights metadata' do
expect_offense(<<-RUBY, 'wrong_class_foo_spec.rb')
describe MyClass, '#foo', blah: :blah do; end
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Spec path should end with `my_class*foo*_spec.rb`.
RUBY
end
it 'registers an offense for a wrong class name' do
expect_offense(<<-RUBY, 'wrong_class_spec.rb')
describe MyClass do; end
^^^^^^^^^^^^^^^^ Spec path should end with `my_class*_spec.rb`.
RUBY
end
it 'registers an offense for a wrong class name with a symbol argument' do
expect_offense(<<-RUBY, 'wrong_class_spec.rb')
describe MyClass, :foo do; end
^^^^^^^^^^^^^^^^^^^^^^ Spec path should end with `my_class*_spec.rb`.
RUBY
end
it 'registers an offense for a file missing _spec' do
expect_offense(<<-RUBY, 'spec/models/user.rb')
describe User do; end
^^^^^^^^^^^^^ Spec path should end with `user*_spec.rb`.
RUBY
end
it 'registers an offense for a feature file missing _spec' do
expect_offense(<<-RUBY, 'spec/features/my_feature.rb')
feature "my feature" do; end
^^^^^^^^^^^^^^^^^^^^ Spec path should end with `*_spec.rb`.
RUBY
end
it 'registers an offense for a file without the .rb extension' do
expect_offense(<<-RUBY, 'spec/models/user_specxrb')
describe User do; end
^^^^^^^^^^^^^ Spec path should end with `user*_spec.rb`.
RUBY
end
it 'does not register an offense for shared examples' do
expect_no_offenses(<<-RUBY, 'spec/models/user.rb')
shared_examples_for 'foo' do; end
RUBY
end
it 'does not register an offence for example groups '\
'do not describe a class / method' do
expect_no_offenses(<<-RUBY, 'some/class/spec.rb')
describe 'Test something' do; end
RUBY
end
it 'does not register an offense for multiple top level describes' do
expect_no_offenses(<<-RUBY, 'some/class/spec.rb')
describe MyClass, 'do_this' do; end
describe MyClass, 'do_that' do; end
RUBY
end
it 'does not register an offense for class specs' do
expect_no_offenses(<<-RUBY, 'some/class_spec.rb')
describe Some::Class do; end
RUBY
end
it 'does not register an offense for different parent directories' do
expect_no_offenses(<<-RUBY, 'parent_dir/some/class_spec.rb')
describe Some::Class do; end
RUBY
end
it 'does not register an offense for CamelCaps class names' do
expect_no_offenses(<<-RUBY, 'my_class_spec.rb')
describe MyClass do; end
RUBY
end
it 'does not register an offense for ACRONYMClassNames' do
expect_no_offenses(<<-RUBY, 'abc_one/two_spec.rb')
describe ABCOne::Two do; end
RUBY
end
it 'does not register an offense for ALLCAPS class names' do
expect_no_offenses(<<-RUBY, 'allcaps_spec.rb')
describe ALLCAPS do; end
RUBY
end
it 'does not register an offense for alphanumeric class names' do
expect_no_offenses(<<-RUBY, 'ipv4_and_ipv6_spec.rb')
describe IPV4AndIPV6 do; end
RUBY
end
it 'does not register an offense for instance methods' do
expect_no_offenses(<<-RUBY, 'some/class/inst_spec.rb')
describe Some::Class, '#inst' do; end
RUBY
end
it 'does not register an offense for class methods' do
expect_no_offenses(<<-RUBY, 'some/class/inst_spec.rb')
describe Some::Class, '.inst' do; end
RUBY
end
it 'does not register an offense for flat hierarchies for instance methods' do
expect_no_offenses(<<-RUBY, 'some/class_inst_spec.rb')
describe Some::Class, '#inst' do; end
RUBY
end
it 'does not register an offense for flat hierarchies for class methods' do
expect_no_offenses(<<-RUBY, 'some/class_inst_spec.rb')
describe Some::Class, '.inst' do; end
RUBY
end
it 'does not register an offense for subdirs for instance methods' do
filename = 'some/class/instance_methods/inst_spec.rb'
expect_no_offenses(<<-RUBY, filename)
describe Some::Class, '#inst' do; end
RUBY
end
it 'does not register an offense for subdirs for class methods' do
filename = 'some/class/class_methods/inst_spec.rb'
expect_no_offenses(<<-RUBY, filename)
describe Some::Class, '.inst' do; end
RUBY
end
it 'does not register an offense for non-alphanumeric characters' do
expect_no_offenses(<<-RUBY, 'some/class/pred_spec.rb')
describe Some::Class, '#pred?' do; end
RUBY
end
it 'does not register an offense for bang method' do
expect_no_offenses(<<-RUBY, 'some/class/bang_spec.rb')
describe Some::Class, '#bang!' do; end
RUBY
end
it 'does not register an offence for an arbitrary spec suffix' do
filename = 'some/class/thing_predicate_spec.rb'
expect_no_offenses(<<-RUBY, filename)
describe Some::Class, '#thing?' do; end
RUBY
end
it 'does not register an offence for an arbitrary spec name '\
'for an operator method' do
filename = 'my_little_class/spaceship_operator_spec.rb'
expect_no_offenses(<<-RUBY, filename)
describe MyLittleClass, '#<=>' do; end
RUBY
end
it 'registers an offense for a path containing the class name' do
expect_offense(<<-RUBY, '/home/foo/spec/models/bar_spec.rb')
describe Foo do; end
^^^^^^^^^^^^ Spec path should end with `foo*_spec.rb`.
RUBY
end
it 'registers an offense for path based on a class name with long module' do
expect_offense(<<-RUBY, '/home/foo/spec/very/my_class_spec.rb')
describe Very::Long::Namespace::MyClass do; end
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Spec path should end with `very/long/namespace/my_class*_spec.rb`.
RUBY
end
it 'does not register offense for absolute file path' do
allow(File).to receive(:expand_path).with('my_class_spec.rb').and_return(
'/home/foo/spec/very/long/namespace/my_class_spec.rb'
)
expect_no_offenses(<<-RUBY, 'my_class_spec.rb')
describe Very::Long::Namespace::MyClass do; end
RUBY
end
# RSpec/FilePath runs on all files - not only **/*_spec.rb
it 'does not register an offense for files defining an empty class' do
expect_no_offenses(<<-RUBY)
class Foo
end
RUBY
end
context 'when configured with CustomTransform' do
let(:cop_config) { { 'CustomTransform' => { 'FooFoo' => 'foofoo' } } }
it 'does not register an offense for custom module name transformation' do
expect_no_offenses(<<-RUBY, 'foofoo/some/class/bar_spec.rb')
describe FooFoo::Some::Class, '#bar' do; end
RUBY
end
it 'does not register an offense for routing specs' do
expect_no_offenses(<<-RUBY, 'foofoo/some/class/bar_spec.rb')
describe MyController, "#foo", type: :routing do; end
RUBY
end
end
context 'when ActiveSupport Inflector is defined' do
before { require 'active_support/inflector' }
it 'registers an offense for a bad path when there is no custom acronym' do
expect_offense(<<-RUBY, 'pvp_class_foo_spec.rb')
describe PvPClass, 'foo' do; end
^^^^^^^^^^^^^^^^^^^^^^^^ Spec path should end with `pv_p_class*foo*_spec.rb`.
RUBY
end
it 'does not register an offense when class name contains custom acronym' do
ActiveSupport::Inflector.inflections do |inflect|
inflect.acronym('PvP')
end
expect_no_offenses(<<-RUBY, 'pvp_class_foo_spec.rb')
describe PvPClass, 'foo' do; end
RUBY
end
end
context 'when configured with IgnoreMethods' do
let(:cop_config) { { 'IgnoreMethods' => true } }
it 'does not register an offense for the described method' do
expect_no_offenses(<<-RUBY, 'my_class_spec.rb')
describe MyClass, '#look_here_a_method' do; end
RUBY
end
end
context 'when configured with SpecSuffixOnly' do
let(:cop_config) { { 'SpecSuffixOnly' => true } }
it 'does not register an offense for the described class' do
expect_no_offenses(<<-RUBY, 'whatever_spec.rb')
describe MyClass do; end
RUBY
end
it 'registers an offense when _spec.rb suffix is missing' do
expect_offense(<<-RUBY, 'spec/whatever.rb')
describe MyClass do; end
^^^^^^^^^^^^^^^^ Spec path should end with `*_spec.rb`.
RUBY
end
it 'registers an offense when a feature file is missing _spec.rb suffix' do
expect_offense(<<-RUBY, 'spec/my_feature.rb')
feature "my feature" do; end
^^^^^^^^^^^^^^^^^^^^ Spec path should end with `*_spec.rb`.
RUBY
end
it 'registers an offense when the file extension is not .rb' do
expect_offense(<<-RUBY, 'whatever_specxrb')
describe MyClass do; end
^^^^^^^^^^^^^^^^ Spec path should end with `*_spec.rb`.
RUBY
end
end
end