rubocop-yast 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,65 +1,567 @@
1
- # encoding: utf-8
1
+ # Automatically generated -- DO NOT EDIT!
2
2
 
3
3
  require "spec_helper"
4
4
 
5
- def expect_y2milestone_offense(cop)
6
- expect(cop.offenses.size).to eq(1)
7
- expect(cop.offenses.first.line).to eq(1)
8
- expect(cop.messages).to eq(["Builtin call `y2milestone` is obsolete, " \
9
- "use native Ruby function instead."])
10
- end
5
+ describe "RuboCop-Yast" do
6
+ describe "Generic Tests:" do
7
+ it "It reports y2milestone builtin as offense" do
8
+ code = code_cleanup(<<-EOT)
9
+ Builtins.y2milestone("foo")
10
+ EOT
11
11
 
12
- describe RuboCop::Cop::Yast::Builtins do
13
- subject(:cop) { described_class.new }
12
+ cop = RuboCop::Cop::Yast::Builtins.new
13
+ inspect_source(cop, [code])
14
14
 
15
- it "reports Builtins.* calls" do
16
- inspect_source(cop, ['Builtins.y2milestone("foo")'])
15
+ expect(cop.offenses.size).to eq(1)
16
+ expect(cop.messages.first).to match(/Builtin call `.*` is obsolete/)
17
+ end
17
18
 
18
- expect_y2milestone_offense(cop)
19
- end
19
+ it "It finds builtin in explicit Yast namespace" do
20
+ code = code_cleanup(<<-EOT)
21
+ Yast::Builtins.y2milestone("foo")
22
+ EOT
20
23
 
21
- it "reports Yast::Builtins.* calls" do
22
- inspect_source(cop, ['Yast::Builtins.y2milestone("foo")'])
24
+ cop = RuboCop::Cop::Yast::Builtins.new
25
+ inspect_source(cop, [code])
23
26
 
24
- expect_y2milestone_offense(cop)
25
- end
27
+ expect(cop.offenses.size).to eq(1)
28
+ expect(cop.messages.first).to match(/Builtin call `.*` is obsolete/)
29
+ end
26
30
 
27
- it "reports ::Yast::Builtins.* calls" do
28
- inspect_source(cop, ['::Yast::Builtins.y2milestone("foo")'])
31
+ it "It finds builtin in explicit ::Yast namespace" do
32
+ code = code_cleanup(<<-EOT)
33
+ ::Yast::Builtins.y2milestone("foo")
34
+ EOT
29
35
 
30
- expect_y2milestone_offense(cop)
31
- end
36
+ cop = RuboCop::Cop::Yast::Builtins.new
37
+ inspect_source(cop, [code])
32
38
 
33
- it "ignores lsort builtin" do
34
- inspect_source(cop, ['Builtins.lsort(["foo"])'])
39
+ expect(cop.offenses.size).to eq(1)
40
+ expect(cop.messages.first).to match(/Builtin call `.*` is obsolete/)
41
+ end
35
42
 
36
- expect(cop.offenses).to be_empty
37
- end
43
+ it "Builtins in the ::Builtins name space are ignored" do
44
+ code = code_cleanup(<<-EOT)
45
+ ::Builtins.y2milestone("foo")
46
+ EOT
38
47
 
39
- it "ignores ::Builtins calls" do
40
- inspect_source(cop, ['::Builtins.y2milestone("foo")'])
48
+ cop = RuboCop::Cop::Yast::Builtins.new
49
+ inspect_source(cop, [code])
41
50
 
42
- expect(cop.offenses).to be_empty
43
- end
51
+ expect(cop.offenses).to be_empty
52
+ end
53
+
54
+ it "Builtins in non Yast name space are ignored" do
55
+ code = code_cleanup(<<-EOT)
56
+ Foo::Builtins.y2milestone("foo")
57
+ EOT
44
58
 
