/
required_ruby_version_spec.rb
131 lines (118 loc) · 5.38 KB
/
required_ruby_version_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Gemspec::RequiredRubyVersion, :config do
context 'target ruby version > 2.7', :ruby27 do
it 'registers an offense when `required_ruby_version` is specified with >= and is lower than `TargetRubyVersion`' do
expect_offense(<<~RUBY, '/path/to/foo.gemspec')
Gem::Specification.new do |spec|
spec.required_ruby_version = '>= 2.6.0'
^^^^^^^^^^ `required_ruby_version` (2.6, declared in foo.gemspec) and `TargetRubyVersion` (2.7, which may be specified in .rubocop.yml) should be equal.
end
RUBY
end
it 'registers an offense when `required_ruby_version` is specified with ~> and is lower than `TargetRubyVersion`' do
expect_offense(<<~RUBY, '/path/to/foo.gemspec')
Gem::Specification.new do |spec|
spec.required_ruby_version = '~> 2.6.0'
^^^^^^^^^^ `required_ruby_version` (2.6, declared in foo.gemspec) and `TargetRubyVersion` (2.7, which may be specified in .rubocop.yml) should be equal.
end
RUBY
end
describe 'false negatives' do
it 'does not register an offense when `required_ruby_version` ' \
'is assigned as a variable (string literal)' do
expect_no_offenses(<<~RUBY)
Gem::Specification.new do |spec|
version = '>= 2.6.0'
spec.required_ruby_version = version
end
RUBY
end
it 'does not register an offense when `required_ruby_version` ' \
'is assigned as a variable (an array of string literal)' do
expect_no_offenses(<<~RUBY)
Gem::Specification.new do |spec|
lowest_version = '>= 2.6.0'
highest_version = '< 2.8.0'
spec.required_ruby_version = [lowest_version, highest_version]
end
RUBY
end
end
end
context 'target ruby version > 2.5', :ruby25 do
it 'registers an offense when `required_ruby_version` is specified with >= and is higher than `TargetRubyVersion`' do
expect_offense(<<~RUBY, '/path/to/bar.gemspec')
Gem::Specification.new do |spec|
spec.required_ruby_version = '>= 2.6.0'
^^^^^^^^^^ `required_ruby_version` (2.6, declared in bar.gemspec) and `TargetRubyVersion` (2.5, which may be specified in .rubocop.yml) should be equal.
end
RUBY
end
it 'registers an offense when `required_ruby_version` is specified with ~> and is higher than `TargetRubyVersion`' do
expect_offense(<<~RUBY, '/path/to/bar.gemspec')
Gem::Specification.new do |spec|
spec.required_ruby_version = '~> 2.6.0'
^^^^^^^^^^ `required_ruby_version` (2.6, declared in bar.gemspec) and `TargetRubyVersion` (2.5, which may be specified in .rubocop.yml) should be equal.
end
RUBY
end
end
context 'target ruby version > 2.6', :ruby26 do
it 'does not register an offense when `required_ruby_version` is specified with >= and equals `TargetRubyVersion`' do
expect_no_offenses(<<~RUBY)
Gem::Specification.new do |spec|
spec.required_ruby_version = '>= 2.6.0'
end
RUBY
end
it 'does not register an offense when `required_ruby_version` is specified with ~> and equals `TargetRubyVersion`' do
expect_no_offenses(<<~RUBY)
Gem::Specification.new do |spec|
spec.required_ruby_version = '~> 2.6.0'
end
RUBY
end
it 'does not register an offense when `required_ruby_version` is specified with >= without a patch version and ' \
'equals `TargetRubyVersion`' do
expect_no_offenses(<<~RUBY)
Gem::Specification.new do |spec|
spec.required_ruby_version = '>= 2.6'
end
RUBY
end
it 'does not register an offense when `required_ruby_version` is specified with ~> without a patch version and ' \
'equals `TargetRubyVersion`' do
expect_no_offenses(<<~RUBY)
Gem::Specification.new do |spec|
spec.required_ruby_version = '~> 2.6'
end
RUBY
end
it 'does not register an offense when lowest version of ' \
'`required_ruby_version` equals `TargetRubyVersion`' do
expect_no_offenses(<<~RUBY)
Gem::Specification.new do |spec|
spec.required_ruby_version = ['>= 2.6.0', '< 2.8.0']
end
RUBY
end
it 'registers an offense when `required_ruby_version` is specified with >= without a minor version and is lower ' \
'than `TargetRubyVersion`' do
expect_offense(<<~RUBY, 'bar.gemspec')
Gem::Specification.new do |spec|
spec.required_ruby_version = '>= 2'
^^^^^^ `required_ruby_version` (2, declared in bar.gemspec) and `TargetRubyVersion` (2.6, which may be specified in .rubocop.yml) should be equal.
end
RUBY
end
it 'registers an offense when `required_ruby_version` is specified with ~> without a minor version and is lower ' \
'than `TargetRubyVersion`' do
expect_offense(<<~RUBY, 'bar.gemspec')
Gem::Specification.new do |spec|
spec.required_ruby_version = '~> 2'
^^^^^^ `required_ruby_version` (2, declared in bar.gemspec) and `TargetRubyVersion` (2.6, which may be specified in .rubocop.yml) should be equal.
end
RUBY
end
end
end