Skip to content

Fix messages from AnnotateRoutes #737

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
12 changes: 6 additions & 6 deletions lib/annotate/annotate_routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ def do_annotations(options = {})
new_text = new_content.join("\n")

if rewrite_contents(existing_text, new_text)
puts "#{routes_file} annotated."
puts "#{routes_file} was annotated."
else
puts "#{routes_file} unchanged."
puts "#{routes_file} was not changed."
end
else
puts "Can't find routes.rb"
puts "#{routes_file} could not be found."
end
end

Expand All @@ -51,12 +51,12 @@ def remove_annotations(_options={})
new_content = strip_on_removal(content, header_position)
new_text = new_content.join("\n")
if rewrite_contents(existing_text, new_text)
puts "Removed annotations from #{routes_file}."
puts "Annotations were removed from #{routes_file}."
else
puts "#{routes_file} unchanged."
puts "#{routes_file} was not changed (Annotation did not exist)."
end
else
puts "Can't find routes.rb"
puts "#{routes_file} could not be found."
end
end

Expand Down
32 changes: 17 additions & 15 deletions spec/lib/annotate/annotate_routes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@

describe AnnotateRoutes do
ROUTE_FILE = 'config/routes.rb'.freeze
ANNOTATION_ADDED = "#{ROUTE_FILE} annotated.".freeze
ANNOTATION_REMOVED = "Removed annotations from #{ROUTE_FILE}.".freeze
FILE_UNCHANGED = "#{ROUTE_FILE} unchanged.".freeze

MESSAGE_ANNOTATED = "#{ROUTE_FILE} was annotated.".freeze
MESSAGE_UNCHANGED = "#{ROUTE_FILE} was not changed.".freeze
MESSAGE_NOT_FOUND = "#{ROUTE_FILE} could not be found.".freeze
MESSAGE_REMOVED = "Annotations were removed from #{ROUTE_FILE}.".freeze

MAGIC_COMMENTS = [
'# encoding: UTF-8',
Expand All @@ -31,7 +33,7 @@

it 'should check if routes.rb exists' do
expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(false)
expect(AnnotateRoutes).to receive(:puts).with("Can't find routes.rb")
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_NOT_FOUND)
AnnotateRoutes.do_annotations
end

Expand All @@ -50,7 +52,7 @@

expect(File).to receive(:read).with(ROUTE_FILE).and_return("").at_least(:once)

expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_ADDED).at_least(:once)
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).at_least(:once)
end

context 'without magic comments' do
Expand Down Expand Up @@ -202,7 +204,7 @@
expect(File).to receive(:read).with(ROUTE_FILE).and_return("")
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file)
expect(mock_file).to receive(:puts).with("\n# == Route Map\n#\n")
expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_ADDED)
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED)

AnnotateRoutes.do_annotations
end
Expand All @@ -211,7 +213,7 @@
expect(File).to receive(:read).with(ROUTE_FILE).and_return("")
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file)
expect(mock_file).to receive(:puts).with("\n# == Route Map\n#\n")
expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_ADDED)
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED)

AnnotateRoutes.do_annotations(ignore_routes: 'my_route')
end
Expand All @@ -220,14 +222,14 @@
expect(File).to receive(:read).with(ROUTE_FILE).and_return("")
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file)
expect(mock_file).to receive(:puts).with("# == Route Map\n#\n")
expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_ADDED)
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED)

AnnotateRoutes.do_annotations(position_in_routes: 'top')
end

it 'should skip annotations if file does already contain annotation' do
expect(File).to receive(:read).with(ROUTE_FILE).and_return("\n# == Route Map\n#\n")
expect(AnnotateRoutes).to receive(:puts).with(FILE_UNCHANGED)
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_UNCHANGED)

AnnotateRoutes.do_annotations
end
Expand All @@ -240,7 +242,7 @@
MAGIC_COMMENTS.each do |magic_comment|
expect(File).to receive(:read).with(ROUTE_FILE).and_return("#{magic_comment}\nSomething")
expect(mock_file).to receive(:puts).with("#{magic_comment}\n\n# == Route Map\n#\n\nSomething\n")
expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_ADDED)
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED)
AnnotateRoutes.do_annotations(position_in_routes: 'top')
end
end
Expand All @@ -252,7 +254,7 @@
MAGIC_COMMENTS.each do |magic_comment|
expect(File).to receive(:read).with(ROUTE_FILE).and_return("#{magic_comment}\nSomething")
expect(mock_file).to receive(:puts).with("#{magic_comment}\nSomething\n\n# == Route Map\n#\n")
expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_ADDED)
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED)
AnnotateRoutes.do_annotations(position_in_routes: 'bottom')
end
end
Expand All @@ -261,7 +263,7 @@
MAGIC_COMMENTS.each do |magic_comment|
expect(File).to receive(:read).with(ROUTE_FILE)
.and_return("#{magic_comment}\n\n# == Route Map\n#\n")
expect(AnnotateRoutes).to receive(:puts).with(FILE_UNCHANGED)
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_UNCHANGED)

AnnotateRoutes.do_annotations
end
Expand All @@ -274,7 +276,7 @@
expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true)
expect(AnnotateRoutes).to receive(:`).with('rake routes').and_return("(in /bad/line)\ngood line")
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file)
expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_ADDED)
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED)
end

it 'should annotate and add a newline!' do
Expand All @@ -295,7 +297,7 @@
expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true)
expect(AnnotateRoutes).to receive(:`).with('rake routes').and_return("another good line\ngood line")
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file)
expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_ADDED)
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED)
end

it 'should annotate and add a newline!' do
Expand All @@ -321,7 +323,7 @@
before(:each) do
expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true)
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file)
expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_REMOVED)
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_REMOVED)
end

it 'should remove trailing annotation and trim trailing newlines, but leave leading newlines alone' do
Expand Down