Skip to content

Commit

Permalink
Refactor test cases when the result of rake routes contains Rake ve…
Browse files Browse the repository at this point in the history
…rsion
  • Loading branch information
nard-tech committed Feb 21, 2020
1 parent 6de2d6e commit dfdffc5
Showing 1 changed file with 157 additions and 164 deletions.
321 changes: 157 additions & 164 deletions spec/lib/annotate/annotate_routes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit dfdffc5

Please sign in to comment.