-
Notifications
You must be signed in to change notification settings - Fork 652
/
Copy pathannotate_routes_spec.rb
740 lines (604 loc) · 27.6 KB
/
annotate_routes_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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
require_relative '../../spec_helper'
require 'annotate/annotate_routes'
describe AnnotateRoutes do
ROUTE_FILE = 'config/routes.rb'.freeze
MESSAGE_ANNOTATED = "#{ROUTE_FILE} was annotated.".freeze
MESSAGE_UNCHANGED = "#{ROUTE_FILE} was not changed.".freeze
MESSAGE_NOT_FOUND = "#{ROUTE_FILE} could not be found.".freeze
MESSAGE_REMOVED = "Annotations were removed from #{ROUTE_FILE}.".freeze
MAGIC_COMMENTS = [
'# encoding: UTF-8',
'# coding: UTF-8',
'# -*- coding: UTF-8 -*-',
'#encoding: utf-8',
'# encoding: utf-8',
'# -*- encoding : utf-8 -*-',
"# encoding: utf-8\n# frozen_string_literal: true",
"# frozen_string_literal: true\n# encoding: utf-8",
'# frozen_string_literal: true',
'#frozen_string_literal: false',
'# -*- frozen_string_literal : true -*-',
'#typed: false',
'# typed: true'
].freeze unless const_defined?(:MAGIC_COMMENTS)
let :stubs do
{}
end
let :mock_file do
double(File, stubs)
end
describe '.do_annotations' do
context 'When "config/routes.rb" does not exist' do
before :each do
expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(false).once
end
it 'does not annotates any file' do
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_NOT_FOUND)
AnnotateRoutes.do_annotations
end
end
context 'When "config/routes.rb" exists' do
before :each do
expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true).once
expect(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content).once
expect(AnnotateRoutes::HeaderGenerator).to receive(:`).with('rake routes').and_return(rake_routes_result).once
end
context 'When the result of `rake routes` is present' do
context 'When the result of `rake routes` does not contain Rake version' do
context 'When the file does not contain magic comment' do
let :rake_routes_result do
<<-EOS
Prefix Verb URI Pattern Controller#Action
myaction1 GET /url1(.:format) mycontroller1#action
myaction2 POST /url2(.:format) mycontroller2#action
myaction3 DELETE|GET /url3(.:format) mycontroller3#action
EOS
end
let :route_file_content do
''
end
context 'When the file does not contain annotation yet' do
context 'When no option is passed' do
let :expected_result do
<<~EOS
# == Route Map
#
# Prefix Verb URI Pattern Controller#Action
# myaction1 GET /url1(.:format) mycontroller1#action
# myaction2 POST /url2(.:format) mycontroller2#action
# myaction3 DELETE|GET /url3(.:format) mycontroller3#action
EOS
end
it 'annotates normally' do
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
expect(mock_file).to receive(:puts).with(expected_result).once
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once
AnnotateRoutes.do_annotations
end
end
context 'When the option "format_markdown" is passed' do
let :expected_result do
<<~EOS
# ## Route Map
#
# Prefix | Verb | URI Pattern | Controller#Action
# --------- | ---------- | --------------- | --------------------
# myaction1 | GET | /url1(.:format) | mycontroller1#action
# myaction2 | POST | /url2(.:format) | mycontroller2#action
# myaction3 | DELETE-GET | /url3(.:format) | mycontroller3#action
EOS
end
it 'annotates in Markdown format' do
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
expect(mock_file).to receive(:puts).with(expected_result).once
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once
AnnotateRoutes.do_annotations(format_markdown: true)
end
end
context 'When the options "wrapper_open" and "wrapper_close" are passed' do
let :expected_result do
<<~EOS
# START
# == Route Map
#
# Prefix Verb URI Pattern Controller#Action
# myaction1 GET /url1(.:format) mycontroller1#action
# myaction2 POST /url2(.:format) mycontroller2#action
# myaction3 DELETE|GET /url3(.:format) mycontroller3#action
# END
EOS
end
it 'annotates and wraps annotation with specified words' do
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
expect(mock_file).to receive(:puts).with(expected_result).once
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once
AnnotateRoutes.do_annotations(wrapper_open: 'START', wrapper_close: 'END')
end
end
end
end
context 'When the file contains magic comments' do
MAGIC_COMMENTS.each do |magic_comment|
describe "magic comment: #{magic_comment.inspect}" do
let :route_file_content do
<<~EOS
#{magic_comment}
EOS
end
let :rake_routes_result do
<<-EOS
Prefix Verb URI Pattern Controller#Action
myaction1 GET /url1(.:format) mycontroller1#action
myaction2 POST /url2(.:format) mycontroller2#action
myaction3 DELETE|GET /url3(.:format) mycontroller3#action
EOS
end
context 'When the file does not contain annotation yet' do
context 'When no option is passed' do
let :expected_result do
<<~EOS
#{magic_comment}
# == Route Map
#
# Prefix Verb URI Pattern Controller#Action
# myaction1 GET /url1(.:format) mycontroller1#action
# myaction2 POST /url2(.:format) mycontroller2#action
# myaction3 DELETE|GET /url3(.:format) mycontroller3#action
EOS
end
it 'annotates normally' do
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
expect(mock_file).to receive(:puts).with(expected_result).once
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once
AnnotateRoutes.do_annotations
end
end
context 'When the option "format_markdown" is passed' do
let :expected_result do
<<~EOS
#{magic_comment}
# ## Route Map
#
# Prefix | Verb | URI Pattern | Controller#Action
# --------- | ---------- | --------------- | --------------------
# myaction1 | GET | /url1(.:format) | mycontroller1#action
# myaction2 | POST | /url2(.:format) | mycontroller2#action
# myaction3 | DELETE-GET | /url3(.:format) | mycontroller3#action
EOS
end
it 'annotates in Markdown format' do
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
expect(mock_file).to receive(:puts).with(expected_result).once
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once
AnnotateRoutes.do_annotations(format_markdown: true)
end
end
context 'When the options "wrapper_open" and "wrapper_close" are passed' do
let :expected_result do
<<~EOS
#{magic_comment}
# START
# == Route Map
#
# Prefix Verb URI Pattern Controller#Action
# myaction1 GET /url1(.:format) mycontroller1#action
# myaction2 POST /url2(.:format) mycontroller2#action
# myaction3 DELETE|GET /url3(.:format) mycontroller3#action
# END
EOS
end
it 'annotates and wraps annotation with specified words' do
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
expect(mock_file).to receive(:puts).with(expected_result).once
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once
AnnotateRoutes.do_annotations(wrapper_open: 'START', wrapper_close: 'END')
end
end
end
end
end
end
end
context 'When the result of `rake routes` contains Rake version' do
context 'with older Rake versions' do
let :rake_routes_result do
<<~EOS.chomp
(in /bad/line)
good line
EOS
end
context 'When the route file does not end with an empty line' do
let :route_file_content do
<<~EOS.chomp
ActionController::Routing...
foo
EOS
end
let :expected_result do
<<~EOS
ActionController::Routing...
foo
# == Route Map
#
# good line
EOS
end
it 'annotates with an empty line' do
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
expect(mock_file).to receive(:puts).with(expected_result).once
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once
AnnotateRoutes.do_annotations
end
end
context 'When the route file ends with an empty line' do
let :route_file_content do
<<~EOS
ActionController::Routing...
foo
EOS
end
let :expected_result do
<<~EOS
ActionController::Routing...
foo
# == Route Map
#
# good line
EOS
end
it 'annotates without an empty line' do
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
expect(mock_file).to receive(:puts).with(expected_result).once
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once
AnnotateRoutes.do_annotations
end
end
end
context 'with newer Rake versions' do
let :rake_routes_result do
<<~EOS.chomp
another good line
good line
EOS
end
context 'When the route file does not end with an empty line' do
context 'When no option is passed' do
let :route_file_content do
<<~EOS.chomp
ActionController::Routing...
foo
EOS
end
let :expected_result do
<<~EOS
ActionController::Routing...
foo
# == Route Map
#
# another good line
# good line
EOS
end
it 'annotates with an empty line' do
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
expect(mock_file).to receive(:puts).with(expected_result).once
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once
AnnotateRoutes.do_annotations
end
end
end
context 'When the route file ends with an empty line' do
let :route_file_content do
<<~EOS
ActionController::Routing...
foo
EOS
end
let :expected_result do
<<~EOS
ActionController::Routing...
foo
# == Route Map
#
# another good line
# good line
EOS
end
it 'annotates without an empty line' do
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
expect(mock_file).to receive(:puts).with(expected_result).once
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once
AnnotateRoutes.do_annotations
end
end
context 'When option "timestamp" is passed' do
let :route_file_content do
<<~EOS.chomp
ActionController::Routing...
foo
EOS
end
let :expected_result do
/ActionController::Routing...\nfoo\n\n# == Route Map \(Updated \d{4}-\d{2}-\d{2} \d{2}:\d{2}\)\n#\n# another good line\n# good line\n/
end
it 'annotates with the timestamp and an empty line' do
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
expect(mock_file).to receive(:puts).with(expected_result).once
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once
AnnotateRoutes.do_annotations timestamp: true
end
end
end
end
end
context 'When the result of `rake routes` is blank' do
let :rake_routes_result do
''
end
context 'When the file does not contain magic comment' do
context 'When the file does not contain annotation yet' do
let :route_file_content do
''
end
context 'When no option is specified' do
let :expected_result do
<<~EOS
# == Route Map
#
EOS
end
it 'inserts annotations' do
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
expect(mock_file).to receive(:puts).with(expected_result).once
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once
AnnotateRoutes.do_annotations
end
end
context 'When the option "ignore_routes" is specified' do
let :expected_result do
<<~EOS
# == Route Map
#
EOS
end
it 'inserts annotations' do
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
expect(mock_file).to receive(:puts).with(expected_result).once
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once
AnnotateRoutes.do_annotations(ignore_routes: 'my_route')
end
end
context 'When the option "position_in_routes" is specified as "top"' do
let :expected_result do
<<~EOS
# == Route Map
#
EOS
end
it 'inserts annotations' do
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
expect(mock_file).to receive(:puts).with(expected_result).once
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once
AnnotateRoutes.do_annotations(position_in_routes: 'top')
end
end
end
context 'When the file already contains annotation' do
context 'When no option is specified' do
let :route_file_content do
<<~EOS
# == Route Map
#
EOS
end
it 'should skip annotations if file does already contain annotation' do
expect(File).not_to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file)
expect(mock_file).not_to receive(:puts)
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_UNCHANGED).once
AnnotateRoutes.do_annotations
end
end
end
end
context 'When the file contains magic comments' do
MAGIC_COMMENTS.each do |magic_comment|
describe "magic comment: #{magic_comment.inspect}" do
let :route_file_content do
<<~EOS
#{magic_comment}
Something
EOS
end
context 'When the file does not contain annotation yet' do
context 'When the option "position_in_routes" is specified as "top"' do
let :expected_result do
<<~EOS
#{magic_comment}
# == Route Map
#
Something
EOS
end
it 'leaves magic comment on top and adds an empty line between magic comment and annotation' do
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
expect(mock_file).to receive(:puts).with(expected_result).once
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once
AnnotateRoutes.do_annotations(position_in_routes: 'top')
end
end
context 'When the option "position_in_routes" is specified as "bottom"' do
let :expected_result do
<<~EOS
#{magic_comment}
Something
# == Route Map
#
EOS
end
it 'leaves magic comment on top and adds an empty line between magic comment and annotation' do
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
expect(mock_file).to receive(:puts).with(expected_result).once
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once
AnnotateRoutes.do_annotations(position_in_routes: 'bottom')
end
end
end
context 'When the file already contains annotation' do
let :route_file_content do
<<~EOS
#{magic_comment}
# == Route Map
#
EOS
end
it 'skips annotations' do
expect(File).not_to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file)
expect(mock_file).not_to receive(:puts)
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_UNCHANGED).once
AnnotateRoutes.do_annotations
end
end
end
end
end
end
end
describe 'frozen option' do
let :aborted_message do
"annotate error. #{ROUTE_FILE} needs to be updated, but annotate was run with `--frozen`."
end
let :rake_routes_result do
<<-EOS
Prefix Verb URI Pattern Controller#Action
myaction1 GET /url1(.:format) mycontroller1#action
myaction2 POST /url2(.:format) mycontroller2#action
myaction3 DELETE|GET /url3(.:format) mycontroller3#action
EOS
end
before :each do
expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true).once
expect(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content).once
expect(AnnotateRoutes::HeaderGenerator).to receive(:`).with('rake routes').and_return(rake_routes_result).once
end
context 'when annotation does not exists' do
let :route_file_content do
''
end
it 'aborts' do
expect { AnnotateRoutes.do_annotations(frozen: true) }.to raise_error SystemExit, aborted_message
end
end
context 'when annotation exists but is not updated' do
let :route_file_content do
<<~EOS
# == Route Map
#
# Prefix Verb URI Pattern Controller#Action
# myaction2 POST /url2(.:format) mycontroller2#action
# myaction3 DELETE|GET /url3(.:format) mycontroller3#action
EOS
end
it 'aborts' do
expect { AnnotateRoutes.do_annotations(frozen: true) }.to raise_error SystemExit, aborted_message
end
end
context 'when annotation exists and is already updated' do
let :route_file_content do
<<~EOS
# == Route Map
#
# Prefix Verb URI Pattern Controller#Action
# myaction1 GET /url1(.:format) mycontroller1#action
# myaction2 POST /url2(.:format) mycontroller2#action
# myaction3 DELETE|GET /url3(.:format) mycontroller3#action
EOS
end
it 'does NOT abort' do
expect { AnnotateRoutes.do_annotations(frozen: true) }.not_to raise_error
end
end
end
end
describe '.remove_annotations' do
before :each do
expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true).once
expect(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content).once
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
end
context 'When trailing annotation exists' do
let :route_file_content do
<<~EOS
ActionController::Routing...
foo
# == Route Map
#
# another good line
# good line
EOS
end
let :expected_result do
<<~EOS
ActionController::Routing...
foo
EOS
end
it 'removes trailing annotation and trim trailing newlines, but leave leading newlines alone' do
expect(mock_file).to receive(:puts).with(expected_result).once
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_REMOVED).once
AnnotateRoutes.remove_annotations
end
end
context 'When prepended annotation exists' do
let :route_file_content do
<<~EOS
# == Route Map
#
# another good line
# good line
Rails.application.routes.draw do
root 'root#index'
end
EOS
end
let :expected_result do
<<~EOS
Rails.application.routes.draw do
root 'root#index'
end
EOS
end
it 'removes prepended annotation and trim leading newlines, but leave trailing newlines alone' do
expect(mock_file).to receive(:puts).with(expected_result).once
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_REMOVED).once
AnnotateRoutes.remove_annotations
end
end
context 'When custom comments are above route map' do
let :route_file_content do
<<~EOS
# My comment
# == Route Map
#
# another good line
# good line
Rails.application.routes.draw do
root 'root#index'
end
EOS
end
let :expected_result do
<<~EOS
# My comment
Rails.application.routes.draw do
root 'root#index'
end
EOS
end
it 'does not remove custom comments above route map' do
expect(mock_file).to receive(:puts).with(expected_result).once
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_REMOVED).once
AnnotateRoutes.remove_annotations
end
end
end
end