Skip to content

Commit 8da60ff

Browse files
[Fix #414] Fix: AnnotateRoutes.remove_annotations duplicates routes
1 parent d02ff2e commit 8da60ff

File tree

2 files changed

+70
-11
lines changed

2 files changed

+70
-11
lines changed

lib/annotate/annotate_routes.rb

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# rubocop:disable Metrics/ModuleLength
2+
13
# == Annotate Routes
24
#
35
# Based on:
@@ -58,19 +60,17 @@ def do_annotations(options = {})
5860
return unless routes_exists?
5961
existing_text = File.read(routes_file)
6062

61-
if write_contents(existing_text, header(options), options)
63+
if rewrite_contents_with_header(existing_text, header(options), options)
6264
puts "#{routes_file} annotated."
6365
end
6466
end
6567

66-
def remove_annotations(options={})
68+
def remove_annotations(_options={})
6769
return unless routes_exists?
6870
existing_text = File.read(routes_file)
6971
content, where_header_found = strip_annotations(existing_text)
70-
71-
content = strip_on_removal(content, where_header_found)
72-
73-
if write_contents(existing_text, content, options)
72+
new_content = strip_on_removal(content, where_header_found)
73+
if rewrite_contents(existing_text, new_content)
7474
puts "Removed annotations from #{routes_file}."
7575
end
7676
end
@@ -104,7 +104,22 @@ def self.routes_exists?
104104
routes_exists
105105
end
106106

107-
def self.write_contents(existing_text, header, options = {})
107+
# @param [String, Array<String>]
108+
def self.rewrite_contents(existing_text, new_content)
109+
# Make sure we end on a trailing newline.
110+
new_content << '' unless new_content.last == ''
111+
new_text = new_content.join("\n")
112+
113+
if existing_text == new_text
114+
puts "#{routes_file} unchanged."
115+
false
116+
else
117+
File.open(routes_file, 'wb') { |f| f.puts(new_text) }
118+
true
119+
end
120+
end
121+
122+
def self.rewrite_contents_with_header(existing_text, header, options = {})
108123
content, where_header_found = strip_annotations(existing_text)
109124
new_content = annotate_routes(header, content, where_header_found, options)
110125

spec/annotate/annotate_routes_spec.rb

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,58 @@ def mock_file(stubs = {})
155155
end
156156

157157
it 'should remove trailing annotation and trim trailing newlines, but leave leading newlines alone' do
158-
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")
159-
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/)
158+
expect(File).to receive(:read).with(ROUTE_FILE).and_return(<<-EOS
159+
160+
161+
162+
ActionController::Routing...
163+
foo
164+
165+
166+
# == Route Map
167+
#
168+
# another good line
169+
# good line
170+
EOS
171+
)
172+
expect(@mock_file).to receive(:puts).with(<<-EOS
173+
174+
175+
176+
ActionController::Routing...
177+
foo
178+
EOS
179+
)
160180
AnnotateRoutes.remove_annotations
161181
end
162182

163183
it 'should remove prepended annotation and trim leading newlines, but leave trailing newlines alone' do
164-
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")
165-
expect(@mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n\n\n\n\n\n\n\n\n\n/)
184+
expect(File).to receive(:read).with(ROUTE_FILE).and_return(<<-EOS
185+
# == Route Map
186+
#
187+
# another good line
188+
# good line
189+
190+
191+
192+
193+
Rails.application.routes.draw do
194+
root 'root#index'
195+
end
196+
197+
198+
199+
EOS
200+
)
201+
expect(@mock_file).to receive(:puts).with(<<-EOS
202+
Rails.application.routes.draw do
203+
root 'root#index'
204+
end
205+
206+
207+
208+
EOS
209+
)
166210
AnnotateRoutes.remove_annotations
167211
end
168212
end

0 commit comments

Comments
 (0)