Skip to content

Commit dfdffc5

Browse files
committed
Refactor test cases when the result of rake routes contains Rake version
1 parent 6de2d6e commit dfdffc5

File tree

1 file changed

+157
-164
lines changed

1 file changed

+157
-164
lines changed

spec/lib/annotate/annotate_routes_spec.rb

Lines changed: 157 additions & 164 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,163 @@
235235
end
236236
end
237237
end
238+
239+
context 'When the result of `rake routes` contains Rake version' do
240+
context 'with older Rake versions' do
241+
let :rake_routes_result do
242+
<<~EOS.chomp
243+
(in /bad/line)
244+
good line
245+
EOS
246+
end
247+
248+
context 'When the route file does not end with an empty line' do
249+
let :route_file_content do
250+
<<~EOS.chomp
251+
ActionController::Routing...
252+
foo
253+
EOS
254+
end
255+
256+
let :expected_result do
257+
<<~EOS
258+
ActionController::Routing...
259+
foo
260+
261+
# == Route Map
262+
#
263+
# good line
264+
EOS
265+
end
266+
267+
it 'annotates with an empty line' do
268+
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
269+
expect(mock_file).to receive(:puts).with(expected_result).once
270+
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once
271+
272+
AnnotateRoutes.do_annotations
273+
end
274+
end
275+
276+
context 'When the route file ends with an empty line' do
277+
let :route_file_content do
278+
<<~EOS
279+
ActionController::Routing...
280+
foo
281+
EOS
282+
end
283+
284+
let :expected_result do
285+
<<~EOS
286+
ActionController::Routing...
287+
foo
288+
289+
# == Route Map
290+
#
291+
# good line
292+
EOS
293+
end
294+
295+
it 'annotates without an empty line' do
296+
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
297+
expect(mock_file).to receive(:puts).with(expected_result).once
298+
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once
299+
300+
AnnotateRoutes.do_annotations
301+
end
302+
end
303+
end
304+
305+
context 'with newer Rake versions' do
306+
let :rake_routes_result do
307+
<<~EOS.chomp
308+
another good line
309+
good line
310+
EOS
311+
end
312+
313+
context 'When the route file does not end with an empty line' do
314+
context 'When no option is passed' do
315+
let :route_file_content do
316+
<<~EOS.chomp
317+
ActionController::Routing...
318+
foo
319+
EOS
320+
end
321+
322+
let :expected_result do
323+
<<~EOS
324+
ActionController::Routing...
325+
foo
326+
327+
# == Route Map
328+
#
329+
# another good line
330+
# good line
331+
EOS
332+
end
333+
334+
it 'annotates with an empty line' do
335+
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
336+
expect(mock_file).to receive(:puts).with(expected_result).once
337+
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once
338+
339+
AnnotateRoutes.do_annotations
340+
end
341+
end
342+
end
343+
344+
context 'When the route file ends with an empty line' do
345+
let :route_file_content do
346+
<<~EOS
347+
ActionController::Routing...
348+
foo
349+
EOS
350+
end
351+
352+
let :expected_result do
353+
<<~EOS
354+
ActionController::Routing...
355+
foo
356+
357+
# == Route Map
358+
#
359+
# another good line
360+
# good line
361+
EOS
362+
end
363+
364+
it 'annotates without an empty line' do
365+
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
366+
expect(mock_file).to receive(:puts).with(expected_result).once
367+
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once
368+
369+
AnnotateRoutes.do_annotations
370+
end
371+
end
372+
373+
context 'When option "timestamp" is passed' do
374+
let :route_file_content do
375+
<<~EOS.chomp
376+
ActionController::Routing...
377+
foo
378+
EOS
379+
end
380+
381+
let :expected_result do
382+
/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/
383+
end
384+
385+
it 'annotates with the timestamp and an empty line' do
386+
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
387+
expect(mock_file).to receive(:puts).with(expected_result).once
388+
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once
389+
390+
AnnotateRoutes.do_annotations timestamp: true
391+
end
392+
end
393+
end
394+
end
238395
end
239396