45
- it "ignores Foo::Builtins calls" do
46
- inspect_source(cop, ['Foo::Builtins.y2milestone("foo")'])
59
+ cop = RuboCop::Cop::Yast::Builtins.new
60
+ inspect_source(cop, [code])
47
61
 
48
- expect(cop.offenses).to be_empty
62
+ expect(cop.offenses).to be_empty
63
+ end
64
+
65
+ it "lsort(), crypt and gettext builtins are allowed" do
66
+ code = code_cleanup(<<-EOT)
67
+ Builtins.lsort(["foo"])
68
+ Builtins.crypt("foo")
69
+ Builtins.dgettext("domain", "foo")
70
+ EOT
71
+
72
+ cop = RuboCop::Cop::Yast::Builtins.new
73
+ inspect_source(cop, [code])
74
+
75
+ expect(cop.offenses).to be_empty
76
+ end
77
+
78
+ it "It does not change unknown builtins" do
79
+ original_code = code_cleanup(<<-EOT)
80
+ Builtins.foo()
81
+ EOT
82
+
83
+ translated_code = code_cleanup(<<-EOT)
84
+ Builtins.foo()
85
+ EOT
86
+
87
+ cop = RuboCop::Cop::Yast::Builtins.new
88
+ expect(autocorrect_source(cop, original_code)).to eq(translated_code)
89
+ end
49
90
  end
50
91
 
51
- it "auto-corrects Builtins.time with ::Time.now.to_i" do
52
- new_source = autocorrect_source(cop, "Builtins.time")
53
- expect(new_source).to eq("::Time.now.to_i")
92
+ describe "Builtins.time():" do
93
+ it "Is trivially converted to `::Time.now.to_i`" do
94
+ original_code = code_cleanup(<<-EOT)
95
+ Builtins.time()
96
+ EOT
97
+
98
+ translated_code = code_cleanup(<<-EOT)
99
+ ::Time.now.to_i
100
+ EOT
101
+
102
+ cop = RuboCop::Cop::Yast::Builtins.new
103
+ expect(autocorrect_source(cop, original_code)).to eq(translated_code)
104
+ end
54
105
  end
55
106
 
56
- it 'auto-corrects Builtins.getenv("foo") with ENV["foo"]' do
57
- new_source = autocorrect_source(cop, 'Builtins.getenv("foo")')
58
- expect(new_source).to eq('ENV["foo"]')
107
+ describe "Builtins.getenv():" do
108
+ it "With string literal parameter is translated to ENV equivalent" do
109
+ original_code = code_cleanup(<<-EOT)
110
+ Builtins.getenv("foo")
111
+ EOT
112
+
113
+ translated_code = code_cleanup(<<-EOT)
114
+ ENV["foo"]
115
+ EOT
116
+
117
+ cop = RuboCop::Cop::Yast::Builtins.new
118
+ expect(autocorrect_source(cop, original_code)).to eq(translated_code)
119
+ end
120
+
121
+ it "Variable as parameter is preserved" do
122
+ original_code = code_cleanup(<<-EOT)
123
+ foo = bar
124
+ Builtins.getenv(foo)
125
+ EOT
126
+
127
+ translated_code = code_cleanup(<<-EOT)
128
+ foo = bar
129
+ ENV[foo]
130
+ EOT
131
+
132
+ cop = RuboCop::Cop::Yast::Builtins.new
133
+ expect(autocorrect_source(cop, original_code)).to eq(translated_code)
134
+ end
135
+
136
+ it "Any other statement is preserved" do
137
+ original_code = code_cleanup(<<-EOT)
138
+ Builtins.getenv(Ops.add(foo, bar))
139
+ EOT
140
+
141
+ translated_code = code_cleanup(<<-EOT)
142
+ ENV[Ops.add(foo, bar)]
143
+ EOT
144
+
145
+ cop = RuboCop::Cop::Yast::Builtins.new
146
+ expect(autocorrect_source(cop, original_code)).to eq(translated_code)
147
+ end
59
148
  end
60
149
 
61
- it "auto-corrects Builtins.getenv(foo) with ENV[foo]" do
62
- new_source = autocorrect_source(cop, "Builtins.getenv(foo)")
63
- expect(new_source).to eq("ENV[foo]")
150
+ describe "Logging:" do
151
+ it "It translates `y2debug` to `log.debug`" do
152
+ original_code = code_cleanup(<<-EOT)
153
+ Builtins.y2debug("foo")
154
+ EOT
155
+
156
+ translated_code = code_cleanup(<<-EOT)
157
+ include Yast::Logger
158
+ log.debug "foo"
159
+ EOT
160
+
161
+ cop = RuboCop::Cop::Yast::Builtins.new
162
+ expect(autocorrect_source(cop, original_code)).to eq(translated_code)
163
+ end
164
+
165
+ it "It translates `y2milestone` to `log.info`" do
166
+ original_code = code_cleanup(<<-EOT)
167
+ Builtins.y2milestone("foo")
168
+ EOT
169
+
170
+ translated_code = code_cleanup(<<-EOT)
171
+ include Yast::Logger
172
+ log.info "foo"
173
+ EOT
174
+
175
+ cop = RuboCop::Cop::Yast::Builtins.new
176
+ expect(autocorrect_source(cop, original_code)).to eq(translated_code)
177
+ end
178
+
179
+ it "It translates `y2warning` to `log.warn`" do
180
+ original_code = code_cleanup(<<-EOT)
181
+ Builtins.y2warning("foo")
182
+ EOT
183
+
184
+ translated_code = code_cleanup(<<-EOT)
185
+ include Yast::Logger
186
+ log.warn "foo"
187
+ EOT
188
+
189
+ cop = RuboCop::Cop::Yast::Builtins.new
190
+ expect(autocorrect_source(cop, original_code)).to eq(translated_code)
191
+ end
192
+
193
+ it "It translates `y2error` to `log.error`" do
194
+ original_code = code_cleanup(<<-EOT)
195
+ Builtins.y2error("foo")
196
+ EOT
197
+
198
+ translated_code = code_cleanup(<<-EOT)
199
+ include Yast::Logger
200
+ log.error "foo"
201
+ EOT
202
+
203
+ cop = RuboCop::Cop::Yast::Builtins.new
204
+ expect(autocorrect_source(cop, original_code)).to eq(translated_code)
205
+ end
206
+
207
+ it "It translates `y2security` to `log.error`" do
208
+ original_code = code_cleanup(<<-EOT)
209
+ Builtins.y2security("foo")
210
+ EOT
211
+
212
+ translated_code = code_cleanup(<<-EOT)
213
+ include Yast::Logger
214
+ log.error "foo"
215
+ EOT
216
+
217
+ cop = RuboCop::Cop::Yast::Builtins.new
218
+ expect(autocorrect_source(cop, original_code)).to eq(translated_code)
219
+ end
220
+
221
+ it "It translates `y2internal` to `log.fatal`" do
222
+ original_code = code_cleanup(<<-EOT)
223
+ Builtins.y2internal("foo")
224
+ EOT
225
+
226
+ translated_code = code_cleanup(<<-EOT)
227
+ include Yast::Logger
228
+ log.fatal "foo"
229
+ EOT
230
+
231
+ cop = RuboCop::Cop::Yast::Builtins.new
232
+ expect(autocorrect_source(cop, original_code)).to eq(translated_code)
233
+ end
234
+
235
+ it "It adds the include statement only once" do
236
+ original_code = code_cleanup(<<-EOT)
237
+ Builtins.y2milestone("foo")
238
+ Builtins.y2milestone("foo")
239
+ EOT
240
+
241
+ translated_code = code_cleanup(<<-EOT)
242
+ include Yast::Logger
243
+ log.info \"foo\"
244
+ log.info \"foo\"
245
+ EOT
246
+
247
+ cop = RuboCop::Cop::Yast::Builtins.new
248
+ expect(autocorrect_source(cop, original_code)).to eq(translated_code)
249
+ end
250
+
251
+ it "It converts YCP sformat to Ruby interpolation" do
252
+ original_code = code_cleanup(<<-EOT)
253
+ Builtins.y2milestone("foo: %1", foo)
254
+ EOT
255
+
256
+ translated_code = code_cleanup(<<-EOT)
257
+ include Yast::Logger
258
+ log.info "foo: \#{foo}"
259
+ EOT
260
+
261
+ cop = RuboCop::Cop::Yast::Builtins.new
262
+ expect(autocorrect_source(cop, original_code)).to eq(translated_code)
263
+ end
264
+
265
+ it "It converts %% in the format string to simple %" do
266
+ original_code = code_cleanup(<<-EOT)
267
+ Builtins.y2milestone("foo: %1%%", foo)
268
+ EOT
269
+
270
+ translated_code = code_cleanup(<<-EOT)
271
+ include Yast::Logger
272
+ log.info "foo: \#{foo}%"
273
+ EOT
274
+
275
+ cop = RuboCop::Cop::Yast::Builtins.new
276
+ expect(autocorrect_source(cop, original_code)).to eq(translated_code)
277
+ end
278
+
279
+ it "It replaces repeated % placeholders in the format string" do
280
+ original_code = code_cleanup(<<-EOT)
281
+ Builtins.y2warning("%1 %1", foo)
282
+ EOT
283
+
284
+ translated_code = code_cleanup(<<-EOT)
285
+ include Yast::Logger
286
+ log.warn "\#{foo} \#{foo}"
287
+ EOT
288
+
289
+ cop = RuboCop::Cop::Yast::Builtins.new
290
+ expect(autocorrect_source(cop, original_code)).to eq(translated_code)
291
+ end
292
+
293
+ it "The % placeholders do not need to start from 1" do
294
+ original_code = code_cleanup(<<-EOT)
295
+ Builtins.y2warning("%2 %2", foo, bar)
296
+ EOT
297
+
298
+ translated_code = code_cleanup(<<-EOT)
299
+ include Yast::Logger
300
+ log.warn "\#{bar} \#{bar}"
301
+ EOT
302
+
303
+ cop = RuboCop::Cop::Yast::Builtins.new
304
+ expect(autocorrect_source(cop, original_code)).to eq(translated_code)
305
+ end
306
+
307
+ it "The % placeholders do not need to be in ascending order" do
308
+ original_code = code_cleanup(<<-EOT)
309
+ Builtins.y2warning("%2 %1", foo, bar)
310
+ EOT
311
+
312
+ translated_code = code_cleanup(<<-EOT)
313
+ include Yast::Logger
314
+ log.warn "\#{bar} \#{foo}"
315
+ EOT
316
+
317
+ cop = RuboCop::Cop::Yast::Builtins.new
318
+ expect(autocorrect_source(cop, original_code)).to eq(translated_code)
319
+ end
320
+
321
+ it "It keeps the original statements in interpolated string" do
322
+ original_code = code_cleanup(<<-EOT)
323
+ Builtins.y2warning("%1", foo + bar)
324
+ EOT
325
+
326
+ translated_code = code_cleanup(<<-EOT)
327
+ include Yast::Logger
328
+ log.warn "\#{foo + bar}"
329
+ EOT
330
+
331
+ cop = RuboCop::Cop::Yast::Builtins.new
332
+ expect(autocorrect_source(cop, original_code)).to eq(translated_code)
333
+ end
334
+
335
+ it "It converts a log with string interpolation" do
336
+ original_code = code_cleanup(<<-EOT)
337
+ Builtins.y2warning("foo: \#{foo}")
338
+ EOT
339
+
340
+ translated_code = code_cleanup(<<-EOT)
341
+ include Yast::Logger
342
+ log.warn "foo: \#{foo}"
343
+ EOT
344
+
345
+ cop = RuboCop::Cop::Yast::Builtins.new
346
+ expect(autocorrect_source(cop, original_code)).to eq(translated_code)
347
+ end
348
+
349
+ it "It converts a log with a message variable" do
350
+ original_code = code_cleanup(<<-EOT)
351
+ msg = "message"
352
+ Builtins.y2warning(msg)
353
+ EOT
354
+
355
+ translated_code = code_cleanup(<<-EOT)
356
+ include Yast::Logger
357
+ msg = "message"
358
+ log.warn msg
359
+ EOT
360
+
361
+ cop = RuboCop::Cop::Yast::Builtins.new
362
+ expect(autocorrect_source(cop, original_code)).to eq(translated_code)
363
+ end
364
+
365
+ it "It converts a log with function call" do
366
+ original_code = code_cleanup(<<-EOT)
367
+ Builtins.y2warning(msg)
368
+ EOT
369
+
370
+ translated_code = code_cleanup(<<-EOT)
371
+ include Yast::Logger
372
+ log.warn msg
373
+ EOT
374
+
375
+ cop = RuboCop::Cop::Yast::Builtins.new
376
+ expect(autocorrect_source(cop, original_code)).to eq(translated_code)
377
+ end
378
+
379
+ it "It doesn't convert a log with extra parameters" do
380
+ original_code = code_cleanup(<<-EOT)
381
+ Builtins.y2warning(msg, arg1, arg2)
382
+ EOT
383
+
384
+ translated_code = code_cleanup(<<-EOT)
385
+ Builtins.y2warning(msg, arg1, arg2)
386
+ EOT
387
+
388
+ cop = RuboCop::Cop::Yast::Builtins.new
389
+ expect(autocorrect_source(cop, original_code)).to eq(translated_code)
390
+ end
391
+
392
+ it "It converts log with operator call" do
393
+ original_code = code_cleanup(<<-EOT)
394
+ Builtins.y2warning(msg1 + msg2)
395
+ EOT
396
+
397
+ translated_code = code_cleanup(<<-EOT)
398
+ include Yast::Logger
399
+ log.warn msg1 + msg2
400
+ EOT
401
+
402
+ cop = RuboCop::Cop::Yast::Builtins.new
403
+ expect(autocorrect_source(cop, original_code)).to eq(translated_code)
404
+ end
405
+
406
+ it "It adds logger include to the class definition" do
407
+ original_code = code_cleanup(<<-EOT)
408
+ class Foo
409
+ Builtins.y2error('foo')
410
+ end
411
+ EOT
412
+
413
+ translated_code = code_cleanup(<<-EOT)
414
+ class Foo
415
+ include Yast::Logger
416
+
417
+ log.error "foo"
418
+ end
419
+ EOT
420
+
421
+ cop = RuboCop::Cop::Yast::Builtins.new
422
+ expect(autocorrect_source(cop, original_code)).to eq(translated_code)
423
+ end
424
+
425
+ it "It adds logger include with correct indentation" do
426
+ original_code = code_cleanup(<<-EOT)
427
+ class Foo
428
+ Builtins.y2error('foo')
429
+ end
430
+ EOT
431
+
432
+ translated_code = code_cleanup(<<-EOT)
433
+ class Foo
434
+ include Yast::Logger
435
+
436
+ log.error "foo"
437
+ end
438
+ EOT
439
+
440
+ cop = RuboCop::Cop::Yast::Builtins.new
441
+ expect(autocorrect_source(cop, original_code)).to eq(translated_code)
442
+ end
443
+
444
+ it "It does not add the logger include if already present" do
445
+ original_code = code_cleanup(<<-EOT)
446
+ class Foo
447
+ include Yast::Logger
448
+ Builtins.y2error('foo')
449
+ end
450
+ EOT
451
+
452
+ translated_code = code_cleanup(<<-EOT)
453
+ class Foo
454
+ include Yast::Logger
455
+ log.error "foo"
456
+ end
457
+ EOT
458
+
459
+ cop = RuboCop::Cop::Yast::Builtins.new
460
+ expect(autocorrect_source(cop, original_code)).to eq(translated_code)
461
+ end
462
+
463
+ it "It adds the logger include after the parent class name if present" do
464
+ original_code = code_cleanup(<<-EOT)
465
+ class Foo < Bar
466
+ Builtins.y2error('foo')
467
+ end
468
+ EOT
469
+
470
+ translated_code = code_cleanup(<<-EOT)
471
+ class Foo < Bar
472
+ include Yast::Logger
473
+
474
+ log.error "foo"
475
+ end
476
+ EOT
477
+
478
+ cop = RuboCop::Cop::Yast::Builtins.new
479
+ expect(autocorrect_source(cop, original_code)).to eq(translated_code)
480
+ end
481
+
482
+ it "It adds logger include once to a derived class in a module" do
483
+ original_code = code_cleanup(<<-EOT)
484
+ module Yast
485
+ class TestClass < Module
486
+ def test
487
+ Builtins.y2error("foo")
488
+ Builtins.y2debug("foo")
489
+ end
490
+ end
491
+ end
492
+ EOT
493
+
494
+ translated_code = code_cleanup(<<-EOT)
495
+ module Yast
496
+ class TestClass < Module
497
+ include Yast::Logger
498
+
499
+ def test
500
+ log.error "foo"
501
+ log.debug "foo"
502
+ end
503
+ end
504
+ end
505
+ EOT
506
+
507
+ cop = RuboCop::Cop::Yast::Builtins.new
508
+ expect(autocorrect_source(cop, original_code)).to eq(translated_code)
509
+ end
510
+
511
+ it "It converts the single quoted format string to double quoted" do
512
+ original_code = code_cleanup(<<-EOT)
513
+ Builtins.y2milestone('foo: %1', foo)
514
+ EOT
515
+
516
+ translated_code = code_cleanup(<<-EOT)
517
+ include Yast::Logger
518
+ log.info "foo: \#{foo}"
519
+ EOT
520
+
521
+ cop = RuboCop::Cop::Yast::Builtins.new
522
+ expect(autocorrect_source(cop, original_code)).to eq(translated_code)
523
+ end
524
+
525
+ it "It escapes double quotes and interpolations" do
526
+ original_code = code_cleanup(<<-EOT)
527
+ Builtins.y2milestone('"\#{foo}"')
528
+ EOT
529
+
530
+ translated_code = code_cleanup(<<-EOT)
531
+ include Yast::Logger
532
+ log.info "\\"\\\#{foo}\\""
533
+ EOT
534
+
535
+ cop = RuboCop::Cop::Yast::Builtins.new
536
+ expect(autocorrect_source(cop, original_code)).to eq(translated_code)
537
+ end
538
+
539
+ it "It does not convert logging with backtrace" do
540
+ original_code = code_cleanup(<<-EOT)
541
+ Builtins.y2milestone(-1, "foo")
542
+ EOT
543
+
544
+ translated_code = code_cleanup(<<-EOT)
545
+ Builtins.y2milestone(-1, "foo")
546
+ EOT
547
+
548
+ cop = RuboCop::Cop::Yast::Builtins.new
549
+ expect(autocorrect_source(cop, original_code)).to eq(translated_code)
550
+ end
551
+
552
+ it "It does not convert code with a local variable 'log'" do
553
+ original_code = code_cleanup(<<-EOT)
554
+ log = 1
555
+ Builtins.y2milestone("foo")
556
+ EOT
557
+
558
+ translated_code = code_cleanup(<<-EOT)
559
+ log = 1
560
+ Builtins.y2milestone("foo")
561
+ EOT
562
+
563
+ cop = RuboCop::Cop::Yast::Builtins.new
564
+ expect(autocorrect_source(cop, original_code)).to eq(translated_code)
565
+ end
64
566
  end
65
567
  end