Commit 9a8db1b7 by Shu Fujita Committed by Andrew W. Lee

Refactor AnnotateRoutes.routes_file_exist? (#716)

I refactored `AnnotateRoutes.routes_exists?` and methods using this. The points are as follows. * Removing `puts` in `AnnotateRoutes.routes_exists?` * Using `File.exist?` instead of `File.exists?` because `File.exists?` is deprecated * Renaming `AnnotateRoutes.routes_exists?` to `AnnotateRoutes.routes_file_exists?` in order to make the name of method more explanatory
parent 966bff0e
...@@ -28,33 +28,34 @@ module AnnotateRoutes ...@@ -28,33 +28,34 @@ module AnnotateRoutes
class << self class << self
def do_annotations(options = {}) def do_annotations(options = {})
return unless routes_exists? if routes_file_exist?
existing_text = File.read(routes_file) existing_text = File.read(routes_file)
if rewrite_contents_with_header(existing_text, header(options), options)
if rewrite_contents_with_header(existing_text, header(options), options) puts "#{routes_file} annotated."
puts "#{routes_file} annotated." end
else
puts "Can't find routes.rb"
end end
end end
def remove_annotations(_options={}) def remove_annotations(_options={})
return unless routes_exists? if routes_file_exist?
existing_text = File.read(routes_file) existing_text = File.read(routes_file)
content, header_position = strip_annotations(existing_text) content, header_position = strip_annotations(existing_text)
new_content = strip_on_removal(content, header_position) new_content = strip_on_removal(content, header_position)
new_text = new_content.join("\n") new_text = new_content.join("\n")
if rewrite_contents(existing_text, new_text)
if rewrite_contents(existing_text, new_text) puts "Removed annotations from #{routes_file}."
puts "Removed annotations from #{routes_file}." end
else
puts "Can't find routes.rb"
end end
end end
private private
def routes_exists? def routes_file_exist?
routes_exists = File.exists?(routes_file) File.exist?(routes_file)
puts "Can't find routes.rb" unless routes_exists
routes_exists
end end
def routes_file def routes_file
......
...@@ -28,7 +28,7 @@ describe AnnotateRoutes do ...@@ -28,7 +28,7 @@ describe AnnotateRoutes do
end end
it 'should check if routes.rb exists' do 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") expect(AnnotateRoutes).to receive(:puts).with("Can't find routes.rb")
AnnotateRoutes.do_annotations AnnotateRoutes.do_annotations
end end
...@@ -42,7 +42,7 @@ describe AnnotateRoutes do ...@@ -42,7 +42,7 @@ describe AnnotateRoutes do
end end
before(:each) do 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) expect(File).to receive(:read).with(ROUTE_FILE).and_return("").at_least(:once)
...@@ -164,7 +164,7 @@ describe AnnotateRoutes do ...@@ -164,7 +164,7 @@ describe AnnotateRoutes do
describe 'When adding' do describe 'When adding' do
before(:each) 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) .and_return(true).at_least(:once)
expect(AnnotateRoutes).to receive(:`).with('rake routes') expect(AnnotateRoutes).to receive(:`).with('rake routes')
.and_return('').at_least(:once) .and_return('').at_least(:once)
...@@ -243,7 +243,7 @@ describe AnnotateRoutes do ...@@ -243,7 +243,7 @@ describe AnnotateRoutes do
describe 'When adding with older Rake versions' do describe 'When adding with older Rake versions' do
before(:each) 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(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(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file)
expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_ADDED) expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_ADDED)
...@@ -264,7 +264,7 @@ describe AnnotateRoutes do ...@@ -264,7 +264,7 @@ describe AnnotateRoutes do
describe 'When adding with newer Rake versions' do describe 'When adding with newer Rake versions' do
before(:each) 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(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(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file)
expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_ADDED) expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_ADDED)
...@@ -291,7 +291,7 @@ describe AnnotateRoutes do ...@@ -291,7 +291,7 @@ describe AnnotateRoutes do
describe 'When removing' do describe 'When removing' do
before(:each) 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(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file)
expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_REMOVED) expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_REMOVED)
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment