-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
target_ruby_spec.rb
482 lines (397 loc) · 15 KB
/
target_ruby_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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
# frozen_string_literal: true
RSpec.describe RuboCop::TargetRuby, :isolated_environment do
include FileHelper
subject(:target_ruby) { described_class.new(configuration) }
let(:configuration) { RuboCop::Config.new(hash, loaded_path) }
let(:default_version) { described_class::DEFAULT_VERSION }
let(:hash) { {} }
let(:loaded_path) { 'example/.rubocop.yml' }
context 'when TargetRubyVersion is set' do
let(:ruby_version) { 2.5 }
let(:hash) { { 'AllCops' => { 'TargetRubyVersion' => ruby_version } } }
it 'uses TargetRubyVersion' do
expect(target_ruby.version).to eq ruby_version
end
it 'does not read .ruby-version' do
expect(File).not_to receive(:file?).with('.ruby-version')
target_ruby.version
end
it 'does not read Gemfile.lock or gems.locked' do
expect(File).not_to receive(:file?).with('Gemfile')
expect(File).not_to receive(:file?).with('gems.locked')
target_ruby.version
end
end
context 'when TargetRubyVersion is not set' do
context 'when .ruby-version is present' do
before do
dir = configuration.base_dir_for_path_parameters
create_file(File.join(dir, '.ruby-version'), ruby_version)
end
context 'when .ruby-version contains an MRI version' do
let(:ruby_version) { '2.4.10' }
let(:ruby_version_to_f) { 2.4 }
it 'reads it to determine the target ruby version' do
expect(target_ruby.version).to eq ruby_version_to_f
end
end
context 'when the MRI version contains multiple digits' do
let(:ruby_version) { '10.11.0' }
let(:ruby_version_to_f) { 10.11 }
it 'reads it to determine the target ruby version' do
expect(target_ruby.version).to eq ruby_version_to_f
end
end
context 'when .ruby-version contains a version prefixed by "ruby-"' do
let(:ruby_version) { 'ruby-2.4.0' }
let(:ruby_version_to_f) { 2.4 }
it 'correctly determines the target ruby version' do
expect(target_ruby.version).to eq ruby_version_to_f
end
end
context 'when .ruby-version contains a JRuby version' do
let(:ruby_version) { 'jruby-9.1.2.0' }
it 'uses the default target ruby version' do
expect(target_ruby.version).to eq default_version
end
end
context 'when .ruby-version contains a Rbx version' do
let(:ruby_version) { 'rbx-3.42' }
it 'uses the default target ruby version' do
expect(target_ruby.version).to eq default_version
end
end
context 'when .ruby-version contains "system" version' do
let(:ruby_version) { 'system' }
it 'uses the default target ruby version' do
expect(target_ruby.version).to eq default_version
end
end
it 'does not read .tool-versions, Gemfile.lock or gems.locked' do
expect(File).not_to receive(:file?).with('.tool-versions')
expect(File).not_to receive(:file?).with('Gemfile')
expect(File).not_to receive(:file?).with('gems.locked')
target_ruby.version
end
end
context 'when .tool-versions is present' do
before do
dir = configuration.base_dir_for_path_parameters
create_file(File.join(dir, '.tool-versions'), tool_versions)
end
context 'when .tool-versions does not contain a ruby version' do
let(:tool_versions) { ['nodejs 14.9.0'] }
it 'uses the default ruby version' do
expect(target_ruby.version).to eq default_version
end
end
context 'when .tool-versions contains only a ruby version' do
let(:tool_versions) { ['ruby 3.0.0'] }
let(:ruby_version_to_f) { 3.0 }
it 'reads it to determine the target ruby version' do
expect(target_ruby.version).to eq ruby_version_to_f
end
it 'does not read Gemfile.lock, gems.locked' do
expect(File).not_to receive(:file?).with(/Gemfile/)
expect(File).not_to receive(:file?).with(/gems\.locked/)
target_ruby.version
end
end
context 'when .tool-versions contains different runtimes' do
let(:tool_versions) { ['nodejs 14.9.0', 'ruby 3.0.0'] }
let(:ruby_version_to_f) { 3.0 }
it 'reads it to determine the target ruby version' do
expect(target_ruby.version).to eq ruby_version_to_f
end
it 'does not read Gemfile.lock, gems.locked' do
expect(File).not_to receive(:file?).with(/Gemfile/)
expect(File).not_to receive(:file?).with(/gems\.locked/)
target_ruby.version
end
end
end
context 'when .ruby-version is not present' do
['Gemfile.lock', 'gems.locked'].each do |file_name|
context "and #{file_name} exists" do
let(:base_path) { configuration.base_dir_for_path_parameters }
let(:lock_file_path) { File.join(base_path, file_name) }
it "uses MRI Ruby version when it is present in #{file_name}" do
content =
<<-HEREDOC
GEM
remote: https://rubygems.org/
specs:
actionmailer (4.1.0)
actionpack (= 4.1.0)
rails (4.1.0)
actionmailer (= 4.1.0)
actionpack (= 4.1.0)
PLATFORMS
ruby
DEPENDENCIES
ruby-extensions (~> 1.9.0)
RUBY VERSION
ruby 2.0.0p0
BUNDLED WITH
1.16.1
HEREDOC
create_file(lock_file_path, content)
expect(target_ruby.version).to eq 2.0
end
it 'uses MRI Ruby version when it has multiple digits' do
content =
<<-HEREDOC
GEM
remote: https://rubygems.org/
specs:
actionmailer (4.1.0)
actionpack (= 4.1.0)
rails (4.1.0)
actionmailer (= 4.1.0)
actionpack (= 4.1.0)
PLATFORMS
ruby
DEPENDENCIES
ruby-extensions (~> 1.9.0)
RUBY VERSION
ruby 20.10.100p450
BUNDLED WITH
1.16.1
HEREDOC
create_file(lock_file_path, content)
expect(target_ruby.version).to eq 20.10
end
it "uses the default Ruby when Ruby is not in #{file_name}" do
content =
<<-HEREDOC
GEM
remote: https://rubygems.org/
specs:
addressable (2.5.2)
public_suffix (>= 2.0.2, < 4.0)
ast (2.4.0)
bump (0.5.4)
PLATFORMS
ruby
DEPENDENCIES
bump
bundler (~> 1.3)
ruby-extensions (~> 1.9.0)
BUNDLED WITH
1.16.1
HEREDOC
create_file(lock_file_path, content)
expect(target_ruby.version).to eq default_version
end
it "uses the default Ruby when rbx is in #{file_name}" do
content =
<<-HEREDOC
GEM
remote: https://rubygems.org/
specs:
addressable (2.5.2)
public_suffix (>= 2.0.2, < 4.0)
ast (2.4.0)
bump (0.5.4)
PLATFORMS
ruby
DEPENDENCIES
bump
bundler (~> 1.3)
ruby-extensions (~> 1.9.0)
RUBY VERSION
ruby 2.0.0p0 (rbx 3.42)
BUNDLED WITH
1.16.1
HEREDOC
create_file(lock_file_path, content)
expect(target_ruby.version).to eq default_version
end
it "uses the default Ruby when jruby is in #{file_name}" do
content =
<<-HEREDOC
GEM
remote: https://rubygems.org/
specs:
addressable (2.5.2)
public_suffix (>= 2.0.2, < 4.0)
ast (2.4.0)
bump (0.5.4)
PLATFORMS
ruby
DEPENDENCIES
bump
bundler (~> 1.3)
ruby-extensions (~> 1.9.0)
RUBY VERSION
ruby 2.0.0p0 (jruby 9.1.13.0)
BUNDLED WITH
1.16.1
HEREDOC
create_file(lock_file_path, content)
expect(target_ruby.version).to eq default_version
end
end
end
context 'when bundler lock files are not present' do
it 'uses the default target ruby version' do
expect(target_ruby.version).to eq default_version
end
end
context 'gemspec file' do
context 'when file contains `required_ruby_version` as a string' do
let(:base_path) { configuration.base_dir_for_path_parameters }
let(:gemspec_file_path) { File.join(base_path, 'example.gemspec') }
it 'sets target_ruby from inclusive range' do
content =
<<-HEREDOC
Gem::Specification.new do |s|
s.name = 'test'
s.required_ruby_version = '>= 2.7.2'
s.licenses = ['MIT']
end
HEREDOC
create_file(gemspec_file_path, content)
expect(target_ruby.version).to eq 2.7
end
it 'sets target_ruby from exclusive range' do
content =
<<-HEREDOC
Gem::Specification.new do |s|
s.name = 'test'
s.required_ruby_version = '> 2.6.8'
s.licenses = ['MIT']
end
HEREDOC
create_file(gemspec_file_path, content)
expect(target_ruby.version).to eq 2.6
end
it 'sets target_ruby from approximate version' do
content =
<<-HEREDOC
Gem::Specification.new do |s|
s.name = 'test'
s.required_ruby_version = '~> 2.6.0'
s.licenses = ['MIT']
end
HEREDOC
create_file(gemspec_file_path, content)
expect(target_ruby.version).to eq 2.6
end
end
context 'when file contains `required_ruby_version` as a requirement' do
let(:base_path) { configuration.base_dir_for_path_parameters }
let(:gemspec_file_path) { File.join(base_path, 'example.gemspec') }
it 'sets target_ruby from required_ruby_version from inclusive requirement range' do
content =
<<-HEREDOC
Gem::Specification.new do |s|
s.name = 'test'
s.required_ruby_version = Gem::Requirement.new('>= 2.3.1')
s.licenses = ['MIT']
end
HEREDOC
create_file(gemspec_file_path, content)
expect(target_ruby.version).to eq default_version
end
it 'sets first known ruby version that satisfies requirement' do
content =
<<-HEREDOC
Gem::Specification.new do |s|
s.name = 'test'
s.required_ruby_version = Gem::Requirement.new('< 3.0.0')
s.licenses = ['MIT']
end
HEREDOC
create_file(gemspec_file_path, content)
expect(target_ruby.version).to eq default_version
end
end
context 'when file contains `required_ruby_version` as an array' do
let(:base_path) { configuration.base_dir_for_path_parameters }
let(:gemspec_file_path) { File.join(base_path, 'example.gemspec') }
it 'sets target_ruby to the minimal version satisfying the requirements' do
content =
<<-HEREDOC
Gem::Specification.new do |s|
s.name = 'test'
s.required_ruby_version = ['<=2.7.4', '>=2.6.5']
s.licenses = ['MIT']
end
HEREDOC
create_file(gemspec_file_path, content)
expect(target_ruby.version).to eq 2.6
end
it 'sets target_ruby from required_ruby_version with many requirements' do
content =
<<-HEREDOC
Gem::Specification.new do |s|
s.name = 'test'
s.required_ruby_version = ['<=3.0.0', '>2.5.8', '~>2.6.1']
s.licenses = ['MIT']
end
HEREDOC
create_file(gemspec_file_path, content)
expect(target_ruby.version).to eq 2.6
end
end
context 'when file does not contain `required_ruby_version`' do
let(:base_path) { configuration.base_dir_for_path_parameters }
let(:gemspec_file_path) { File.join(base_path, 'example.gemspec') }
it 'sets default target_ruby' do
content =
<<-HEREDOC
Gem::Specification.new do |s|
s.name = 'test'
s.platform = Gem::Platform::RUBY
s.licenses = ['MIT']
s.summary = 'test tool.'
end
HEREDOC
create_file(gemspec_file_path, content)
expect(target_ruby.version).to eq default_version
end
end
end
end
context 'when .ruby-version is in a parent directory' do
before do
dir = configuration.base_dir_for_path_parameters
create_file(File.join(dir, '..', '.ruby-version'), '2.5.8')
end
it 'reads it to determine the target ruby version' do
expect(target_ruby.version).to eq 2.5
end
end
context 'when .ruby-version is not in a parent directory' do
['Gemfile.lock', 'gems.locked'].each do |file_name|
context "when #{file_name} is in a parent directory" do
it 'does' do
content =
<<-HEREDOC
GEM
remote: https://rubygems.org/
specs:
actionmailer (4.1.0)
actionpack (= 4.1.0)
rails (4.1.0)
actionmailer (= 4.1.0)
actionpack (= 4.1.0)
PLATFORMS
ruby
DEPENDENCIES
ruby-extensions (~> 1.9.0)
RUBY VERSION
ruby 2.0.0p0
BUNDLED WITH
1.16.1
HEREDOC
dir = configuration.base_dir_for_path_parameters
create_file(File.join(dir, '..', file_name), content)
expect(target_ruby.version).to eq 2.0
end
end
end
end
end
end