From e2c79e04f3dd592618bd3bd433d9290ea4b534b8 Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Sun, 2 Feb 2020 15:00:34 +0900 Subject: [PATCH 01/19] Unify describe clauses with magic comments --- spec/lib/annotate/annotate_routes_spec.rb | 162 +++++++++++----------- 1 file changed, 79 insertions(+), 83 deletions(-) diff --git a/spec/lib/annotate/annotate_routes_spec.rb b/spec/lib/annotate/annotate_routes_spec.rb index e4430e89c..ab7ef4a00 100644 --- a/spec/lib/annotate/annotate_routes_spec.rb +++ b/spec/lib/annotate/annotate_routes_spec.rb @@ -115,78 +115,76 @@ end end - context 'file with magic comments' do - it 'should not remove magic comments' do - MAGIC_COMMENTS.each do |magic_comment| - expected_result = <<~EOS + context 'When the file contains magic comments' do + MAGIC_COMMENTS.each do |magic_comment| + describe "magic comment: #{magic_comment.inspect}" do + it 'should not remove magic comments' do + expected_result = <<~EOS - #{magic_comment} + #{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 + # == 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') - .and_return("#{magic_comment}\n#{rake_routes_result}") + expect(AnnotateRoutes).to receive(:`).with('rake routes') + .and_return("#{magic_comment}\n#{rake_routes_result}") - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) - expect(mock_file).to receive(:puts).with(expected_result) + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) + expect(mock_file).to receive(:puts).with(expected_result) - AnnotateRoutes.do_annotations - end - end + AnnotateRoutes.do_annotations + end - it 'annotate markdown' do - MAGIC_COMMENTS.each do |magic_comment| - expected_result = <<~EOS + it 'annotate markdown' do + expected_result = <<~EOS - #{magic_comment} + #{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 + # ## 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') - .and_return("#{magic_comment}\n#{rake_routes_result}") + expect(AnnotateRoutes).to receive(:`).with('rake routes') + .and_return("#{magic_comment}\n#{rake_routes_result}") - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) - expect(mock_file).to receive(:puts).with(expected_result) + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) + expect(mock_file).to receive(:puts).with(expected_result) - AnnotateRoutes.do_annotations(format_markdown: true) - end - end + AnnotateRoutes.do_annotations(format_markdown: true) + end - it 'wraps annotation if wrapper is specified' do - MAGIC_COMMENTS.each do |magic_comment| - expected_result = <<~EOS + it 'wraps annotation if wrapper is specified' do + expected_result = <<~EOS - #{magic_comment} + #{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 + # 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') - .and_return("#{magic_comment}\n#{rake_routes_result}") - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) - expect(mock_file).to receive(:puts).with(expected_result) + expect(AnnotateRoutes).to receive(:`).with('rake routes') + .and_return("#{magic_comment}\n#{rake_routes_result}") + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) + expect(mock_file).to receive(:puts).with(expected_result) - AnnotateRoutes.do_annotations(wrapper_open: 'START', wrapper_close: 'END') + AnnotateRoutes.do_annotations(wrapper_open: 'START', wrapper_close: 'END') + end end end end @@ -234,38 +232,36 @@ AnnotateRoutes.do_annotations end - context 'file with magic comments' do - it 'leaves magic comment on top, adds an empty line between magic comment and annotation (position_in_routes :top)' do - expect(File).to receive(:open).with(ROUTE_FILE, 'wb') - .and_yield(mock_file).at_least(:once) + context 'When the file contains magic comments' do + MAGIC_COMMENTS.each do |magic_comment| + describe "magic comment: #{magic_comment.inspect}" do + it 'leaves magic comment on top, adds an empty line between magic comment and annotation (position_in_routes :top)' do + expect(File).to receive(:open).with(ROUTE_FILE, 'wb') + .and_yield(mock_file).at_least(:once) - 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(MESSAGE_ANNOTATED) - AnnotateRoutes.do_annotations(position_in_routes: 'top') - end - end + 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(MESSAGE_ANNOTATED) + AnnotateRoutes.do_annotations(position_in_routes: 'top') + end - it 'leaves magic comment on top, adds an empty line between magic comment and annotation (position_in_routes :bottom)' do - expect(File).to receive(:open).with(ROUTE_FILE, 'wb') - .and_yield(mock_file).at_least(:once) + it 'leaves magic comment on top, adds an empty line between magic comment and annotation (position_in_routes :bottom)' do + expect(File).to receive(:open).with(ROUTE_FILE, 'wb') + .and_yield(mock_file).at_least(:once) - 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(MESSAGE_ANNOTATED) - AnnotateRoutes.do_annotations(position_in_routes: 'bottom') - end - end + 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(MESSAGE_ANNOTATED) + AnnotateRoutes.do_annotations(position_in_routes: 'bottom') + end - it 'skips annotations if file does already contain annotation' do - 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(MESSAGE_UNCHANGED) + it 'skips annotations if file does already contain annotation' do + expect(File).to receive(:read).with(ROUTE_FILE) + .and_return("#{magic_comment}\n\n# == Route Map\n#\n") + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_UNCHANGED) - AnnotateRoutes.do_annotations + AnnotateRoutes.do_annotations + end end end end From c80d3ea4c0a84d6517c10cc8424a4879d4571656 Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Sun, 2 Feb 2020 16:33:04 +0900 Subject: [PATCH 02/19] Make purpose of test cases more clear using describe and rewrite content in 'it' --- spec/lib/annotate/annotate_routes_spec.rb | 186 ++++++++++++---------- 1 file changed, 100 insertions(+), 86 deletions(-) diff --git a/spec/lib/annotate/annotate_routes_spec.rb b/spec/lib/annotate/annotate_routes_spec.rb index ab7ef4a00..68970d2f9 100644 --- a/spec/lib/annotate/annotate_routes_spec.rb +++ b/spec/lib/annotate/annotate_routes_spec.rb @@ -118,72 +118,80 @@ context 'When the file contains magic comments' do MAGIC_COMMENTS.each do |magic_comment| describe "magic comment: #{magic_comment.inspect}" do - it 'should not remove magic comments' do - 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') - .and_return("#{magic_comment}\n#{rake_routes_result}") - - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) - expect(mock_file).to receive(:puts).with(expected_result) - - AnnotateRoutes.do_annotations - end - - it 'annotate markdown' do - 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') - .and_return("#{magic_comment}\n#{rake_routes_result}") - - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) - expect(mock_file).to receive(:puts).with(expected_result) - - AnnotateRoutes.do_annotations(format_markdown: true) - end - - it 'wraps annotation if wrapper is specified' do - 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') - .and_return("#{magic_comment}\n#{rake_routes_result}") - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) - expect(mock_file).to receive(:puts).with(expected_result) - - AnnotateRoutes.do_annotations(wrapper_open: 'START', wrapper_close: 'END') + context 'When the file does not contain annotation yet' do + context 'When no option is passed' do + it 'annotates normally' do + 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') + .and_return("#{magic_comment}\n#{rake_routes_result}") + + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) + expect(mock_file).to receive(:puts).with(expected_result) + + AnnotateRoutes.do_annotations + end + end + + context 'When the option "format_markdown" is passed' do + it 'annotates in Markdown format' do + 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') + .and_return("#{magic_comment}\n#{rake_routes_result}") + + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) + expect(mock_file).to receive(:puts).with(expected_result) + + AnnotateRoutes.do_annotations(format_markdown: true) + end + end + + context 'When the options "wrapper_open" and "wrapper_close" are passed' do + it 'annotates and wraps annotation with specified words' do + 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') + .and_return("#{magic_comment}\n#{rake_routes_result}") + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) + expect(mock_file).to receive(:puts).with(expected_result) + + AnnotateRoutes.do_annotations(wrapper_open: 'START', wrapper_close: 'END') + end + end end end end @@ -235,32 +243,38 @@ context 'When the file contains magic comments' do MAGIC_COMMENTS.each do |magic_comment| describe "magic comment: #{magic_comment.inspect}" do - it 'leaves magic comment on top, adds an empty line between magic comment and annotation (position_in_routes :top)' do - expect(File).to receive(:open).with(ROUTE_FILE, 'wb') - .and_yield(mock_file).at_least(:once) - - 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(MESSAGE_ANNOTATED) - AnnotateRoutes.do_annotations(position_in_routes: 'top') + context 'When the option "position_in_routes" is specified as "top"' do + it 'leaves magic comment on top and adds an empty line between magic comment and annotation' do + expect(File).to receive(:open).with(ROUTE_FILE, 'wb') + .and_yield(mock_file).at_least(:once) + + 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(MESSAGE_ANNOTATED) + AnnotateRoutes.do_annotations(position_in_routes: 'top') + end end - it 'leaves magic comment on top, adds an empty line between magic comment and annotation (position_in_routes :bottom)' do - expect(File).to receive(:open).with(ROUTE_FILE, 'wb') - .and_yield(mock_file).at_least(:once) + context 'When the option "position_in_routes" is specified as "bottom"' do + it 'leaves magic comment on top and adds an empty line between magic comment and annotation' do + expect(File).to receive(:open).with(ROUTE_FILE, 'wb') + .and_yield(mock_file).at_least(:once) - 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(MESSAGE_ANNOTATED) - AnnotateRoutes.do_annotations(position_in_routes: 'bottom') + 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(MESSAGE_ANNOTATED) + AnnotateRoutes.do_annotations(position_in_routes: 'bottom') + end end - it 'skips annotations if file does already contain annotation' do - expect(File).to receive(:read).with(ROUTE_FILE) - .and_return("#{magic_comment}\n\n# == Route Map\n#\n") - expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_UNCHANGED) + context 'When the file already contains annotation' do + it 'skips annotations' do + expect(File).to receive(:read).with(ROUTE_FILE) + .and_return("#{magic_comment}\n\n# == Route Map\n#\n") + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_UNCHANGED) - AnnotateRoutes.do_annotations + AnnotateRoutes.do_annotations + end end end end From 93121f01a48dfcd537271d44aab23c78ff4193cd Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Sun, 2 Feb 2020 17:10:46 +0900 Subject: [PATCH 03/19] Define variables using let --- spec/lib/annotate/annotate_routes_spec.rb | 81 +++++++++++++++++++---- 1 file changed, 67 insertions(+), 14 deletions(-) diff --git a/spec/lib/annotate/annotate_routes_spec.rb b/spec/lib/annotate/annotate_routes_spec.rb index 68970d2f9..4baaef559 100644 --- a/spec/lib/annotate/annotate_routes_spec.rb +++ b/spec/lib/annotate/annotate_routes_spec.rb @@ -118,10 +118,20 @@ context 'When the file contains magic comments' do MAGIC_COMMENTS.each do |magic_comment| describe "magic comment: #{magic_comment.inspect}" do + let :rake_routes_result do + <<~EOS + #{magic_comment} + 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 + end + context 'When the file does not contain annotation yet' do context 'When no option is passed' do - it 'annotates normally' do - expected_result = <<~EOS + let :expected_result do + <<~EOS #{magic_comment} @@ -132,9 +142,11 @@ # myaction2 POST /url2(.:format) mycontroller2#action # myaction3 DELETE|GET /url3(.:format) mycontroller3#action EOS + end + it 'annotates normally' do expect(AnnotateRoutes).to receive(:`).with('rake routes') - .and_return("#{magic_comment}\n#{rake_routes_result}") + .and_return(rake_routes_result) expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) expect(mock_file).to receive(:puts).with(expected_result) @@ -144,8 +156,8 @@ end context 'When the option "format_markdown" is passed' do - it 'annotates in Markdown format' do - expected_result = <<~EOS + let :expected_result do + <<~EOS #{magic_comment} @@ -157,9 +169,11 @@ # myaction2 | POST | /url2(.:format) | mycontroller2#action # myaction3 | DELETE-GET | /url3(.:format) | mycontroller3#action EOS + end + it 'annotates in Markdown format' do expect(AnnotateRoutes).to receive(:`).with('rake routes') - .and_return("#{magic_comment}\n#{rake_routes_result}") + .and_return(rake_routes_result) expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) expect(mock_file).to receive(:puts).with(expected_result) @@ -169,8 +183,8 @@ end context 'When the options "wrapper_open" and "wrapper_close" are passed' do - it 'annotates and wraps annotation with specified words' do - expected_result = <<~EOS + let :expected_result do + <<~EOS #{magic_comment} @@ -183,9 +197,11 @@ # myaction3 DELETE|GET /url3(.:format) mycontroller3#action # END EOS + end + it 'annotates and wraps annotation with specified words' do expect(AnnotateRoutes).to receive(:`).with('rake routes') - .and_return("#{magic_comment}\n#{rake_routes_result}") + .and_return(rake_routes_result) expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) expect(mock_file).to receive(:puts).with(expected_result) @@ -243,34 +259,71 @@ context 'When the file contains magic comments' do MAGIC_COMMENTS.each do |magic_comment| describe "magic comment: #{magic_comment.inspect}" do + let :route_file_content do + <<~EOS + #{magic_comment} + Something + EOS + end + context 'When the option "position_in_routes" is specified as "top"' do + let :expected_result do + <<~EOS + #{magic_comment} + + # == Route Map + # + + Something + EOS + end + it 'leaves magic comment on top and adds an empty line between magic comment and annotation' do expect(File).to receive(:open).with(ROUTE_FILE, 'wb') .and_yield(mock_file).at_least(:once) - 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(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content) + expect(mock_file).to receive(:puts).with(expected_result) expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED) AnnotateRoutes.do_annotations(position_in_routes: 'top') end end context 'When the option "position_in_routes" is specified as "bottom"' do + let :expected_result do + <<~EOS + #{magic_comment} + Something + + # == Route Map + # + EOS + end + it 'leaves magic comment on top and adds an empty line between magic comment and annotation' do expect(File).to receive(:open).with(ROUTE_FILE, 'wb') .and_yield(mock_file).at_least(:once) - 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(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content) + expect(mock_file).to receive(:puts).with(expected_result) expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED) AnnotateRoutes.do_annotations(position_in_routes: 'bottom') end end context 'When the file already contains annotation' do + let :route_file_content do + <<~EOS + #{magic_comment} + + # == Route Map + # + EOS + end + it 'skips annotations' do expect(File).to receive(:read).with(ROUTE_FILE) - .and_return("#{magic_comment}\n\n# == Route Map\n#\n") + .and_return(route_file_content) expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_UNCHANGED) AnnotateRoutes.do_annotations From e6f69d4ab8b356c0129065ddc4e30495194fc0e7 Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Sun, 2 Feb 2020 17:18:49 +0900 Subject: [PATCH 04/19] Refactor by before(:each) --- spec/lib/annotate/annotate_routes_spec.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/spec/lib/annotate/annotate_routes_spec.rb b/spec/lib/annotate/annotate_routes_spec.rb index 4baaef559..5a779d16a 100644 --- a/spec/lib/annotate/annotate_routes_spec.rb +++ b/spec/lib/annotate/annotate_routes_spec.rb @@ -266,6 +266,10 @@ EOS end + before :each do + expect(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content) + end + context 'When the option "position_in_routes" is specified as "top"' do let :expected_result do <<~EOS @@ -282,7 +286,6 @@ expect(File).to receive(:open).with(ROUTE_FILE, 'wb') .and_yield(mock_file).at_least(:once) - expect(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content) expect(mock_file).to receive(:puts).with(expected_result) expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED) AnnotateRoutes.do_annotations(position_in_routes: 'top') @@ -304,7 +307,6 @@ expect(File).to receive(:open).with(ROUTE_FILE, 'wb') .and_yield(mock_file).at_least(:once) - expect(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content) expect(mock_file).to receive(:puts).with(expected_result) expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED) AnnotateRoutes.do_annotations(position_in_routes: 'bottom') @@ -322,8 +324,6 @@ end it 'skips annotations' do - expect(File).to receive(:read).with(ROUTE_FILE) - .and_return(route_file_content) expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_UNCHANGED) AnnotateRoutes.do_annotations From 01db725fb2464312be76bb005ed64572d4e56d65 Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Sun, 2 Feb 2020 17:46:53 +0900 Subject: [PATCH 05/19] Make purpose of test cases more clear using describe and rewrite content in 'it' --- spec/lib/annotate/annotate_routes_spec.rb | 352 ++++++++++++---------- 1 file changed, 190 insertions(+), 162 deletions(-) diff --git a/spec/lib/annotate/annotate_routes_spec.rb b/spec/lib/annotate/annotate_routes_spec.rb index 5a779d16a..2abf583a7 100644 --- a/spec/lib/annotate/annotate_routes_spec.rb +++ b/spec/lib/annotate/annotate_routes_spec.rb @@ -31,181 +31,197 @@ double(File, stubs) end - 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(MESSAGE_NOT_FOUND) - AnnotateRoutes.do_annotations - end + describe '.do_annotations' do + context 'When "config/routes.rb" does not exist' do + before :each do + expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(false).once + end + + it 'does not annotates any file' do + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_NOT_FOUND) - describe 'Annotate#example' do - let :rake_routes_result do - <<-EOS + AnnotateRoutes.do_annotations + end + end + + context 'When "config/routes.rb" exists' do + let :rake_routes_result do + <<-EOS 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 - end - - before(:each) do - expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true).at_least(:once) + EOS + end - expect(File).to receive(:read).with(ROUTE_FILE).and_return("").at_least(:once) + before(:each) do + expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true).at_least(:once) - expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).at_least(:once) - end + expect(File).to receive(:read).with(ROUTE_FILE).and_return("").at_least(:once) - context 'without magic comments' do - before(:each) do - expect(AnnotateRoutes).to receive(:`).with('rake routes').and_return(rake_routes_result) + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).at_least(:once) end - it 'annotate normal' do - expected_result = <<~EOS + context 'When the file does not contain magic comment' do + before(:each) do + expect(AnnotateRoutes).to receive(:`).with('rake routes').and_return(rake_routes_result) + end - # == 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 + context 'When the file does not contain annotation yet' do + context 'When no option is passed' do + it 'annotates normally' do + expected_result = <<~EOS - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) - 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 + EOS - AnnotateRoutes.do_annotations - end + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) + expect(mock_file).to receive(:puts).with(expected_result) - it 'annotate markdown' do - expected_result = <<~EOS + AnnotateRoutes.do_annotations + end + end - # ## 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 + context 'When the option "format_markdown" is passed' do + it 'annotates in Markdown format' do + expected_result = <<~EOS - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) - 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 + EOS - AnnotateRoutes.do_annotations(format_markdown: true) - end + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) + expect(mock_file).to receive(:puts).with(expected_result) - it 'wraps annotation if wrapper is specified' do - expected_result = <<~EOS + AnnotateRoutes.do_annotations(format_markdown: true) + end + end - # 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 + context 'When the options "wrapper_open" and "wrapper_close" are passed' do + it 'annotates and wraps annotation with specified words' do + expected_result = <<~EOS - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) - 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 + EOS - AnnotateRoutes.do_annotations(wrapper_open: 'START', wrapper_close: 'END') - end - end + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) + expect(mock_file).to receive(:puts).with(expected_result) - context 'When the file contains magic comments' do - MAGIC_COMMENTS.each do |magic_comment| - describe "magic comment: #{magic_comment.inspect}" do - let :rake_routes_result do - <<~EOS - #{magic_comment} - 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 + AnnotateRoutes.do_annotations(wrapper_open: 'START', wrapper_close: 'END') + end end + end + end - context 'When the file does not contain annotation yet' do - context 'When no option is passed' do - let :expected_result do - <<~EOS + context 'When the file contains magic comments' do + MAGIC_COMMENTS.each do |magic_comment| + describe "magic comment: #{magic_comment.inspect}" do + let :rake_routes_result do + <<~EOS + #{magic_comment} + 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 + end - #{magic_comment} + context 'When the file does not contain annotation yet' do + context 'When no option is passed' do + let :expected_result do + <<~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 - end + #{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 + end - it 'annotates normally' do - expect(AnnotateRoutes).to receive(:`).with('rake routes') - .and_return(rake_routes_result) + it 'annotates normally' do + expect(AnnotateRoutes).to receive(:`).with('rake routes') + .and_return(rake_routes_result) - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) - expect(mock_file).to receive(:puts).with(expected_result) + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) + expect(mock_file).to receive(:puts).with(expected_result) - AnnotateRoutes.do_annotations + AnnotateRoutes.do_annotations + end end - end - context 'When the option "format_markdown" is passed' do - let :expected_result do - <<~EOS + context 'When the option "format_markdown" is passed' do + let :expected_result do + <<~EOS - #{magic_comment} + #{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 - end + # ## 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 + end - it 'annotates in Markdown format' do - expect(AnnotateRoutes).to receive(:`).with('rake routes') - .and_return(rake_routes_result) + it 'annotates in Markdown format' do + expect(AnnotateRoutes).to receive(:`).with('rake routes') + .and_return(rake_routes_result) - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) - expect(mock_file).to receive(:puts).with(expected_result) + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) + expect(mock_file).to receive(:puts).with(expected_result) - AnnotateRoutes.do_annotations(format_markdown: true) + AnnotateRoutes.do_annotations(format_markdown: true) + end end - end - context 'When the options "wrapper_open" and "wrapper_close" are passed' do - let :expected_result do - <<~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 - end - - it 'annotates and wraps annotation with specified words' do - expect(AnnotateRoutes).to receive(:`).with('rake routes') - .and_return(rake_routes_result) - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) - expect(mock_file).to receive(:puts).with(expected_result) - - AnnotateRoutes.do_annotations(wrapper_open: 'START', wrapper_close: 'END') + context 'When the options "wrapper_open" and "wrapper_close" are passed' do + let :expected_result do + <<~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 + end + + it 'annotates and wraps annotation with specified words' do + expect(AnnotateRoutes).to receive(:`).with('rake routes') + .and_return(rake_routes_result) + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) + expect(mock_file).to receive(:puts).with(expected_result) + + AnnotateRoutes.do_annotations(wrapper_open: 'START', wrapper_close: 'END') + end end end end @@ -222,38 +238,50 @@ .and_return('').at_least(:once) end - 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(: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(MESSAGE_ANNOTATED) + context 'When the file does not contain magic comment' do + context 'When the file does not contain annotation yet' do + context 'When no option is specified' do + it 'inserts annotations' do + 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(MESSAGE_ANNOTATED) - AnnotateRoutes.do_annotations - end + AnnotateRoutes.do_annotations + end + end - 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(: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(MESSAGE_ANNOTATED) + context 'When the option "ignore_routes" is specified' do + it 'inserts annotations' do + 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(MESSAGE_ANNOTATED) - AnnotateRoutes.do_annotations(ignore_routes: 'my_route') - end + AnnotateRoutes.do_annotations(ignore_routes: 'my_route') + end + end - 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(: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(MESSAGE_ANNOTATED) + context 'When the option "position_in_routes" is specified as "top"' do + it 'inserts annotations' do + 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(MESSAGE_ANNOTATED) - AnnotateRoutes.do_annotations(position_in_routes: 'top') - end + AnnotateRoutes.do_annotations(position_in_routes: 'top') + end + end + 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(MESSAGE_UNCHANGED) + context 'When the file already contains annotation' do + 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(MESSAGE_UNCHANGED) - AnnotateRoutes.do_annotations + AnnotateRoutes.do_annotations + end + end end context 'When the file contains magic comments' do From 3142d5e75527a5806d9cc7010ef3fbb6f79777b2 Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Sun, 2 Feb 2020 17:53:21 +0900 Subject: [PATCH 06/19] Define variables using let --- spec/lib/annotate/annotate_routes_spec.rb | 71 ++++++++++++++++++----- 1 file changed, 56 insertions(+), 15 deletions(-) diff --git a/spec/lib/annotate/annotate_routes_spec.rb b/spec/lib/annotate/annotate_routes_spec.rb index 2abf583a7..3bfaf6ec8 100644 --- a/spec/lib/annotate/annotate_routes_spec.rb +++ b/spec/lib/annotate/annotate_routes_spec.rb @@ -54,10 +54,14 @@ EOS end + let :route_file_content do + '' + end + before(:each) do expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true).at_least(:once) - expect(File).to receive(:read).with(ROUTE_FILE).and_return("").at_least(:once) + expect(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content).at_least(:once) expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).at_least(:once) end @@ -69,8 +73,8 @@ context 'When the file does not contain annotation yet' do context 'When no option is passed' do - it 'annotates normally' do - expected_result = <<~EOS + let :expected_result do + <<~EOS # == Route Map # @@ -79,7 +83,9 @@ # myaction2 POST /url2(.:format) mycontroller2#action # myaction3 DELETE|GET /url3(.:format) mycontroller3#action EOS + end + it 'annotates normally' do expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) expect(mock_file).to receive(:puts).with(expected_result) @@ -88,8 +94,8 @@ end context 'When the option "format_markdown" is passed' do - it 'annotates in Markdown format' do - expected_result = <<~EOS + let :expected_result do + <<~EOS # ## Route Map # @@ -99,7 +105,9 @@ # myaction2 | POST | /url2(.:format) | mycontroller2#action # myaction3 | DELETE-GET | /url3(.:format) | mycontroller3#action EOS + end + it 'annotates in Markdown format' do expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) expect(mock_file).to receive(:puts).with(expected_result) @@ -108,8 +116,8 @@ end context 'When the options "wrapper_open" and "wrapper_close" are passed' do - it 'annotates and wraps annotation with specified words' do - expected_result = <<~EOS + let :expected_result do + <<~EOS # START # == Route Map @@ -120,7 +128,9 @@ # myaction3 DELETE|GET /url3(.:format) mycontroller3#action # END EOS + end + it 'annotates and wraps annotation with specified words' do expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) expect(mock_file).to receive(:puts).with(expected_result) @@ -235,16 +245,32 @@ expect(File).to receive(:exist?).with(ROUTE_FILE) .and_return(true).at_least(:once) expect(AnnotateRoutes).to receive(:`).with('rake routes') - .and_return('').at_least(:once) + .and_return(rake_routes_result).at_least(:once) + end + + let :rake_routes_result do + '' end context 'When the file does not contain magic comment' do context 'When the file does not contain annotation yet' do + let :expected_result do + <<~EOS + + # == Route Map + # + EOS + end + + let :route_file_content do + '' + end + context 'When no option is specified' do it 'inserts annotations' do - expect(File).to receive(:read).with(ROUTE_FILE).and_return("") + expect(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content) 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(expected_result) expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED) AnnotateRoutes.do_annotations @@ -253,9 +279,9 @@ context 'When the option "ignore_routes" is specified' do it 'inserts annotations' do - expect(File).to receive(:read).with(ROUTE_FILE).and_return("") + expect(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content) 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(expected_result) expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED) AnnotateRoutes.do_annotations(ignore_routes: 'my_route') @@ -263,10 +289,17 @@ end context 'When the option "position_in_routes" is specified as "top"' do + let :expected_result do + <<~EOS + # == Route Map + # + EOS + end + it 'inserts annotations' do - expect(File).to receive(:read).with(ROUTE_FILE).and_return("") + expect(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content) 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(expected_result) expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED) AnnotateRoutes.do_annotations(position_in_routes: 'top') @@ -275,8 +308,16 @@ end context 'When the file already contains annotation' do + let :route_file_content do + <<~EOS + + # == Route Map + # + EOS + 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(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content) expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_UNCHANGED) AnnotateRoutes.do_annotations From 3b3b061dba85bc4bb8fc4d5474d142c1f63a2847 Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Sun, 2 Feb 2020 19:38:57 +0900 Subject: [PATCH 07/19] Write precondition of result of `rake routes` in `before(:each)` --- spec/lib/annotate/annotate_routes_spec.rb | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/spec/lib/annotate/annotate_routes_spec.rb b/spec/lib/annotate/annotate_routes_spec.rb index 3bfaf6ec8..3bc14292b 100644 --- a/spec/lib/annotate/annotate_routes_spec.rb +++ b/spec/lib/annotate/annotate_routes_spec.rb @@ -63,14 +63,11 @@ expect(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content).at_least(:once) + expect(AnnotateRoutes).to receive(:`).with('rake routes').and_return(rake_routes_result) expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).at_least(:once) end context 'When the file does not contain magic comment' do - before(:each) do - expect(AnnotateRoutes).to receive(:`).with('rake routes').and_return(rake_routes_result) - end - context 'When the file does not contain annotation yet' do context 'When no option is passed' do let :expected_result do @@ -170,9 +167,6 @@ end it 'annotates normally' do - expect(AnnotateRoutes).to receive(:`).with('rake routes') - .and_return(rake_routes_result) - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) expect(mock_file).to receive(:puts).with(expected_result) @@ -197,9 +191,6 @@ end it 'annotates in Markdown format' do - expect(AnnotateRoutes).to receive(:`).with('rake routes') - .and_return(rake_routes_result) - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) expect(mock_file).to receive(:puts).with(expected_result) @@ -225,8 +216,6 @@ end it 'annotates and wraps annotation with specified words' do - expect(AnnotateRoutes).to receive(:`).with('rake routes') - .and_return(rake_routes_result) expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) expect(mock_file).to receive(:puts).with(expected_result) From 5758c75212c3d3489f8936e889fce3e59db96f3c Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Sun, 2 Feb 2020 19:43:06 +0900 Subject: [PATCH 08/19] Write test cases of message in 'it' block --- spec/lib/annotate/annotate_routes_spec.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/spec/lib/annotate/annotate_routes_spec.rb b/spec/lib/annotate/annotate_routes_spec.rb index 3bc14292b..1e3b0d694 100644 --- a/spec/lib/annotate/annotate_routes_spec.rb +++ b/spec/lib/annotate/annotate_routes_spec.rb @@ -64,7 +64,6 @@ expect(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content).at_least(:once) expect(AnnotateRoutes).to receive(:`).with('rake routes').and_return(rake_routes_result) - expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).at_least(:once) end context 'When the file does not contain magic comment' do @@ -85,6 +84,7 @@ it 'annotates normally' do expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) expect(mock_file).to receive(:puts).with(expected_result) + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).at_least(:once) AnnotateRoutes.do_annotations end @@ -107,6 +107,7 @@ it 'annotates in Markdown format' do expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) expect(mock_file).to receive(:puts).with(expected_result) + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).at_least(:once) AnnotateRoutes.do_annotations(format_markdown: true) end @@ -130,6 +131,7 @@ it 'annotates and wraps annotation with specified words' do expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) expect(mock_file).to receive(:puts).with(expected_result) + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).at_least(:once) AnnotateRoutes.do_annotations(wrapper_open: 'START', wrapper_close: 'END') end @@ -169,6 +171,7 @@ it 'annotates normally' do expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) expect(mock_file).to receive(:puts).with(expected_result) + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).at_least(:once) AnnotateRoutes.do_annotations end @@ -193,6 +196,7 @@ it 'annotates in Markdown format' do expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) expect(mock_file).to receive(:puts).with(expected_result) + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).at_least(:once) AnnotateRoutes.do_annotations(format_markdown: true) end @@ -218,6 +222,7 @@ it 'annotates and wraps annotation with specified words' do expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) expect(mock_file).to receive(:puts).with(expected_result) + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).at_least(:once) AnnotateRoutes.do_annotations(wrapper_open: 'START', wrapper_close: 'END') end From 3d89a4ea6f81853dc3fd48b7ab694a05d04957e2 Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Sun, 2 Feb 2020 19:51:46 +0900 Subject: [PATCH 09/19] Remove an empty line --- spec/lib/annotate/annotate_routes_spec.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/spec/lib/annotate/annotate_routes_spec.rb b/spec/lib/annotate/annotate_routes_spec.rb index 1e3b0d694..00139f4b7 100644 --- a/spec/lib/annotate/annotate_routes_spec.rb +++ b/spec/lib/annotate/annotate_routes_spec.rb @@ -60,7 +60,6 @@ before(:each) do expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true).at_least(:once) - expect(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content).at_least(:once) expect(AnnotateRoutes).to receive(:`).with('rake routes').and_return(rake_routes_result) From d6ba9a1470c9c1912be1ba409da545675bb1a73c Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Sun, 2 Feb 2020 19:56:32 +0900 Subject: [PATCH 10/19] Write test cases for file content in 'it' block --- spec/lib/annotate/annotate_routes_spec.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/spec/lib/annotate/annotate_routes_spec.rb b/spec/lib/annotate/annotate_routes_spec.rb index 00139f4b7..158bd9ed0 100644 --- a/spec/lib/annotate/annotate_routes_spec.rb +++ b/spec/lib/annotate/annotate_routes_spec.rb @@ -399,7 +399,6 @@ describe 'As for Rake versions' do 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(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content) expect(AnnotateRoutes).to receive(:`).with('rake routes').and_return(rake_routes_result) @@ -433,6 +432,7 @@ end it 'annotates with an empty line' do + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) expect(mock_file).to receive(:puts).with(expected_result) expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED) @@ -460,6 +460,7 @@ end it 'annotates without an empty line' do + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) expect(mock_file).to receive(:puts).with(expected_result) expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED) @@ -498,6 +499,7 @@ end it 'annotates with an empty line' do + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) expect(mock_file).to receive(:puts).with(expected_result) expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED) @@ -527,6 +529,7 @@ end it 'annotates without an empty line' do + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) expect(mock_file).to receive(:puts).with(expected_result) expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED) @@ -547,6 +550,7 @@ end it 'annotates with the timestamp and an empty line' do + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) expect(mock_file).to receive(:puts).with(expected_result) expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED) From b064f1e061c61a04e3739828899813b1cdba2d38 Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Sun, 2 Feb 2020 20:03:28 +0900 Subject: [PATCH 11/19] Write precondition of result of reading file in `before(:each)` --- spec/lib/annotate/annotate_routes_spec.rb | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/spec/lib/annotate/annotate_routes_spec.rb b/spec/lib/annotate/annotate_routes_spec.rb index 158bd9ed0..9363d5278 100644 --- a/spec/lib/annotate/annotate_routes_spec.rb +++ b/spec/lib/annotate/annotate_routes_spec.rb @@ -235,10 +235,10 @@ describe 'When adding' do before(:each) do - expect(File).to receive(:exist?).with(ROUTE_FILE) - .and_return(true).at_least(:once) - expect(AnnotateRoutes).to receive(:`).with('rake routes') - .and_return(rake_routes_result).at_least(:once) + expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true).at_least(:once) + expect(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content) + + expect(AnnotateRoutes).to receive(:`).with('rake routes').and_return(rake_routes_result).at_least(:once) end let :rake_routes_result do @@ -261,7 +261,6 @@ context 'When no option is specified' do it 'inserts annotations' do - expect(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content) expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) expect(mock_file).to receive(:puts).with(expected_result) expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED) @@ -272,7 +271,6 @@ context 'When the option "ignore_routes" is specified' do it 'inserts annotations' do - expect(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content) expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) expect(mock_file).to receive(:puts).with(expected_result) expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED) @@ -290,7 +288,6 @@ end it 'inserts annotations' do - expect(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content) expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) expect(mock_file).to receive(:puts).with(expected_result) expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED) @@ -310,7 +307,6 @@ end it 'should skip annotations if file does already contain annotation' do - expect(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content) expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_UNCHANGED) AnnotateRoutes.do_annotations @@ -328,10 +324,6 @@ EOS end - before :each do - expect(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content) - end - context 'When the option "position_in_routes" is specified as "top"' do let :expected_result do <<~EOS From 4dd53de0fc99634078b97359fee7bd8c4c784b25 Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Sun, 2 Feb 2020 20:00:40 +0900 Subject: [PATCH 12/19] Specify number of times of executions --- spec/lib/annotate/annotate_routes_spec.rb | 120 +++++++++++----------- 1 file changed, 59 insertions(+), 61 deletions(-) diff --git a/spec/lib/annotate/annotate_routes_spec.rb b/spec/lib/annotate/annotate_routes_spec.rb index 9363d5278..f5b862142 100644 --- a/spec/lib/annotate/annotate_routes_spec.rb +++ b/spec/lib/annotate/annotate_routes_spec.rb @@ -59,10 +59,10 @@ end before(:each) do - expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true).at_least(:once) - expect(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content).at_least(:once) + expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true).once + expect(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content).once - expect(AnnotateRoutes).to receive(:`).with('rake routes').and_return(rake_routes_result) + expect(AnnotateRoutes).to receive(:`).with('rake routes').and_return(rake_routes_result).once end context 'When the file does not contain magic comment' do @@ -81,9 +81,9 @@ end it 'annotates normally' do - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) - expect(mock_file).to receive(:puts).with(expected_result) - expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).at_least(:once) + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once + expect(mock_file).to receive(:puts).with(expected_result).once + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once AnnotateRoutes.do_annotations end @@ -104,9 +104,9 @@ end it 'annotates in Markdown format' do - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) - expect(mock_file).to receive(:puts).with(expected_result) - expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).at_least(:once) + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once + expect(mock_file).to receive(:puts).with(expected_result).once + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once AnnotateRoutes.do_annotations(format_markdown: true) end @@ -128,9 +128,9 @@ end it 'annotates and wraps annotation with specified words' do - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) - expect(mock_file).to receive(:puts).with(expected_result) - expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).at_least(:once) + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once + expect(mock_file).to receive(:puts).with(expected_result).once + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once AnnotateRoutes.do_annotations(wrapper_open: 'START', wrapper_close: 'END') end @@ -168,9 +168,9 @@ end it 'annotates normally' do - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) - expect(mock_file).to receive(:puts).with(expected_result) - expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).at_least(:once) + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once + expect(mock_file).to receive(:puts).with(expected_result).once + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once AnnotateRoutes.do_annotations end @@ -193,9 +193,9 @@ end it 'annotates in Markdown format' do - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) - expect(mock_file).to receive(:puts).with(expected_result) - expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).at_least(:once) + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once + expect(mock_file).to receive(:puts).with(expected_result).once + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once AnnotateRoutes.do_annotations(format_markdown: true) end @@ -219,9 +219,9 @@ end it 'annotates and wraps annotation with specified words' do - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) - expect(mock_file).to receive(:puts).with(expected_result) - expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).at_least(:once) + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once + expect(mock_file).to receive(:puts).with(expected_result).once + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once AnnotateRoutes.do_annotations(wrapper_open: 'START', wrapper_close: 'END') end @@ -235,10 +235,10 @@ describe 'When adding' do before(:each) do - expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true).at_least(:once) - expect(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content) + expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true).once + expect(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content).once - expect(AnnotateRoutes).to receive(:`).with('rake routes').and_return(rake_routes_result).at_least(:once) + expect(AnnotateRoutes).to receive(:`).with('rake routes').and_return(rake_routes_result).once end let :rake_routes_result do @@ -261,9 +261,9 @@ context 'When no option is specified' do it 'inserts annotations' do - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) - expect(mock_file).to receive(:puts).with(expected_result) - expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED) + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once + expect(mock_file).to receive(:puts).with(expected_result).once + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once AnnotateRoutes.do_annotations end @@ -271,9 +271,9 @@ context 'When the option "ignore_routes" is specified' do it 'inserts annotations' do - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) - expect(mock_file).to receive(:puts).with(expected_result) - expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED) + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once + expect(mock_file).to receive(:puts).with(expected_result).once + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once AnnotateRoutes.do_annotations(ignore_routes: 'my_route') end @@ -288,9 +288,9 @@ end it 'inserts annotations' do - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) - expect(mock_file).to receive(:puts).with(expected_result) - expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED) + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once + expect(mock_file).to receive(:puts).with(expected_result).once + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once AnnotateRoutes.do_annotations(position_in_routes: 'top') end @@ -307,7 +307,7 @@ end it 'should skip annotations if file does already contain annotation' do - expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_UNCHANGED) + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_UNCHANGED).once AnnotateRoutes.do_annotations end @@ -337,11 +337,10 @@ end it 'leaves magic comment on top and adds an empty line between magic comment and annotation' do - expect(File).to receive(:open).with(ROUTE_FILE, 'wb') - .and_yield(mock_file).at_least(:once) + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once + expect(mock_file).to receive(:puts).with(expected_result).once + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once - expect(mock_file).to receive(:puts).with(expected_result) - expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED) AnnotateRoutes.do_annotations(position_in_routes: 'top') end end @@ -358,11 +357,10 @@ end it 'leaves magic comment on top and adds an empty line between magic comment and annotation' do - expect(File).to receive(:open).with(ROUTE_FILE, 'wb') - .and_yield(mock_file).at_least(:once) + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once + expect(mock_file).to receive(:puts).with(expected_result).once + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once - expect(mock_file).to receive(:puts).with(expected_result) - expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED) AnnotateRoutes.do_annotations(position_in_routes: 'bottom') end end @@ -378,7 +376,7 @@ end it 'skips annotations' do - expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_UNCHANGED) + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_UNCHANGED).once AnnotateRoutes.do_annotations end @@ -390,10 +388,10 @@ describe 'As for Rake versions' do before :each do - expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true) - expect(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content) + expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true).once + expect(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content).once - expect(AnnotateRoutes).to receive(:`).with('rake routes').and_return(rake_routes_result) + expect(AnnotateRoutes).to receive(:`).with('rake routes').and_return(rake_routes_result).once end context 'with older Rake versions' do @@ -424,9 +422,9 @@ end it 'annotates with an empty line' do - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) - expect(mock_file).to receive(:puts).with(expected_result) - expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED) + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once + expect(mock_file).to receive(:puts).with(expected_result).once + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once AnnotateRoutes.do_annotations end @@ -452,9 +450,9 @@ end it 'annotates without an empty line' do - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) - expect(mock_file).to receive(:puts).with(expected_result) - expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED) + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once + expect(mock_file).to receive(:puts).with(expected_result).once + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once AnnotateRoutes.do_annotations end @@ -491,9 +489,9 @@ end it 'annotates with an empty line' do - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) - expect(mock_file).to receive(:puts).with(expected_result) - expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED) + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once + expect(mock_file).to receive(:puts).with(expected_result).once + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once AnnotateRoutes.do_annotations end @@ -521,9 +519,9 @@ end it 'annotates without an empty line' do - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) - expect(mock_file).to receive(:puts).with(expected_result) - expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED) + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once + expect(mock_file).to receive(:puts).with(expected_result).once + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once AnnotateRoutes.do_annotations end @@ -542,9 +540,9 @@ end it 'annotates with the timestamp and an empty line' do - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) - expect(mock_file).to receive(:puts).with(expected_result) - expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED) + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once + expect(mock_file).to receive(:puts).with(expected_result).once + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once AnnotateRoutes.do_annotations timestamp: true end From 702830e5a87bf25a8e58ed872601bbed28cda640 Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Sun, 2 Feb 2020 20:28:57 +0900 Subject: [PATCH 13/19] Add test cases for confirming that the file is not opened and file does not receive 'puts' --- spec/lib/annotate/annotate_routes_spec.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/spec/lib/annotate/annotate_routes_spec.rb b/spec/lib/annotate/annotate_routes_spec.rb index f5b862142..7ac377247 100644 --- a/spec/lib/annotate/annotate_routes_spec.rb +++ b/spec/lib/annotate/annotate_routes_spec.rb @@ -307,6 +307,8 @@ end it 'should skip annotations if file does already contain annotation' do + expect(File).not_to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) + expect(mock_file).not_to receive(:puts) expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_UNCHANGED).once AnnotateRoutes.do_annotations @@ -376,6 +378,8 @@ end it 'skips annotations' do + expect(File).not_to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) + expect(mock_file).not_to receive(:puts) expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_UNCHANGED).once AnnotateRoutes.do_annotations From b3e14d94b1ea546e4dac3a6aa3425cf88d56f209 Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Mon, 3 Feb 2020 09:00:49 +0900 Subject: [PATCH 14/19] Fix test cases when the file contains magic comments --- spec/lib/annotate/annotate_routes_spec.rb | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/spec/lib/annotate/annotate_routes_spec.rb b/spec/lib/annotate/annotate_routes_spec.rb index 7ac377247..b2e9d8414 100644 --- a/spec/lib/annotate/annotate_routes_spec.rb +++ b/spec/lib/annotate/annotate_routes_spec.rb @@ -141,13 +141,18 @@ context 'When the file contains magic comments' do MAGIC_COMMENTS.each do |magic_comment| describe "magic comment: #{magic_comment.inspect}" do - let :rake_routes_result do + let :route_file_content do <<~EOS #{magic_comment} - 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 + end + + let :rake_routes_result do + <<-EOS + 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 end @@ -155,7 +160,6 @@ context 'When no option is passed' do let :expected_result do <<~EOS - #{magic_comment} # == Route Map @@ -179,7 +183,6 @@ context 'When the option "format_markdown" is passed' do let :expected_result do <<~EOS - #{magic_comment} # ## Route Map @@ -204,7 +207,6 @@ context 'When the options "wrapper_open" and "wrapper_close" are passed' do let :expected_result do <<~EOS - #{magic_comment} # START From 65b201b5b8c1d25d5a64b89d939b882bbbfe6565 Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Mon, 3 Feb 2020 08:44:02 +0900 Subject: [PATCH 15/19] Make purpose of test cases more clear --- spec/lib/annotate/annotate_routes_spec.rb | 300 +++++++++++----------- 1 file changed, 151 insertions(+), 149 deletions(-) diff --git a/spec/lib/annotate/annotate_routes_spec.rb b/spec/lib/annotate/annotate_routes_spec.rb index b2e9d8414..ba904bb2b 100644 --- a/spec/lib/annotate/annotate_routes_spec.rb +++ b/spec/lib/annotate/annotate_routes_spec.rb @@ -45,19 +45,6 @@ end context 'When "config/routes.rb" exists' do - let :rake_routes_result do - <<-EOS - 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 - end - - let :route_file_content do - '' - end - before(:each) do expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true).once expect(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content).once @@ -65,167 +52,182 @@ expect(AnnotateRoutes).to receive(:`).with('rake routes').and_return(rake_routes_result).once end - context 'When the file does not contain magic comment' do - context 'When the file does not contain annotation yet' do - context 'When no option is passed' do - let :expected_result do - <<~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 - end - - it 'annotates normally' do - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once - expect(mock_file).to receive(:puts).with(expected_result).once - expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once - - AnnotateRoutes.do_annotations - end + context 'When the result of `rake routes` is present' do + context 'When the file does not contain magic comment' do + let :rake_routes_result do + <<-EOS + 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 end - context 'When the option "format_markdown" is passed' do - let :expected_result do - <<~EOS + let :route_file_content do + '' + end - # ## 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 - end + context 'When the file does not contain annotation yet' do + context 'When no option is passed' do + let :expected_result do + <<~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 + end - it 'annotates in Markdown format' do - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once - expect(mock_file).to receive(:puts).with(expected_result).once - expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once + it 'annotates normally' do + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once + expect(mock_file).to receive(:puts).with(expected_result).once + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once - AnnotateRoutes.do_annotations(format_markdown: true) + AnnotateRoutes.do_annotations + end end - end - context 'When the options "wrapper_open" and "wrapper_close" are passed' do - let :expected_result do - <<~EOS + context 'When the option "format_markdown" is passed' do + let :expected_result do + <<~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 + end - # 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 + it 'annotates in Markdown format' do + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once + expect(mock_file).to receive(:puts).with(expected_result).once + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once + + AnnotateRoutes.do_annotations(format_markdown: true) + end end - it 'annotates and wraps annotation with specified words' do - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once - expect(mock_file).to receive(:puts).with(expected_result).once - expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once + context 'When the options "wrapper_open" and "wrapper_close" are passed' do + let :expected_result do + <<~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 + end - AnnotateRoutes.do_annotations(wrapper_open: 'START', wrapper_close: 'END') + it 'annotates and wraps annotation with specified words' do + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once + expect(mock_file).to receive(:puts).with(expected_result).once + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once + + AnnotateRoutes.do_annotations(wrapper_open: 'START', wrapper_close: 'END') + end end end end - end - context 'When the file contains magic comments' do - MAGIC_COMMENTS.each do |magic_comment| - describe "magic comment: #{magic_comment.inspect}" do - let :route_file_content do - <<~EOS - #{magic_comment} - EOS - end + context 'When the file contains magic comments' do + MAGIC_COMMENTS.each do |magic_comment| + describe "magic comment: #{magic_comment.inspect}" do + let :route_file_content do + <<~EOS + #{magic_comment} + EOS + end - let :rake_routes_result do - <<-EOS + let :rake_routes_result do + <<-EOS 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 - end - - context 'When the file does not contain annotation yet' do - context 'When no option is passed' do - let :expected_result do - <<~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 - end - - it 'annotates normally' do - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once - expect(mock_file).to receive(:puts).with(expected_result).once - expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once - - AnnotateRoutes.do_annotations - end + EOS end - context 'When the option "format_markdown" is passed' do - let :expected_result do - <<~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 + context 'When the file does not contain annotation yet' do + context 'When no option is passed' do + let :expected_result do + <<~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 + end + + it 'annotates normally' do + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once + expect(mock_file).to receive(:puts).with(expected_result).once + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once + + AnnotateRoutes.do_annotations + end end - it 'annotates in Markdown format' do - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once - expect(mock_file).to receive(:puts).with(expected_result).once - expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once - - AnnotateRoutes.do_annotations(format_markdown: true) + context 'When the option "format_markdown" is passed' do + let :expected_result do + <<~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 + end + + it 'annotates in Markdown format' do + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once + expect(mock_file).to receive(:puts).with(expected_result).once + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once + + AnnotateRoutes.do_annotations(format_markdown: true) + end end - end - - context 'When the options "wrapper_open" and "wrapper_close" are passed' do - let :expected_result do - <<~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 - end - - it 'annotates and wraps annotation with specified words' do - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once - expect(mock_file).to receive(:puts).with(expected_result).once - expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once - AnnotateRoutes.do_annotations(wrapper_open: 'START', wrapper_close: 'END') + context 'When the options "wrapper_open" and "wrapper_close" are passed' do + let :expected_result do + <<~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 + end + + it 'annotates and wraps annotation with specified words' do + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once + expect(mock_file).to receive(:puts).with(expected_result).once + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once + + AnnotateRoutes.do_annotations(wrapper_open: 'START', wrapper_close: 'END') + end end end end @@ -235,7 +237,7 @@ end end - describe 'When adding' do + describe 'When the result of `rake routes` is blank' do before(:each) do expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true).once expect(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content).once From f5cff71aa23eb8282adca2e17469adf07f4ca6fb Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Mon, 3 Feb 2020 15:26:51 +0900 Subject: [PATCH 16/19] Refactor test cases when the result of `rake routes` is blank --- spec/lib/annotate/annotate_routes_spec.rb | 245 +++++++++++----------- 1 file changed, 125 insertions(+), 120 deletions(-) diff --git a/spec/lib/annotate/annotate_routes_spec.rb b/spec/lib/annotate/annotate_routes_spec.rb index ba904bb2b..1d360286f 100644 --- a/spec/lib/annotate/annotate_routes_spec.rb +++ b/spec/lib/annotate/annotate_routes_spec.rb @@ -234,159 +234,164 @@ end end end - end - end - describe 'When the result of `rake routes` is blank' do - before(:each) do - expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true).once - expect(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content).once + context 'When the result of `rake routes` is blank' do + let :rake_routes_result do + '' + end - expect(AnnotateRoutes).to receive(:`).with('rake routes').and_return(rake_routes_result).once - end + context 'When the file does not contain magic comment' do + context 'When the file does not contain annotation yet' do + let :route_file_content do + '' + end - let :rake_routes_result do - '' - end + context 'When no option is specified' do + let :expected_result do + <<~EOS - context 'When the file does not contain magic comment' do - context 'When the file does not contain annotation yet' do - let :expected_result do - <<~EOS + # == Route Map + # + EOS + end - # == Route Map - # - EOS - end + it 'inserts annotations' do + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once + expect(mock_file).to receive(:puts).with(expected_result).once + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once - let :route_file_content do - '' - end + AnnotateRoutes.do_annotations + end + end - context 'When no option is specified' do - it 'inserts annotations' do - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once - expect(mock_file).to receive(:puts).with(expected_result).once - expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once + context 'When the option "ignore_routes" is specified' do + let :expected_result do + <<~EOS - AnnotateRoutes.do_annotations - end - end + # == Route Map + # + EOS + end - context 'When the option "ignore_routes" is specified' do - it 'inserts annotations' do - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once - expect(mock_file).to receive(:puts).with(expected_result).once - expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once + it 'inserts annotations' do + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once + expect(mock_file).to receive(:puts).with(expected_result).once + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once - AnnotateRoutes.do_annotations(ignore_routes: 'my_route') - end - end + AnnotateRoutes.do_annotations(ignore_routes: 'my_route') + end + end - context 'When the option "position_in_routes" is specified as "top"' do - let :expected_result do - <<~EOS - # == Route Map - # - EOS - end + context 'When the option "position_in_routes" is specified as "top"' do + let :expected_result do + <<~EOS + # == Route Map + # + EOS + end - it 'inserts annotations' do - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once - expect(mock_file).to receive(:puts).with(expected_result).once - expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once + it 'inserts annotations' do + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once + expect(mock_file).to receive(:puts).with(expected_result).once + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once - AnnotateRoutes.do_annotations(position_in_routes: 'top') + AnnotateRoutes.do_annotations(position_in_routes: 'top') + end + end end - end - end - context 'When the file already contains annotation' do - let :route_file_content do - <<~EOS + context 'When the file already contains annotation' do + context 'When no option is specified' do + let :route_file_content do + <<~EOS - # == Route Map - # - EOS - end + # == Route Map + # + EOS + end - it 'should skip annotations if file does already contain annotation' do - expect(File).not_to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) - expect(mock_file).not_to receive(:puts) - expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_UNCHANGED).once + it 'should skip annotations if file does already contain annotation' do + expect(File).not_to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) + expect(mock_file).not_to receive(:puts) + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_UNCHANGED).once - AnnotateRoutes.do_annotations + AnnotateRoutes.do_annotations + end + end + end end - end - end - context 'When the file contains magic comments' do - MAGIC_COMMENTS.each do |magic_comment| - describe "magic comment: #{magic_comment.inspect}" do - let :route_file_content do - <<~EOS - #{magic_comment} - Something - EOS - end + context 'When the file contains magic comments' do + MAGIC_COMMENTS.each do |magic_comment| + describe "magic comment: #{magic_comment.inspect}" do + let :route_file_content do + <<~EOS + #{magic_comment} + Something + EOS + end - context 'When the option "position_in_routes" is specified as "top"' do - let :expected_result do - <<~EOS - #{magic_comment} + context 'When the file does not contain annotation yet' do + context 'When the option "position_in_routes" is specified as "top"' do + let :expected_result do + <<~EOS + #{magic_comment} - # == Route Map - # + # == Route Map + # - Something - EOS - end + Something + EOS + end - it 'leaves magic comment on top and adds an empty line between magic comment and annotation' do - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once - expect(mock_file).to receive(:puts).with(expected_result).once - expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once + it 'leaves magic comment on top and adds an empty line between magic comment and annotation' do + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once + expect(mock_file).to receive(:puts).with(expected_result).once + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once - AnnotateRoutes.do_annotations(position_in_routes: 'top') - end - end + AnnotateRoutes.do_annotations(position_in_routes: 'top') + end + end - context 'When the option "position_in_routes" is specified as "bottom"' do - let :expected_result do - <<~EOS - #{magic_comment} - Something + context 'When the option "position_in_routes" is specified as "bottom"' do + let :expected_result do + <<~EOS + #{magic_comment} + Something - # == Route Map - # - EOS - end + # == Route Map + # + EOS + end - it 'leaves magic comment on top and adds an empty line between magic comment and annotation' do - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once - expect(mock_file).to receive(:puts).with(expected_result).once - expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once + it 'leaves magic comment on top and adds an empty line between magic comment and annotation' do + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once + expect(mock_file).to receive(:puts).with(expected_result).once + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once - AnnotateRoutes.do_annotations(position_in_routes: 'bottom') - end - end + AnnotateRoutes.do_annotations(position_in_routes: 'bottom') + end + end + end - context 'When the file already contains annotation' do - let :route_file_content do - <<~EOS - #{magic_comment} + context 'When the file already contains annotation' do + let :route_file_content do + <<~EOS + #{magic_comment} - # == Route Map - # - EOS - end + # == Route Map + # + EOS + end - it 'skips annotations' do - expect(File).not_to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) - expect(mock_file).not_to receive(:puts) - expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_UNCHANGED).once + it 'skips annotations' do + expect(File).not_to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) + expect(mock_file).not_to receive(:puts) + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_UNCHANGED).once - AnnotateRoutes.do_annotations + AnnotateRoutes.do_annotations + end + end end end end From 6de2d6e7d27a7c163972a0f6987c5014781849e7 Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Mon, 3 Feb 2020 15:38:19 +0900 Subject: [PATCH 17/19] Make purpose of test cases more clear --- spec/lib/annotate/annotate_routes_spec.rb | 284 +++++++++++----------- 1 file changed, 143 insertions(+), 141 deletions(-) diff --git a/spec/lib/annotate/annotate_routes_spec.rb b/spec/lib/annotate/annotate_routes_spec.rb index 1d360286f..26ad4c522 100644 --- a/spec/lib/annotate/annotate_routes_spec.rb +++ b/spec/lib/annotate/annotate_routes_spec.rb @@ -45,7 +45,7 @@ end context 'When "config/routes.rb" exists' do - before(:each) do + before :each do expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true).once expect(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content).once @@ -53,180 +53,182 @@ end context 'When the result of `rake routes` is present' do - context 'When the file does not contain magic comment' do - let :rake_routes_result do - <<-EOS + context 'When the result of `rake routes` does not contain Rake version' do + context 'When the file does not contain magic comment' do + let :rake_routes_result do + <<-EOS 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 - end + EOS + end - let :route_file_content do - '' - end + let :route_file_content do + '' + end - context 'When the file does not contain annotation yet' do - context 'When no option is passed' do - let :expected_result do - <<~EOS + context 'When the file does not contain annotation yet' do + context 'When no option is passed' do + let :expected_result do + <<~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 - end + # == 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 + end - it 'annotates normally' do - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once - expect(mock_file).to receive(:puts).with(expected_result).once - expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once + it 'annotates normally' do + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once + expect(mock_file).to receive(:puts).with(expected_result).once + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once - AnnotateRoutes.do_annotations + AnnotateRoutes.do_annotations + end end - end - context 'When the option "format_markdown" is passed' do - let :expected_result do - <<~EOS + context 'When the option "format_markdown" is passed' do + let :expected_result do + <<~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 - end + # ## 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 + end - it 'annotates in Markdown format' do - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once - expect(mock_file).to receive(:puts).with(expected_result).once - expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once + it 'annotates in Markdown format' do + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once + expect(mock_file).to receive(:puts).with(expected_result).once + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once - AnnotateRoutes.do_annotations(format_markdown: true) + AnnotateRoutes.do_annotations(format_markdown: true) + end end - end - context 'When the options "wrapper_open" and "wrapper_close" are passed' do - let :expected_result do - <<~EOS + context 'When the options "wrapper_open" and "wrapper_close" are passed' do + let :expected_result do + <<~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 - end + # 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 + end - it 'annotates and wraps annotation with specified words' do - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once - expect(mock_file).to receive(:puts).with(expected_result).once - expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once + it 'annotates and wraps annotation with specified words' do + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once + expect(mock_file).to receive(:puts).with(expected_result).once + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once - AnnotateRoutes.do_annotations(wrapper_open: 'START', wrapper_close: 'END') + AnnotateRoutes.do_annotations(wrapper_open: 'START', wrapper_close: 'END') + end end end end - end - context 'When the file contains magic comments' do - MAGIC_COMMENTS.each do |magic_comment| - describe "magic comment: #{magic_comment.inspect}" do - let :route_file_content do - <<~EOS - #{magic_comment} - EOS - end + context 'When the file contains magic comments' do + MAGIC_COMMENTS.each do |magic_comment| + describe "magic comment: #{magic_comment.inspect}" do + let :route_file_content do + <<~EOS + #{magic_comment} + EOS + end - let :rake_routes_result do - <<-EOS + let :rake_routes_result do + <<-EOS 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 - end - - context 'When the file does not contain annotation yet' do - context 'When no option is passed' do - let :expected_result do - <<~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 - end - - it 'annotates normally' do - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once - expect(mock_file).to receive(:puts).with(expected_result).once - expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once - - AnnotateRoutes.do_annotations - end + EOS end - context 'When the option "format_markdown" is passed' do - let :expected_result do - <<~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 - end - - it 'annotates in Markdown format' do - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once - expect(mock_file).to receive(:puts).with(expected_result).once - expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once - - AnnotateRoutes.do_annotations(format_markdown: true) + context 'When the file does not contain annotation yet' do + context 'When no option is passed' do + let :expected_result do + <<~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 + end + + it 'annotates normally' do + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once + expect(mock_file).to receive(:puts).with(expected_result).once + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once + + AnnotateRoutes.do_annotations + end end - end - - context 'When the options "wrapper_open" and "wrapper_close" are passed' do - let :expected_result do - <<~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 + context 'When the option "format_markdown" is passed' do + let :expected_result do + <<~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 + end + + it 'annotates in Markdown format' do + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once + expect(mock_file).to receive(:puts).with(expected_result).once + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once + + AnnotateRoutes.do_annotations(format_markdown: true) + end end - it 'annotates and wraps annotation with specified words' do - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once - expect(mock_file).to receive(:puts).with(expected_result).once - expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once - - AnnotateRoutes.do_annotations(wrapper_open: 'START', wrapper_close: 'END') + context 'When the options "wrapper_open" and "wrapper_close" are passed' do + let :expected_result do + <<~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 + end + + it 'annotates and wraps annotation with specified words' do + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once + expect(mock_file).to receive(:puts).with(expected_result).once + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once + + AnnotateRoutes.do_annotations(wrapper_open: 'START', wrapper_close: 'END') + end end end end From dfdffc501e16e23cf2ef13bc153b12426488d05a Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Mon, 3 Feb 2020 15:44:18 +0900 Subject: [PATCH 18/19] Refactor test cases when the result of `rake routes` contains Rake version --- spec/lib/annotate/annotate_routes_spec.rb | 321 +++++++++++----------- 1 file changed, 157 insertions(+), 164 deletions(-) diff --git a/spec/lib/annotate/annotate_routes_spec.rb b/spec/lib/annotate/annotate_routes_spec.rb index 26ad4c522..dc4400a35 100644 --- a/spec/lib/annotate/annotate_routes_spec.rb +++ b/spec/lib/annotate/annotate_routes_spec.rb @@ -235,6 +235,163 @@ end end end + + context 'When the result of `rake routes` contains Rake version' do + context 'with older Rake versions' do + let :rake_routes_result do + <<~EOS.chomp + (in /bad/line) + good line + EOS + end + + context 'When the route file does not end with an empty line' do + let :route_file_content do + <<~EOS.chomp + ActionController::Routing... + foo + EOS + end + + let :expected_result do + <<~EOS + ActionController::Routing... + foo + + # == Route Map + # + # good line + EOS + end + + it 'annotates with an empty line' do + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once + expect(mock_file).to receive(:puts).with(expected_result).once + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once + + AnnotateRoutes.do_annotations + end + end + + context 'When the route file ends with an empty line' do + let :route_file_content do + <<~EOS + ActionController::Routing... + foo + EOS + end + + let :expected_result do + <<~EOS + ActionController::Routing... + foo + + # == Route Map + # + # good line + EOS + end + + it 'annotates without an empty line' do + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once + expect(mock_file).to receive(:puts).with(expected_result).once + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once + + AnnotateRoutes.do_annotations + end + end + end + + context 'with newer Rake versions' do + let :rake_routes_result do + <<~EOS.chomp + another good line + good line + EOS + end + + context 'When the route file does not end with an empty line' do + context 'When no option is passed' do + let :route_file_content do + <<~EOS.chomp + ActionController::Routing... + foo + EOS + end + + let :expected_result do + <<~EOS + ActionController::Routing... + foo + + # == Route Map + # + # another good line + # good line + EOS + end + + it 'annotates with an empty line' do + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once + expect(mock_file).to receive(:puts).with(expected_result).once + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once + + AnnotateRoutes.do_annotations + end + end + end + + context 'When the route file ends with an empty line' do + let :route_file_content do + <<~EOS + ActionController::Routing... + foo + EOS + end + + let :expected_result do + <<~EOS + ActionController::Routing... + foo + + # == Route Map + # + # another good line + # good line + EOS + end + + it 'annotates without an empty line' do + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once + expect(mock_file).to receive(:puts).with(expected_result).once + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once + + AnnotateRoutes.do_annotations + end + end + + context 'When option "timestamp" is passed' do + let :route_file_content do + <<~EOS.chomp + ActionController::Routing... + foo + EOS + end + + let :expected_result do + /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/ + end + + it 'annotates with the timestamp and an empty line' do + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once + expect(mock_file).to receive(:puts).with(expected_result).once + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once + + AnnotateRoutes.do_annotations timestamp: true + end + end + end + end end context 'When the result of `rake routes` is blank' do @@ -401,170 +558,6 @@ end end - describe 'As for Rake versions' do - before :each do - expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true).once - expect(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content).once - - expect(AnnotateRoutes).to receive(:`).with('rake routes').and_return(rake_routes_result).once - end - - context 'with older Rake versions' do - let :rake_routes_result do - <<~EOS.chomp - (in /bad/line) - good line - EOS - end - - context 'When the route file does not end with an empty line' do - let :route_file_content do - <<~EOS.chomp - ActionController::Routing... - foo - EOS - end - - let :expected_result do - <<~EOS - ActionController::Routing... - foo - - # == Route Map - # - # good line - EOS - end - - it 'annotates with an empty line' do - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once - expect(mock_file).to receive(:puts).with(expected_result).once - expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once - - AnnotateRoutes.do_annotations - end - end - - context 'When the route file ends with an empty line' do - let :route_file_content do - <<~EOS - ActionController::Routing... - foo - EOS - end - - let :expected_result do - <<~EOS - ActionController::Routing... - foo - - # == Route Map - # - # good line - EOS - end - - it 'annotates without an empty line' do - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once - expect(mock_file).to receive(:puts).with(expected_result).once - expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once - - AnnotateRoutes.do_annotations - end - end - end - - context 'with newer Rake versions' do - let :rake_routes_result do - <<~EOS.chomp - another good line - good line - EOS - end - - context 'When the route file does not end with an empty line' do - context 'When no option is passed' do - let :route_file_content do - <<~EOS.chomp - ActionController::Routing... - foo - EOS - end - - let :expected_result do - <<~EOS - ActionController::Routing... - foo - - # == Route Map - # - # another good line - # good line - EOS - end - - it 'annotates with an empty line' do - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once - expect(mock_file).to receive(:puts).with(expected_result).once - expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once - - AnnotateRoutes.do_annotations - end - end - end - - context 'When the route file ends with an empty line' do - let :route_file_content do - <<~EOS - ActionController::Routing... - foo - EOS - end - - let :expected_result do - <<~EOS - ActionController::Routing... - foo - - # == Route Map - # - # another good line - # good line - EOS - end - - it 'annotates without an empty line' do - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once - expect(mock_file).to receive(:puts).with(expected_result).once - expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once - - AnnotateRoutes.do_annotations - end - end - - context 'When option "timestamp" is passed' do - let :route_file_content do - <<~EOS.chomp - ActionController::Routing... - foo - EOS - end - - let :expected_result do - /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/ - end - - it 'annotates with the timestamp and an empty line' do - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once - expect(mock_file).to receive(:puts).with(expected_result).once - expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once - - AnnotateRoutes.do_annotations timestamp: true - end - end - end - end - describe '.remove_annotations' do before :each do expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true).once From 627f0927058ea9582ec75a8c4ded7d204c6d27c3 Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Thu, 13 Feb 2020 20:06:30 +0900 Subject: [PATCH 19/19] Execute `rubocop --auto-gen-config` --- .rubocop_todo.yml | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index c2d82b946..fc7c1e779 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2020-02-13 18:10:52 +0900 using RuboCop version 0.68.1. +# on 2020-02-13 20:05:34 +0900 using RuboCop version 0.68.1. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new @@ -64,14 +64,6 @@ Layout/ExtraSpacing: - 'lib/annotate/annotate_models.rb' - 'lib/tasks/annotate_routes.rake' -# Offense count: 4 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, IndentationWidth. -# SupportedStyles: aligned, indented, indented_relative_to_receiver -Layout/MultilineMethodCallIndentation: - Exclude: - - 'spec/lib/annotate/annotate_routes_spec.rb' - # Offense count: 5 # Cop supports --auto-correct. # Configuration parameters: EnforcedStyle, IndentationWidth. @@ -221,7 +213,7 @@ Naming/AccessorMethodName: Exclude: - 'lib/annotate.rb' -# Offense count: 93 +# Offense count: 103 # Configuration parameters: Blacklist. # Blacklist: (?-mix:(^|\s)(EO[A-Z]{1}|END)(\s|$)) Naming/HeredocDelimiterNaming: @@ -478,7 +470,7 @@ Style/StderrPuts: - 'lib/annotate.rb' - 'lib/annotate/annotate_models.rb' -# Offense count: 111 +# Offense count: 107 # Cop supports --auto-correct. # Configuration parameters: EnforcedStyle, ConsistentQuotesInMultiline. # SupportedStyles: single_quotes, double_quotes @@ -490,7 +482,6 @@ Style/StringLiterals: - 'lib/tasks/annotate_models_migrate.rake' - 'lib/tasks/annotate_routes.rake' - 'spec/lib/annotate/annotate_models_spec.rb' - - 'spec/lib/annotate/annotate_routes_spec.rb' - 'spec/lib/annotate/parser_spec.rb' # Offense count: 1 @@ -528,7 +519,7 @@ Style/UnneededPercentQ: Exclude: - 'annotate.gemspec' -# Offense count: 346 +# Offense count: 377 # Cop supports --auto-correct. # Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns. # URISchemes: http, https