From a5d5681d48bfb3fd6da66204b0f157af525f117a Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Thu, 9 Jan 2020 01:13:28 +0900 Subject: [PATCH 1/3] Refactor AnnotateRoutes.routes_exists? --- lib/annotate/annotate_routes.rb | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/lib/annotate/annotate_routes.rb b/lib/annotate/annotate_routes.rb index 31c2f2da3..0fe929315 100644 --- a/lib/annotate/annotate_routes.rb +++ b/lib/annotate/annotate_routes.rb @@ -26,31 +26,33 @@ module AnnotateRoutes class << self def do_annotations(options = {}) - return unless routes_exists? - existing_text = File.read(routes_file) - - if rewrite_contents_with_header(existing_text, header(options), options) - puts "#{routes_file} annotated." + if routes_exists? + existing_text = File.read(routes_file) + if rewrite_contents_with_header(existing_text, header(options), options) + puts "#{routes_file} annotated." + end + else + puts "Can't find routes.rb" end end def remove_annotations(_options={}) - return unless routes_exists? - existing_text = File.read(routes_file) - content, header_position = strip_annotations(existing_text) - new_content = strip_on_removal(content, header_position) - if rewrite_contents(existing_text, new_content) - puts "Removed annotations from #{routes_file}." + if routes_exists? + existing_text = File.read(routes_file) + content, header_position = strip_annotations(existing_text) + new_content = strip_on_removal(content, header_position) + if rewrite_contents(existing_text, new_content) + puts "Removed annotations from #{routes_file}." + end + else + puts "Can't find routes.rb" end end private def routes_exists? - routes_exists = File.exists?(routes_file) - puts "Can't find routes.rb" unless routes_exists - - routes_exists + File.exists?(routes_file) end def routes_file From d3e3b137140b5fd09ce0a0bb1448c66f48528b89 Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Thu, 9 Jan 2020 01:15:33 +0900 Subject: [PATCH 2/3] Fix AnnotateRoutes.routes_exists? --- lib/annotate/annotate_routes.rb | 2 +- spec/lib/annotate/annotate_routes_spec.rb | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/annotate/annotate_routes.rb b/lib/annotate/annotate_routes.rb index 0fe929315..ac46c8e9e 100644 --- a/lib/annotate/annotate_routes.rb +++ b/lib/annotate/annotate_routes.rb @@ -52,7 +52,7 @@ def remove_annotations(_options={}) private def routes_exists? - File.exists?(routes_file) + File.exist?(routes_file) end def routes_file diff --git a/spec/lib/annotate/annotate_routes_spec.rb b/spec/lib/annotate/annotate_routes_spec.rb index 2d2cd4eae..ed19b781d 100644 --- a/spec/lib/annotate/annotate_routes_spec.rb +++ b/spec/lib/annotate/annotate_routes_spec.rb @@ -28,7 +28,7 @@ def magic_comments_list_each end it 'should check if routes.rb exists' do - expect(File).to receive(:exists?).with(ROUTE_FILE).and_return(false) + expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(false) expect(AnnotateRoutes).to receive(:puts).with("Can't find routes.rb") AnnotateRoutes.do_annotations end @@ -42,7 +42,7 @@ def magic_comments_list_each end before(:each) do - expect(File).to receive(:exists?).with(ROUTE_FILE).and_return(true).at_least(:once) + expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true).at_least(:once) expect(File).to receive(:read).with(ROUTE_FILE).and_return("").at_least(:once) @@ -164,7 +164,7 @@ def magic_comments_list_each describe 'When adding' do before(:each) do - expect(File).to receive(:exists?).with(ROUTE_FILE) + expect(File).to receive(:exist?).with(ROUTE_FILE) .and_return(true).at_least(:once) expect(AnnotateRoutes).to receive(:`).with('rake routes') .and_return('').at_least(:once) @@ -243,7 +243,7 @@ def magic_comments_list_each describe 'When adding with older Rake versions' do before(:each) do - expect(File).to receive(:exists?).with(ROUTE_FILE).and_return(true) + expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true) expect(AnnotateRoutes).to receive(:`).with('rake routes').and_return("(in /bad/line)\ngood line") expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_ADDED) @@ -264,7 +264,7 @@ def magic_comments_list_each describe 'When adding with newer Rake versions' do before(:each) do - expect(File).to receive(:exists?).with(ROUTE_FILE).and_return(true) + expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true) expect(AnnotateRoutes).to receive(:`).with('rake routes').and_return("another good line\ngood line") expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_ADDED) @@ -291,7 +291,7 @@ def magic_comments_list_each describe 'When removing' do before(:each) do - expect(File).to receive(:exists?).with(ROUTE_FILE).and_return(true) + expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true) expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_REMOVED) end From 77e5b7bf0513cffc3596687ef03f9df295bffdd2 Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Thu, 9 Jan 2020 01:16:25 +0900 Subject: [PATCH 3/3] Rename AnnotateRoutes.routes_exists? to .routes_file_exist? --- lib/annotate/annotate_routes.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/annotate/annotate_routes.rb b/lib/annotate/annotate_routes.rb index ac46c8e9e..a7aeec699 100644 --- a/lib/annotate/annotate_routes.rb +++ b/lib/annotate/annotate_routes.rb @@ -26,7 +26,7 @@ module AnnotateRoutes class << self def do_annotations(options = {}) - if routes_exists? + if routes_file_exist? existing_text = File.read(routes_file) if rewrite_contents_with_header(existing_text, header(options), options) puts "#{routes_file} annotated." @@ -37,7 +37,7 @@ def do_annotations(options = {}) end def remove_annotations(_options={}) - if routes_exists? + if routes_file_exist? existing_text = File.read(routes_file) content, header_position = strip_annotations(existing_text) new_content = strip_on_removal(content, header_position) @@ -51,7 +51,7 @@ def remove_annotations(_options={}) private - def routes_exists? + def routes_file_exist? File.exist?(routes_file) end