Skip to content

Commit 45a0c60

Browse files
[Fix #414] Fix: AnnotateRoutes.remove_annotations duplicates routes
1 parent e763429 commit 45a0c60

File tree

2 files changed

+68
-11
lines changed

2 files changed

+68
-11
lines changed

lib/annotate/annotate_routes.rb

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,17 @@ def do_annotations(options = {})
6666
return unless routes_exists?
6767
existing_text = File.read(routes_file)
6868

69-
if write_contents(existing_text, header(options), options)
69+
if rewrite_contents_with_header(existing_text, header(options), options)
7070
puts "#{routes_file} annotated."
7171
end
7272
end
7373

74-
def remove_annotations(options={})
74+
def remove_annotations(_options={})
7575
return unless routes_exists?
7676
existing_text = File.read(routes_file)
7777
content, where_header_found = strip_annotations(existing_text)
78-
79-
content = strip_on_removal(content, where_header_found)
80-
81-
if write_contents(existing_text, content, options)
78+
new_content = strip_on_removal(content, where_header_found)
79+
if rewrite_contents(existing_text, new_content)
8280
puts "Removed annotations from #{routes_file}."
8381
end
8482
end
@@ -112,7 +110,22 @@ def self.routes_exists?
112110
routes_exists
113111
end
114112

115-
def self.write_contents(existing_text, header, options = {})
113+
# @param [String, Array<String>]
114+
def self.rewrite_contents(existing_text, new_content)
115+
# Make sure we end on a trailing newline.
116+
new_content << '' unless new_content.last == ''
117+
new_text = new_content.join("\n")
118+
119+
if existing_text == new_text
120+
puts "#{routes_file} unchanged."
121+
false
122+
else
123+
File.open(routes_file, 'wb') { |f| f.puts(new_text) }
124+
true
125+
end
126+
end
127+
128+
def self.rewrite_contents_with_header(existing_text, header, options = {})
116129
content, where_header_found = strip_annotations(existing_text)
117130
new_content = annotate_routes(header, content, where_header_found, options)
118131

spec/annotate/annotate_routes_spec.rb

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

172172
it 'should remove trailing annotation and trim trailing newlines, but leave leading newlines alone' do
173-
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")
174-
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/)
173+
expect(File).to receive(:read).with(ROUTE_FILE).and_return(<<-EOS
174+
175+
176+
177+
ActionController::Routing...
178+
foo
179+
180+
181+
# == Route Map
182+
#
183+
# another good line
184+
# good line
185+
EOS
186+
)
187+
expect(@mock_file).to receive(:puts).with(<<-EOS
188+
189+
190+
191+
ActionController::Routing...
192+
foo
193+
EOS
194+
)
175195
AnnotateRoutes.remove_annotations
176196
end
177197

178198
it 'should remove prepended annotation and trim leading newlines, but leave trailing newlines alone' do
179-
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")
180-
expect(@mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n\n\n\n\n\n\n\n\n\n/)
199+
expect(File).to receive(:read).with(ROUTE_FILE).and_return(<<-EOS
200+
# == Route Map
201+
#
202+
# another good line
203+
# good line
204+
205+
206+
207+
208+
Rails.application.routes.draw do
209+
root 'root#index'
210+
end
211+
212+
213+
214+
EOS
215+
)
216+
expect(@mock_file).to receive(:puts).with(<<-EOS
217+
Rails.application.routes.draw do
218+
root 'root#index'
219+
end
220+
221+
222+
223+
EOS
224+
)
181225
AnnotateRoutes.remove_annotations
182226
end
183227
end

0 commit comments

Comments
 (0)