/
generator_spec.rb
351 lines (286 loc) · 9.67 KB
/
generator_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Generator do
subject(:generator) do
described_class.new(cop_identifier, 'your_id', output: stdout)
end
let(:stdout) { StringIO.new }
let(:cop_identifier) { 'Style/FakeCop' }
before do
allow(File).to receive(:write)
stub_const('HOME_DIR', Dir.pwd)
end
describe '#write_source' do
include_context 'cli spec behavior'
it 'generates a helpful source file with the name filled in' do
generated_source = <<~RUBY
# frozen_string_literal: true
# TODO: when finished, run `rake generate_cops_documentation` to update the docs
module RuboCop
module Cop
module Style
# TODO: Write cop description and example of bad / good code. For every
# `SupportedStyle` and unique configuration, there needs to be examples.
# Examples must have valid Ruby syntax. Do not use upticks.
#
# @example EnforcedStyle: bar (default)
# # Description of the `bar` style.
#
# # bad
# bad_bar_method
#
# # bad
# bad_bar_method(args)
#
# # good
# good_bar_method
#
# # good
# good_bar_method(args)
#
# @example EnforcedStyle: foo
# # Description of the `foo` style.
#
# # bad
# bad_foo_method
#
# # bad
# bad_foo_method(args)
#
# # good
# good_foo_method
#
# # good
# good_foo_method(args)
#
class FakeCop < Base
# TODO: Implement the cop in here.
#
# In many cases, you can use a node matcher for matching node pattern.
# See https://github.com/rubocop-hq/rubocop-ast/blob/master/lib/rubocop/ast/node_pattern.rb
#
# For example
MSG = 'Use `#good_method` instead of `#bad_method`.'
def_node_matcher :bad_method?, <<~PATTERN
(send nil? :bad_method ...)
PATTERN
def on_send(node)
return unless bad_method?(node)
add_offense(node)
end
end
end
end
end
RUBY
expect(File)
.to receive(:write)
.with('lib/rubocop/cop/style/fake_cop.rb', generated_source)
generator.write_source
expect(stdout.string)
.to eq("[create] lib/rubocop/cop/style/fake_cop.rb\n")
end
it 'refuses to overwrite existing files' do
new_cop = described_class.new('Layout/IndentationStyle', 'your_id')
allow(new_cop).to receive(:exit!)
expect { new_cop.write_source }
.to output(
'rake new_cop: lib/rubocop/cop/layout/indentation_style.rb '\
"already exists!\n"
).to_stderr
end
end
describe '#write_spec' do
include_context 'cli spec behavior'
it 'generates a helpful starting spec file with the class filled in' do
generated_source = <<~SPEC
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Style::FakeCop do
subject(:cop) { described_class.new(config) }
let(:config) { RuboCop::Config.new }
# TODO: Write test code
#
# For example
it 'registers an offense when using `#bad_method`' do
expect_offense(<<~RUBY)
bad_method
^^^^^^^^^^ Use `#good_method` instead of `#bad_method`.
RUBY
end
it 'does not register an offense when using `#good_method`' do
expect_no_offenses(<<~RUBY)
good_method
RUBY
end
end
SPEC
expect(File)
.to receive(:write)
.with('spec/rubocop/cop/style/fake_cop_spec.rb', generated_source)
generator.write_spec
end
it 'refuses to overwrite existing files' do
new_cop = described_class.new('Layout/IndentationStyle', 'your_id')
allow(new_cop).to receive(:exit!)
expect { new_cop.write_spec }
.to output(
'rake new_cop: spec/rubocop/cop/layout/indentation_style_spec.rb '\
"already exists!\n"
).to_stderr
end
end
describe '#todo' do
it 'provides a checklist for implementing the cop' do
expect(generator.todo).to eql(<<~TODO)
Do 3 steps:
1. Add an entry to the "New features" section in CHANGELOG.md,
e.g. "Add new `Style/FakeCop` cop. ([@your_id][])"
2. Modify the description of Style/FakeCop in config/default.yml
3. Implement your new cop in the generated file!
TODO
end
end
describe '.new' do
it 'does not accept an unqualified cop' do
expect { described_class.new('FakeCop', 'your_id') }
.to raise_error(ArgumentError)
.with_message('Specify a cop name with Department/Name style')
end
end
describe '#inject_config' do
let(:path) { @path } # rubocop:disable RSpec/InstanceVariable
around do |example|
Tempfile.create('rubocop-config.yml') do |file|
@path = file.path
example.run
end
end
before do
IO.write(path, <<~YAML)
Style/Alias:
Enabled: true
Style/Lambda:
Enabled: true
Style/SpecialGlobalVars:
Enabled: true
YAML
stub_const('RuboCop::Version::STRING', '0.58.2')
end
context 'when it is the middle in alphabetical order' do
it 'inserts the cop' do
expect(File).to receive(:write).with(path, <<~YAML)
Style/Alias:
Enabled: true
Style/FakeCop:
Description: 'TODO: Write a description of the cop.'
Enabled: pending
VersionAdded: '0.59'
Style/Lambda:
Enabled: true
Style/SpecialGlobalVars:
Enabled: true
YAML
generator.inject_config(config_file_path: path)
expect(stdout.string)
.to eq('[modify] A configuration for the cop is added into ' \
"#{path}.\n")
end
end
context 'when it is the first in alphabetical order' do
let(:cop_identifier) { 'Style/Aaa' }
it 'inserts the cop' do
expect(File).to receive(:write).with(path, <<~YAML)
Style/Aaa:
Description: 'TODO: Write a description of the cop.'
Enabled: pending
VersionAdded: '0.59'
Style/Alias:
Enabled: true
Style/Lambda:
Enabled: true
Style/SpecialGlobalVars:
Enabled: true
YAML
generator.inject_config(config_file_path: path)
expect(stdout.string)
.to eq('[modify] A configuration for the cop is added into ' \
"#{path}.\n")
end
end
context 'when it is the last in alphabetical order' do
let(:cop_identifier) { 'Style/Zzz' }
it 'inserts the cop' do
expect(File).to receive(:write).with(path, <<~YAML)
Style/Alias:
Enabled: true
Style/Lambda:
Enabled: true
Style/SpecialGlobalVars:
Enabled: true
Style/Zzz:
Description: 'TODO: Write a description of the cop.'
Enabled: pending
VersionAdded: '0.59'
YAML
generator.inject_config(config_file_path: path)
expect(stdout.string)
.to eq('[modify] A configuration for the cop is added into ' \
"#{path}.\n")
end
end
context 'with version provided' do
it 'uses the provided version' do
expect(File).to receive(:write).with(path, <<~YAML)
Style/Alias:
Enabled: true
Style/FakeCop:
Description: 'TODO: Write a description of the cop.'
Enabled: pending
VersionAdded: '1.52'
Style/Lambda:
Enabled: true
Style/SpecialGlobalVars:
Enabled: true
YAML
generator.inject_config(config_file_path: path, version_added: '1.52')
end
end
end
describe '#snake_case' do
it 'converts "Lint" to snake_case' do
expect(generator.__send__(:snake_case, 'Lint')).to eq('lint')
end
it 'converts "FooBar" to snake_case' do
expect(generator.__send__(:snake_case, 'FooBar')).to eq('foo_bar')
end
it 'converts "RSpec" to snake_case' do
expect(generator.__send__(:snake_case, 'RSpec')).to eq('rspec')
end
end
describe 'compliance with rubocop', :isolated_environment do
include FileHelper
around do |example|
new_global = RuboCop::Cop::Registry.new(
[RuboCop::Cop::InternalAffairs::NodeDestructuring]
)
RuboCop::Cop::Registry.with_temporary_global(new_global) { example.run }
end
let(:config) do
config = RuboCop::ConfigStore.new
path = File.join(RuboCop::ConfigLoader::RUBOCOP_HOME,
RuboCop::ConfigLoader::DOTFILE)
config.options_config = path
config
end
let(:options) { { formatters: [] } }
let(:runner) { RuboCop::Runner.new(options, config) }
it 'generates a cop file that has no offense' do
generator.write_source
expect(runner.run([])).to be true
end
# TODO: include it back after rubocop-rspec updated to support deep departments names
xit 'generates a spec file that has no offense' do
generator.write_spec
expect(runner.run([])).to be true
end
end
end