Commit bd305993 by Shu Fujita Committed by Andrew W. Lee

Refactor RSpec for AnnotateRoutes (1) (#736)

I refactored RSpec file for another large scale refactor in another PR.
parent e2bbbd99
...@@ -7,12 +7,7 @@ describe AnnotateRoutes do ...@@ -7,12 +7,7 @@ describe AnnotateRoutes do
ANNOTATION_REMOVED = "Removed annotations from #{ROUTE_FILE}.".freeze ANNOTATION_REMOVED = "Removed annotations from #{ROUTE_FILE}.".freeze
FILE_UNCHANGED = "#{ROUTE_FILE} unchanged.".freeze FILE_UNCHANGED = "#{ROUTE_FILE} unchanged.".freeze
def mock_file(stubs = {}) MAGIC_COMMENTS = [
@mock_file ||= double(File, stubs)
end
def magic_comments_list_each
[
'# encoding: UTF-8', '# encoding: UTF-8',
'# coding: UTF-8', '# coding: UTF-8',
'# -*- coding: UTF-8 -*-', '# -*- coding: UTF-8 -*-',
...@@ -24,7 +19,14 @@ describe AnnotateRoutes do ...@@ -24,7 +19,14 @@ describe AnnotateRoutes do
'# frozen_string_literal: true', '# frozen_string_literal: true',
'#frozen_string_literal: false', '#frozen_string_literal: false',
'# -*- frozen_string_literal : true -*-' '# -*- frozen_string_literal : true -*-'
].each { |magic_comment| yield magic_comment } ].freeze
let :stubs do
{}
end
let :mock_file do
double(File, stubs)
end end
it 'should check if routes.rb exists' do it 'should check if routes.rb exists' do
...@@ -34,11 +36,13 @@ describe AnnotateRoutes do ...@@ -34,11 +36,13 @@ describe AnnotateRoutes do
end end
describe 'Annotate#example' do describe 'Annotate#example' do
let(:rake_routes_content) do let :rake_routes_result do
" Prefix Verb URI Pattern Controller#Action <<-EOS
Prefix Verb URI Pattern Controller#Action
myaction1 GET /url1(.:format) mycontroller1#action myaction1 GET /url1(.:format) mycontroller1#action
myaction2 POST /url2(.:format) mycontroller2#action myaction2 POST /url2(.:format) mycontroller2#action
myaction3 DELETE|GET /url3(.:format) mycontroller3#action\n" myaction3 DELETE|GET /url3(.:format) mycontroller3#action
EOS
end end
before(:each) do before(:each) do
...@@ -51,47 +55,59 @@ describe AnnotateRoutes do ...@@ -51,47 +55,59 @@ describe AnnotateRoutes do
context 'without magic comments' do context 'without magic comments' do
before(:each) do before(:each) do
expect(AnnotateRoutes).to receive(:`).with('rake routes').and_return(rake_routes_content) expect(AnnotateRoutes).to receive(:`).with('rake routes').and_return(rake_routes_result)
end end
it 'annotate normal' do it 'annotate normal' do
expected_result = <<~EOS
# == Route Map
#
# Prefix Verb URI Pattern Controller#Action
# myaction1 GET /url1(.:format) mycontroller1#action
# myaction2 POST /url2(.:format) mycontroller2#action
# myaction3 DELETE|GET /url3(.:format) mycontroller3#action
EOS
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file)
expect(@mock_file).to receive(:puts).with(" expect(mock_file).to receive(:puts).with(expected_result)
# == Route Map
#
# Prefix Verb URI Pattern Controller#Action
# myaction1 GET /url1(.:format) mycontroller1#action
# myaction2 POST /url2(.:format) mycontroller2#action
# myaction3 DELETE|GET /url3(.:format) mycontroller3#action\n")
AnnotateRoutes.do_annotations AnnotateRoutes.do_annotations
end end
it 'annotate markdown' do it 'annotate markdown' do
expected_result = <<~EOS
# ## Route Map
#
# Prefix | Verb | URI Pattern | Controller#Action
# --------- | ---------- | --------------- | --------------------
# myaction1 | GET | /url1(.:format) | mycontroller1#action
# myaction2 | POST | /url2(.:format) | mycontroller2#action
# myaction3 | DELETE-GET | /url3(.:format) | mycontroller3#action
EOS
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file)
expect(@mock_file).to receive(:puts).with(" expect(mock_file).to receive(:puts).with(expected_result)
# ## Route Map
#
# Prefix | Verb | URI Pattern | Controller#Action
# --------- | ---------- | --------------- | --------------------
# myaction1 | GET | /url1(.:format) | mycontroller1#action
# myaction2 | POST | /url2(.:format) | mycontroller2#action
# myaction3 | DELETE-GET | /url3(.:format) | mycontroller3#action\n")
AnnotateRoutes.do_annotations(format_markdown: true) AnnotateRoutes.do_annotations(format_markdown: true)
end end
it 'wraps annotation if wrapper is specified' do it 'wraps annotation if wrapper is specified' do
expected_result = <<~EOS
# START
# == Route Map
#
# Prefix Verb URI Pattern Controller#Action
# myaction1 GET /url1(.:format) mycontroller1#action
# myaction2 POST /url2(.:format) mycontroller2#action
# myaction3 DELETE|GET /url3(.:format) mycontroller3#action
# END
EOS
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file)
expect(@mock_file).to receive(:puts).with(" expect(mock_file).to receive(:puts).with(expected_result)
# START
# == Route Map
#
# Prefix Verb URI Pattern Controller#Action
# myaction1 GET /url1(.:format) mycontroller1#action
# myaction2 POST /url2(.:format) mycontroller2#action
# myaction3 DELETE|GET /url3(.:format) mycontroller3#action
# END\n")
AnnotateRoutes.do_annotations(wrapper_open: 'START', wrapper_close: 'END') AnnotateRoutes.do_annotations(wrapper_open: 'START', wrapper_close: 'END')
end end
...@@ -99,62 +115,74 @@ describe AnnotateRoutes do ...@@ -99,62 +115,74 @@ describe AnnotateRoutes do
context 'file with magic comments' do context 'file with magic comments' do
it 'should not remove magic comments' do it 'should not remove magic comments' do
magic_comments_list_each do |magic_comment| MAGIC_COMMENTS.each do |magic_comment|
expected_result = <<~EOS
#{magic_comment}
# == Route Map
#
# Prefix Verb URI Pattern Controller#Action
# myaction1 GET /url1(.:format) mycontroller1#action
# myaction2 POST /url2(.:format) mycontroller2#action
# myaction3 DELETE|GET /url3(.:format) mycontroller3#action
EOS
expect(AnnotateRoutes).to receive(:`).with('rake routes') expect(AnnotateRoutes).to receive(:`).with('rake routes')
.and_return("#{magic_comment}\n#{rake_routes_content}") .and_return("#{magic_comment}\n#{rake_routes_result}")
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file)
expect(@mock_file).to receive(:puts).with(" expect(mock_file).to receive(:puts).with(expected_result)
#{magic_comment}
# == Route Map
#
# Prefix Verb URI Pattern Controller#Action
# myaction1 GET /url1(.:format) mycontroller1#action
# myaction2 POST /url2(.:format) mycontroller2#action
# myaction3 DELETE|GET /url3(.:format) mycontroller3#action\n")
AnnotateRoutes.do_annotations AnnotateRoutes.do_annotations
end end
end end
it 'annotate markdown' do it 'annotate markdown' do
magic_comments_list_each do |magic_comment| MAGIC_COMMENTS.each do |magic_comment|
expected_result = <<~EOS
#{magic_comment}
# ## Route Map
#
# Prefix | Verb | URI Pattern | Controller#Action
# --------- | ---------- | --------------- | --------------------
# myaction1 | GET | /url1(.:format) | mycontroller1#action
# myaction2 | POST | /url2(.:format) | mycontroller2#action
# myaction3 | DELETE-GET | /url3(.:format) | mycontroller3#action
EOS
expect(AnnotateRoutes).to receive(:`).with('rake routes') expect(AnnotateRoutes).to receive(:`).with('rake routes')
.and_return("#{magic_comment}\n#{rake_routes_content}") .and_return("#{magic_comment}\n#{rake_routes_result}")
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file)
expect(@mock_file).to receive(:puts).with(" expect(mock_file).to receive(:puts).with(expected_result)
#{magic_comment}
# ## Route Map
#
# Prefix | Verb | URI Pattern | Controller#Action
# --------- | ---------- | --------------- | --------------------
# myaction1 | GET | /url1(.:format) | mycontroller1#action
# myaction2 | POST | /url2(.:format) | mycontroller2#action
# myaction3 | DELETE-GET | /url3(.:format) | mycontroller3#action\n")
AnnotateRoutes.do_annotations(format_markdown: true) AnnotateRoutes.do_annotations(format_markdown: true)
end end
end end
it 'wraps annotation if wrapper is specified' do it 'wraps annotation if wrapper is specified' do
magic_comments_list_each do |magic_comment| MAGIC_COMMENTS.each do |magic_comment|
expected_result = <<~EOS
#{magic_comment}
# START
# == Route Map
#
# Prefix Verb URI Pattern Controller#Action
# myaction1 GET /url1(.:format) mycontroller1#action
# myaction2 POST /url2(.:format) mycontroller2#action
# myaction3 DELETE|GET /url3(.:format) mycontroller3#action
# END
EOS
expect(AnnotateRoutes).to receive(:`).with('rake routes') expect(AnnotateRoutes).to receive(:`).with('rake routes')
.and_return("#{magic_comment}\n#{rake_routes_content}") .and_return("#{magic_comment}\n#{rake_routes_result}")
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file)
expect(@mock_file).to receive(:puts).with(" expect(mock_file).to receive(:puts).with(expected_result)
#{magic_comment}
# START
# == Route Map
#
# Prefix Verb URI Pattern Controller#Action
# myaction1 GET /url1(.:format) mycontroller1#action
# myaction2 POST /url2(.:format) mycontroller2#action
# myaction3 DELETE|GET /url3(.:format) mycontroller3#action
# END\n")
AnnotateRoutes.do_annotations(wrapper_open: 'START', wrapper_close: 'END') AnnotateRoutes.do_annotations(wrapper_open: 'START', wrapper_close: 'END')
end end
...@@ -173,7 +201,7 @@ describe AnnotateRoutes do ...@@ -173,7 +201,7 @@ describe AnnotateRoutes do
it 'should insert annotations if file does not contain annotations' do it 'should insert annotations if file does not contain annotations' do
expect(File).to receive(:read).with(ROUTE_FILE).and_return("") expect(File).to receive(:read).with(ROUTE_FILE).and_return("")
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) 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(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(ANNOTATION_ADDED)
AnnotateRoutes.do_annotations AnnotateRoutes.do_annotations
...@@ -182,7 +210,7 @@ describe AnnotateRoutes do ...@@ -182,7 +210,7 @@ describe AnnotateRoutes do
it 'should insert annotations if file does not contain annotations and ignore routes' do it 'should insert annotations if file does not contain annotations and ignore routes' do
expect(File).to receive(:read).with(ROUTE_FILE).and_return("") expect(File).to receive(:read).with(ROUTE_FILE).and_return("")
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) 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(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(ANNOTATION_ADDED)
AnnotateRoutes.do_annotations(ignore_routes: 'my_route') AnnotateRoutes.do_annotations(ignore_routes: 'my_route')
...@@ -191,7 +219,7 @@ describe AnnotateRoutes do ...@@ -191,7 +219,7 @@ describe AnnotateRoutes do
it 'should insert annotations if file does not contain annotations and position top' do it 'should insert annotations if file does not contain annotations and position top' do
expect(File).to receive(:read).with(ROUTE_FILE).and_return("") expect(File).to receive(:read).with(ROUTE_FILE).and_return("")
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) 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(mock_file).to receive(:puts).with("# == Route Map\n#\n")
expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_ADDED) expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_ADDED)
AnnotateRoutes.do_annotations(position_in_routes: 'top') AnnotateRoutes.do_annotations(position_in_routes: 'top')
...@@ -209,9 +237,9 @@ describe AnnotateRoutes do ...@@ -209,9 +237,9 @@ describe AnnotateRoutes do
expect(File).to receive(:open).with(ROUTE_FILE, 'wb') expect(File).to receive(:open).with(ROUTE_FILE, 'wb')
.and_yield(mock_file).at_least(:once) .and_yield(mock_file).at_least(:once)
magic_comments_list_each do |magic_comment| MAGIC_COMMENTS.each do |magic_comment|
expect(File).to receive(:read).with(ROUTE_FILE).and_return("#{magic_comment}\nSomething") 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(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(ANNOTATION_ADDED)
AnnotateRoutes.do_annotations(position_in_routes: 'top') AnnotateRoutes.do_annotations(position_in_routes: 'top')
end end
...@@ -221,16 +249,16 @@ describe AnnotateRoutes do ...@@ -221,16 +249,16 @@ describe AnnotateRoutes do
expect(File).to receive(:open).with(ROUTE_FILE, 'wb') expect(File).to receive(:open).with(ROUTE_FILE, 'wb')
.and_yield(mock_file).at_least(:once) .and_yield(mock_file).at_least(:once)
magic_comments_list_each do |magic_comment| MAGIC_COMMENTS.each do |magic_comment|
expect(File).to receive(:read).with(ROUTE_FILE).and_return("#{magic_comment}\nSomething") 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(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(ANNOTATION_ADDED)
AnnotateRoutes.do_annotations(position_in_routes: 'bottom') AnnotateRoutes.do_annotations(position_in_routes: 'bottom')
end end
end end
it 'skips annotations if file does already contain annotation' do it 'skips annotations if file does already contain annotation' do
magic_comments_list_each do |magic_comment| MAGIC_COMMENTS.each do |magic_comment|
expect(File).to receive(:read).with(ROUTE_FILE) expect(File).to receive(:read).with(ROUTE_FILE)
.and_return("#{magic_comment}\n\n# == Route Map\n#\n") .and_return("#{magic_comment}\n\n# == Route Map\n#\n")
expect(AnnotateRoutes).to receive(:puts).with(FILE_UNCHANGED) expect(AnnotateRoutes).to receive(:puts).with(FILE_UNCHANGED)
...@@ -251,13 +279,13 @@ describe AnnotateRoutes do ...@@ -251,13 +279,13 @@ describe AnnotateRoutes do
it 'should annotate and add a newline!' do it 'should annotate and add a newline!' do
expect(File).to receive(:read).with(ROUTE_FILE).and_return("ActionController::Routing...\nfoo") expect(File).to receive(:read).with(ROUTE_FILE).and_return("ActionController::Routing...\nfoo")
expect(@mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map\n#\n# good line\n/) expect(mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map\n#\n# good line\n/)
AnnotateRoutes.do_annotations AnnotateRoutes.do_annotations
end end
it 'should not add a newline if there are empty lines' do it 'should not add a newline if there are empty lines' do
expect(File).to receive(:read).with(ROUTE_FILE).and_return("ActionController::Routing...\nfoo\n") expect(File).to receive(:read).with(ROUTE_FILE).and_return("ActionController::Routing...\nfoo\n")
expect(@mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map\n#\n# good line\n/) expect(mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map\n#\n# good line\n/)
AnnotateRoutes.do_annotations AnnotateRoutes.do_annotations
end end
end end
...@@ -272,19 +300,19 @@ describe AnnotateRoutes do ...@@ -272,19 +300,19 @@ describe AnnotateRoutes do
it 'should annotate and add a newline!' do it 'should annotate and add a newline!' do
expect(File).to receive(:read).with(ROUTE_FILE).and_return("ActionController::Routing...\nfoo") expect(File).to receive(:read).with(ROUTE_FILE).and_return("ActionController::Routing...\nfoo")
expect(@mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map\n#\n# another good line\n# good line\n/) expect(mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map\n#\n# another good line\n# good line\n/)
AnnotateRoutes.do_annotations AnnotateRoutes.do_annotations
end end
it 'should not add a newline if there are empty lines' do it 'should not add a newline if there are empty lines' do
expect(File).to receive(:read).with(ROUTE_FILE).and_return("ActionController::Routing...\nfoo\n") expect(File).to receive(:read).with(ROUTE_FILE).and_return("ActionController::Routing...\nfoo\n")
expect(@mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map\n#\n# another good line\n# good line\n/) expect(mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map\n#\n# another good line\n# good line\n/)
AnnotateRoutes.do_annotations AnnotateRoutes.do_annotations
end end
it 'should add a timestamp when :timestamp is passed' do it 'should add a timestamp when :timestamp is passed' do
expect(File).to receive(:read).with(ROUTE_FILE).and_return("ActionController::Routing...\nfoo") expect(File).to receive(:read).with(ROUTE_FILE).and_return("ActionController::Routing...\nfoo")
expect(@mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map \(Updated \d{4}-\d{2}-\d{2} \d{2}:\d{2}\)\n#\n# another good line\n# good line\n/) expect(mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map \(Updated \d{4}-\d{2}-\d{2} \d{2}:\d{2}\)\n#\n# another good line\n# good line\n/)
AnnotateRoutes.do_annotations timestamp: true AnnotateRoutes.do_annotations timestamp: true
end end
end end
...@@ -297,7 +325,7 @@ describe AnnotateRoutes do ...@@ -297,7 +325,7 @@ describe AnnotateRoutes do
end end
it 'should remove trailing annotation and trim trailing newlines, but leave leading newlines alone' do 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(<<-EOS route_file_content = <<~EOS
...@@ -310,20 +338,22 @@ describe AnnotateRoutes do ...@@ -310,20 +338,22 @@ describe AnnotateRoutes do
# another good line # another good line
# good line # good line
EOS EOS
)
expect(@mock_file).to receive(:puts).with(<<-EOS expected_result = <<~EOS
ActionController::Routing... ActionController::Routing...
foo foo
EOS EOS
)
expect(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content)
expect(mock_file).to receive(:puts).with(expected_result)
AnnotateRoutes.remove_annotations AnnotateRoutes.remove_annotations
end end
it 'should remove prepended annotation and trim leading newlines, but leave trailing newlines alone' do 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(<<-EOS route_file_content = <<~EOS
# == Route Map # == Route Map
# #
# another good line # another good line
...@@ -339,8 +369,8 @@ describe AnnotateRoutes do ...@@ -339,8 +369,8 @@ describe AnnotateRoutes do
EOS EOS
)
expect(@mock_file).to receive(:puts).with(<<-EOS expected_result = <<~EOS
Rails.application.routes.draw do Rails.application.routes.draw do
root 'root#index' root 'root#index'
end end
...@@ -348,12 +378,14 @@ describe AnnotateRoutes do ...@@ -348,12 +378,14 @@ describe AnnotateRoutes do
EOS EOS
)
expect(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content)
expect(mock_file).to receive(:puts).with(expected_result)
AnnotateRoutes.remove_annotations AnnotateRoutes.remove_annotations
end end
it 'should not remove custom comments above route map' do it 'should not remove custom comments above route map' do
expect(File).to receive(:read).with(ROUTE_FILE).and_return(<<-EOS route_file_content = <<~EOS
# My comment # My comment
# == Route Map # == Route Map
# #
...@@ -363,15 +395,16 @@ describe AnnotateRoutes do ...@@ -363,15 +395,16 @@ describe AnnotateRoutes do
root 'root#index' root 'root#index'
end end
EOS EOS
)
expect(@mock_file).to receive(:puts).with(<<-EOS expected_result = <<~EOS
# My comment # My comment
Rails.application.routes.draw do Rails.application.routes.draw do
root 'root#index' root 'root#index'
end end
EOS EOS
)
expect(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content)
expect(mock_file).to receive(:puts).with(expected_result)
AnnotateRoutes.remove_annotations AnnotateRoutes.remove_annotations
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment