Skip to content

Commit 7c8e916

Browse files
sashabelozerovctran
authored andcommitted
[Fix #414] Fix: AnnotateRoutes.remove_annotations duplicates routes (#493)
1 parent d44f705 commit 7c8e916

File tree

2 files changed

+93
-12
lines changed

2 files changed

+93
-12
lines changed

lib/annotate/annotate_routes.rb

Lines changed: 21 additions & 8 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

@@ -162,7 +175,7 @@ def self.strip_annotations(content)
162175
content.split(/\n/, -1).each_with_index do |line, line_number|
163176
if mode == :header && line !~ /\s*#/
164177
mode = :content
165-
next unless line == ''
178+
real_content << line unless line.blank?
166179
elsif mode == :content
167180
if line =~ /^\s*#\s*== Route.*$/
168181
header_found_at = line_number + 1 # index start's at 0

spec/annotate/annotate_routes_spec.rb

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,82 @@ 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+
)
225+
AnnotateRoutes.remove_annotations
226+
end
227+
228+
it 'should not remove custom comments above route map' do
229+
expect(File).to receive(:read).with(ROUTE_FILE).and_return(<<-EOS
230+
# My comment
231+
# == Route Map
232+
#
233+
# another good line
234+
# good line
235+
Rails.application.routes.draw do
236+
root 'root#index'
237+
end
238+
EOS
239+
)
240+
241+
expect(@mock_file).to receive(:puts).with(<<-EOS
242+
# My comment
243+
Rails.application.routes.draw do
244+
root 'root#index'
245+
end
246+
EOS
247+
)
248+
181249
AnnotateRoutes.remove_annotations
182250
end
183251
end

0 commit comments

Comments
 (0)