Skip to content

Support --frozen option for routing annotations #979

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions lib/annotate/annotate_routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ def do_annotations(options = {})
new_content = annotate_routes(HeaderGenerator.generate(options), content, header_position, options)
new_text = new_content.join("\n")

if rewrite_contents(existing_text, new_text)
if options[:frozen]
if needs_rewrite_contents?(existing_text, new_text)
abort "annotate error. #{routes_file} needs to be updated, but annotate was run with `--frozen`."
else
puts "#{routes_file} was not changed."
end
elsif rewrite_contents(existing_text, new_text)
puts "#{routes_file} was annotated."
else
puts "#{routes_file} was not changed."
Expand Down Expand Up @@ -83,14 +89,18 @@ def strip_on_removal(content, header_position)
end

def rewrite_contents(existing_text, new_text)
if existing_text == new_text
false
else
if needs_rewrite_contents?(existing_text, new_text)
File.open(routes_file, 'wb') { |f| f.puts(new_text) }
true
else
false
end
end

def needs_rewrite_contents?(existing_text, new_text)
existing_text != new_text
end

def annotate_routes(header, content, header_position, options = {})
magic_comments_map, content = Helpers.extract_magic_comments_from_array(content)
if %w(before top).include?(options[:position_in_routes])
Expand Down
1 change: 1 addition & 0 deletions lib/tasks/annotate_routes.rake
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ task :annotate_routes => :environment do
options[:position_in_routes] = Annotate::Helpers.fallback(ENV['position_in_routes'], ENV['position'])
options[:ignore_routes] = Annotate::Helpers.fallback(ENV['ignore_routes'], nil)
options[:require] = ENV['require'] ? ENV['require'].split(',') : []
options[:frozen] = Annotate::Helpers.true?(ENV['frozen'])
options[:wrapper_open] = Annotate::Helpers.fallback(ENV['wrapper_open'], ENV['wrapper'])
options[:wrapper_close] = Annotate::Helpers.fallback(ENV['wrapper_close'], ENV['wrapper'])
AnnotateRoutes.do_annotations(options)
Expand Down
65 changes: 65 additions & 0 deletions spec/lib/annotate/annotate_routes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,71 @@
end
end
end

describe 'frozen option' do
let :aborted_message do
"annotate error. #{ROUTE_FILE} needs to be updated, but annotate was run with `--frozen`."
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

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::HeaderGenerator).to receive(:`).with('rake routes').and_return(rake_routes_result).once
end

context 'when annotation does not exists' do
let :route_file_content do
''
end

it 'aborts' do
expect { AnnotateRoutes.do_annotations(frozen: true) }.to raise_error SystemExit, aborted_message
end
end

context 'when annotation exists but is not updated' do
let :route_file_content do
<<~EOS
# == Route Map
#
# Prefix Verb URI Pattern Controller#Action
# myaction2 POST /url2(.:format) mycontroller2#action
# myaction3 DELETE|GET /url3(.:format) mycontroller3#action
EOS
end

it 'aborts' do
expect { AnnotateRoutes.do_annotations(frozen: true) }.to raise_error SystemExit, aborted_message
end
end

context 'when annotation exists and is already updated' do
let :route_file_content 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 'does NOT abort' do
expect { AnnotateRoutes.do_annotations(frozen: true) }.not_to raise_error
end
end
end
end

describe '.remove_annotations' do
Expand Down