Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions lib/annotate/annotate_routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,17 @@ def do_annotations(options = {})
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
Expand Down Expand Up @@ -112,7 +110,22 @@ def self.routes_exists?
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)

Expand Down Expand Up @@ -162,7 +175,7 @@ def self.strip_annotations(content)
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
Expand Down
76 changes: 72 additions & 4 deletions spec/annotate/annotate_routes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,82 @@ def mock_file(stubs = {})
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
Expand Down