Commit 7c8e916d by Alexander Belozerov Committed by Cuong Tran

[Fix #414] Fix: AnnotateRoutes.remove_annotations duplicates routes (#493)

parent d44f7051
......@@ -66,19 +66,17 @@ module AnnotateRoutes
return unless routes_exists?
existing_text = File.read(routes_file)
if write_contents(existing_text, header(options), options)
if rewrite_contents_with_header(existing_text, header(options), options)
puts "#{routes_file} annotated."
end
end
def remove_annotations(options={})
def remove_annotations(_options={})
return unless routes_exists?
existing_text = File.read(routes_file)
content, where_header_found = strip_annotations(existing_text)
content = strip_on_removal(content, where_header_found)
if write_contents(existing_text, content, options)
new_content = strip_on_removal(content, where_header_found)
if rewrite_contents(existing_text, new_content)
puts "Removed annotations from #{routes_file}."
end
end
......@@ -112,7 +110,22 @@ module AnnotateRoutes
routes_exists
end
def self.write_contents(existing_text, header, options = {})
# @param [String, Array<String>]
def self.rewrite_contents(existing_text, new_content)
# Make sure we end on a trailing newline.
new_content << '' unless new_content.last == ''
new_text = new_content.join("\n")
if existing_text == new_text
puts "#{routes_file} unchanged."
false
else
File.open(routes_file, 'wb') { |f| f.puts(new_text) }
true
end
end
def self.rewrite_contents_with_header(existing_text, header, options = {})
content, where_header_found = strip_annotations(existing_text)
new_content = annotate_routes(header, content, where_header_found, options)
......@@ -162,7 +175,7 @@ module AnnotateRoutes
content.split(/\n/, -1).each_with_index do |line, line_number|
if mode == :header && line !~ /\s*#/
mode = :content
next unless line == ''
real_content << line unless line.blank?
elsif mode == :content
if line =~ /^\s*#\s*== Route.*$/
header_found_at = line_number + 1 # index start's at 0
......
......@@ -170,14 +170,82 @@ describe AnnotateRoutes do
end
it 'should remove trailing annotation and trim trailing newlines, but leave leading newlines alone' do
expect(File).to receive(:read).with(ROUTE_FILE).and_return("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nActionController::Routing...\nfoo\n\n\n\n\n\n\n\n\n\n\n# == Route Map\n#\n# another good line\n# good line\n")
expect(@mock_file).to receive(:puts).with(/\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nActionController::Routing...\nfoo\n/)
expect(File).to receive(:read).with(ROUTE_FILE).and_return(<<-EOS
ActionController::Routing...
foo
# == Route Map
#
# another good line
# good line
EOS
)
expect(@mock_file).to receive(:puts).with(<<-EOS
ActionController::Routing...
foo
EOS
)
AnnotateRoutes.remove_annotations
end
it 'should remove prepended annotation and trim leading newlines, but leave trailing newlines alone' do
expect(File).to receive(:read).with(ROUTE_FILE).and_return("# == Route Map\n#\n# another good line\n# good line\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nActionController::Routing...\nfoo\n\n\n\n\n\n\n\n\n\n\n")
expect(@mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n\n\n\n\n\n\n\n\n\n/)
expect(File).to receive(:read).with(ROUTE_FILE).and_return(<<-EOS
# == Route Map
#
# another good line
# good line
Rails.application.routes.draw do
root 'root#index'
end
EOS
)
expect(@mock_file).to receive(:puts).with(<<-EOS
Rails.application.routes.draw do
root 'root#index'
end
EOS
)
AnnotateRoutes.remove_annotations
end
it 'should not remove custom comments above route map' do
expect(File).to receive(:read).with(ROUTE_FILE).and_return(<<-EOS
# My comment
# == Route Map
#
# another good line
# good line
Rails.application.routes.draw do
root 'root#index'
end
EOS
)
expect(@mock_file).to receive(:puts).with(<<-EOS
# My comment
Rails.application.routes.draw do
root 'root#index'
end
EOS
)
AnnotateRoutes.remove_annotations
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