-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
ordered_gems_spec.rb
262 lines (230 loc) · 6.47 KB
/
ordered_gems_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Bundler::OrderedGems, :config do
let(:cop_config) do
{
'TreatCommentsAsGroupSeparators' => treat_comments_as_group_separators,
'Include' => nil
}
end
let(:treat_comments_as_group_separators) { false }
context 'When gems are alphabetically sorted' do
it 'does not register any offenses' do
expect_no_offenses(<<~RUBY)
gem 'rspec'
gem 'rubocop'
RUBY
end
end
context 'when a gem is referenced from a variable' do
it 'ignores the line' do
expect_no_offenses(<<~RUBY)
gem 'rspec'
gem ENV['env_key_undefined'] if ENV.key?('env_key_undefined')
gem 'rubocop'
RUBY
end
it 'resets the sorting to a new block' do
expect_no_offenses(<<~RUBY)
gem 'rubocop'
gem ENV['env_key_undefined'] if ENV.key?('env_key_undefined')
gem 'ast'
RUBY
end
end
context 'When gems are not alphabetically sorted' do
it 'registers an offense' do
expect_offense(<<~RUBY)
gem 'rubocop'
gem 'rspec'
^^^^^^^^^^^ Gems should be sorted in an alphabetical order within their section of the Gemfile. Gem `rspec` should appear before `rubocop`.
RUBY
expect_correction(<<~RUBY)
gem 'rspec'
gem 'rubocop'
RUBY
end
end
context 'When each individual group of line is sorted' do
it 'does not register any offenses' do
expect_no_offenses(<<~RUBY)
gem 'rspec'
gem 'rubocop'
gem 'hello'
gem 'world'
RUBY
end
end
context 'When a gem declaration takes several lines' do
it 'registers an offense' do
expect_offense(<<~RUBY)
gem 'rubocop',
'0.1.1'
gem 'rspec'
^^^^^^^^^^^ Gems should be sorted in an alphabetical order within their section of the Gemfile. Gem `rspec` should appear before `rubocop`.
RUBY
expect_correction(<<~RUBY)
gem 'rspec'
gem 'rubocop',
'0.1.1'
RUBY
end
end
context 'When the gemfile is empty' do
it 'does not register any offenses' do
expect_no_offenses('# Gemfile')
end
end
context 'When each individual group of line is not sorted' do
let(:source) { <<~RUBY }
gem "d"
gem "b"
gem "e"
gem "a"
gem "c"
gem "h"
gem "g"
gem "j"
gem "f"
gem "i"
RUBY
it 'registers some offenses' do
expect_offense(<<~RUBY)
gem "d"
gem "b"
^^^^^^^ Gems should be sorted in an alphabetical order within their section of the Gemfile. Gem `b` should appear before `d`.
gem "e"
gem "a"
^^^^^^^ Gems should be sorted in an alphabetical order within their section of the Gemfile. Gem `a` should appear before `e`.
gem "c"
gem "h"
gem "g"
^^^^^^^ Gems should be sorted in an alphabetical order within their section of the Gemfile. Gem `g` should appear before `h`.
gem "j"
gem "f"
^^^^^^^ Gems should be sorted in an alphabetical order within their section of the Gemfile. Gem `f` should appear before `j`.
gem "i"
RUBY
end
it 'autocorrects' do
new_source = autocorrect_source_with_loop(source)
expect(new_source).to eq(<<~RUBY)
gem "a"
gem "b"
gem "c"
gem "d"
gem "e"
gem "f"
gem "g"
gem "h"
gem "i"
gem "j"
RUBY
end
end
context 'When gem groups is separated by multiline comment' do
context 'with TreatCommentsAsGroupSeparators: true' do
let(:treat_comments_as_group_separators) { true }
it 'accepts' do
expect_no_offenses(<<~RUBY)
# For code quality
gem 'rubocop'
# For
# test
gem 'rspec'
RUBY
end
end
context 'with TreatCommentsAsGroupSeparators: false' do
it 'registers an offense' do
expect_offense(<<~RUBY)
# For code quality
gem 'rubocop'
# For
# test
gem 'rspec'
^^^^^^^^^^^ Gems should be sorted in an alphabetical order within their section of the Gemfile. Gem `rspec` should appear before `rubocop`.
RUBY
expect_correction(<<~RUBY)
# For
# test
gem 'rspec'
# For code quality
gem 'rubocop'
RUBY
end
end
end
context 'When gems have an inline comment, and not sorted' do
let(:source) { <<~RUBY }
gem 'rubocop' # For code quality
gem 'pry'
gem 'rspec' # For test
RUBY
it 'registers an offense' do
expect_offense(<<~RUBY)
gem 'rubocop' # For code quality
gem 'pry'
^^^^^^^^^ Gems should be sorted in an alphabetical order within their section of the Gemfile. Gem `pry` should appear before `rubocop`.
gem 'rspec' # For test
RUBY
end
it 'autocorrects' do
new_source = autocorrect_source_with_loop(source)
expect(new_source).to eq(<<~RUBY)
gem 'pry'
gem 'rspec' # For test
gem 'rubocop' # For code quality
RUBY
end
end
context 'When gems are asciibetically sorted' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
gem 'paper_trail'
gem 'paperclip'
RUBY
end
end
context 'When a gem that starts with a capital letter is sorted' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
gem 'a'
gem 'Z'
RUBY
end
end
context 'When a gem that starts with a capital letter is not sorted' do
it 'registers an offense' do
expect_offense(<<~RUBY)
gem 'Z'
gem 'a'
^^^^^^^ Gems should be sorted in an alphabetical order within their section of the Gemfile. Gem `a` should appear before `Z`.
RUBY
expect_correction(<<~RUBY)
gem 'a'
gem 'Z'
RUBY
end
end
context 'When there are duplicated gems in group' do
it 'registers an offense' do
expect_offense(<<~RUBY)
gem 'a'
group :development do
gem 'b'
gem 'c'
gem 'b'
^^^^^^^ Gems should be sorted in an alphabetical order within their section of the Gemfile. Gem `b` should appear before `c`.
end
RUBY
expect_correction(<<~RUBY)
gem 'a'
group :development do
gem 'b'
gem 'b'
gem 'c'
end
RUBY
end
end
end