240397
context 'When the result of `rake routes` is blank' do
@@ -401,170 +558,6 @@
401558
end
402559
end
403560

404-
describe 'As for Rake versions' do
405-
before :each do
406-
expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true).once
407-
expect(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content).once
408-
409-
expect(AnnotateRoutes).to receive(:`).with('rake routes').and_return(rake_routes_result).once
410-
end
411-
412-
context 'with older Rake versions' do
413-
let :rake_routes_result do
414-
<<~EOS.chomp
415-
(in /bad/line)
416-
good line
417-
EOS
418-
end
419-
420-
context 'When the route file does not end with an empty line' do
421-
let :route_file_content do
422-
<<~EOS.chomp
423-
ActionController::Routing...
424-
foo
425-
EOS
426-
end
427-
428-
let :expected_result do
429-
<<~EOS
430-
ActionController::Routing...
431-
foo
432-
433-
# == Route Map
434-
#
435-
# good line
436-
EOS
437-
end
438-
439-
it 'annotates with an empty line' do
440-
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
441-
expect(mock_file).to receive(:puts).with(expected_result).once
442-
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once
443-
444-
AnnotateRoutes.do_annotations
445-
end
446-
end
447-
448-
context 'When the route file ends with an empty line' do
449-
let :route_file_content do
450-
<<~EOS
451-
ActionController::Routing...
452-
foo
453-
EOS
454-
end
455-
456-
let :expected_result do
457-
<<~EOS
458-
ActionController::Routing...
459-
foo
460-
461-
# == Route Map
462-
#
463-
# good line
464-
EOS
465-
end
466-
467-
it 'annotates without an empty line' do
468-
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
469-
expect(mock_file).to receive(:puts).with(expected_result).once
470-
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once
471-
472-
AnnotateRoutes.do_annotations
473-
end
474-
end
475-
end
476-
477-
context 'with newer Rake versions' do
478-
let :rake_routes_result do
479-
<<~EOS.chomp
480-
another good line
481-
good line
482-
EOS
483-
end
484-
485-
context 'When the route file does not end with an empty line' do
486-
context 'When no option is passed' do
487-
let :route_file_content do
488-
<<~EOS.chomp
489-
ActionController::Routing...
490-
foo
491-
EOS
492-
end
493-
494-
let :expected_result do
495-
<<~EOS
496-
ActionController::Routing...
497-
foo
498-
499-
# == Route Map
500-
#
501-
# another good line
502-
# good line
503-
EOS
504-
end
505-
506-
it 'annotates with an empty line' do
507-
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
508-
expect(mock_file).to receive(:puts).with(expected_result).once
509-
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once
510-
511-
AnnotateRoutes.do_annotations
512-
end
513-
end
514-
end
515-
516-
context 'When the route file ends with an empty line' do
517-
let :route_file_content do
518-
<<~EOS
519-
ActionController::Routing...
520-
foo
521-
EOS
522-
end
523-
524-
let :expected_result do
525-
<<~EOS
526-
ActionController::Routing...
527-
foo
528-
529-
# == Route Map
530-
#
531-
# another good line
532-
# good line
533-
EOS
534-
end
535-
536-
it 'annotates without an empty line' do
537-
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
538-
expect(mock_file).to receive(:puts).with(expected_result).once
539-
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once
540-
541-
AnnotateRoutes.do_annotations
542-
end
543-
end
544-
545-
context 'When option "timestamp" is passed' do
546-
let :route_file_content do
547-
<<~EOS.chomp
548-
ActionController::Routing...
549-
foo
550-
EOS
551-
end
552-
553-
let :expected_result do
554-
/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/
555-
end
556-
557-
it 'annotates with the timestamp and an empty line' do
558-
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
559-
expect(mock_file).to receive(:puts).with(expected_result).once
560-
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once
561-
562-
AnnotateRoutes.do_annotations timestamp: true
563-
end
564-
end
565-
end
566-
end
567-
568561
describe '.remove_annotations' do
569562
before :each do
570563
expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true).once

0 commit comments

Comments
 (0)