Unverified Commit d902dc4c by Shu Fujita Committed by GitHub

Refactor test cases of AnnotateRoutes as for Rake versions (#754)

Refactor test cases for entire rewrite in the future.
parent ec0b3b61
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2020-02-05 17:13:49 -1000 using RuboCop version 0.68.1.
# on 2020-02-12 18:43:06 +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
......@@ -221,7 +221,7 @@ Naming/AccessorMethodName:
Exclude:
- 'lib/annotate.rb'
# Offense count: 82
# Offense count: 93
# Configuration parameters: Blacklist.
# Blacklist: (?-mix:(^|\s)(EO[A-Z]{1}|END)(\s|$))
Naming/HeredocDelimiterNaming:
......@@ -528,7 +528,7 @@ Style/UnneededPercentQ:
Exclude:
- 'annotate.gemspec'
# Offense count: 353
# Offense count: 344
# Cop supports --auto-correct.
# Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
......
......@@ -271,53 +271,165 @@ describe AnnotateRoutes do
end
end
describe 'When adding with older Rake versions' do
before(:each) do
describe 'As for Rake versions' do
before :each do
expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true)
expect(AnnotateRoutes).to receive(:`).with('rake routes').and_return("(in /bad/line)\ngood line")
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file)
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED)
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)
end
it 'should annotate and add a newline!' do
expect(File).to receive(:read).with(ROUTE_FILE).and_return("ActionController::Routing...\nfoo")
expect(mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map\n#\n# good line\n/)
AnnotateRoutes.do_annotations
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 'should not add a newline if there are empty lines' do
expect(File).to receive(:read).with(ROUTE_FILE).and_return("ActionController::Routing...\nfoo\n")
expect(mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map\n#\n# good line\n/)
it 'annotates with an empty line' do
expect(mock_file).to receive(:puts).with(expected_result)
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED)
AnnotateRoutes.do_annotations
end
end
describe 'When adding with newer Rake versions' do
before(:each) do
expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true)
expect(AnnotateRoutes).to receive(:`).with('rake routes').and_return("another good line\ngood line")
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file)
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(mock_file).to receive(:puts).with(expected_result)
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED)
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 'should annotate and add a newline!' do
expect(File).to receive(:read).with(ROUTE_FILE).and_return("ActionController::Routing...\nfoo")
expect(mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map\n#\n# another good line\n# good line\n/)
it 'annotates with an empty line' do
expect(mock_file).to receive(:puts).with(expected_result)
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED)
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(mock_file).to receive(:puts).with(expected_result)
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED)
it 'should not add a newline if there are empty lines' do
expect(File).to receive(:read).with(ROUTE_FILE).and_return("ActionController::Routing...\nfoo\n")
expect(mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map\n#\n# another good line\n# good line\n/)
AnnotateRoutes.do_annotations
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(mock_file).to receive(:puts).with(expected_result)
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED)
it 'should add a timestamp when :timestamp is passed' do
expect(File).to receive(:read).with(ROUTE_FILE).and_return("ActionController::Routing...\nfoo")
expect(mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map \(Updated \d{4}-\d{2}-\d{2} \d{2}:\d{2}\)\n#\n# another good line\n# good line\n/)
AnnotateRoutes.do_annotations timestamp: true
end
end
end
end
describe '.remove_annotations' do
before :each do
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment