diff --git a/spec/lib/annotate/annotate_routes_spec.rb b/spec/lib/annotate/annotate_routes_spec.rb index 26ad4c522..dc4400a35 100644 --- a/spec/lib/annotate/annotate_routes_spec.rb +++ b/spec/lib/annotate/annotate_routes_spec.rb @@ -235,6 +235,163 @@ 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 @@ -401,170 +558,6 @@ end end - describe 'As for Rake versions' 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).to receive(:`).with('rake routes').and_return(rake_routes_result).once - end - - 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 - describe '.remove_annotations' do before :each do expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true).once