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 all 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
19 changes: 10 additions & 9 deletions lib/annotate/annotate_routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ def do_annotations(options = {})
content, header_position = Helpers.strip_annotations(existing_text)
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 rewrite_contents(existing_text, new_text, options[:frozen])
puts "#{routes_file} was annotated."
else
puts "#{routes_file} was not changed."
Expand All @@ -40,13 +39,13 @@ def do_annotations(options = {})
end
end

def remove_annotations(_options={})
def remove_annotations(options={})
if routes_file_exist?
existing_text = File.read(routes_file)
content, header_position = Helpers.strip_annotations(existing_text)
new_content = strip_on_removal(content, header_position)
new_text = new_content.join("\n")
if rewrite_contents(existing_text, new_text)
if rewrite_contents(existing_text, new_text, options[:frozen])
puts "Annotations were removed from #{routes_file}."
else
puts "#{routes_file} was not changed (Annotation did not exist)."
Expand Down Expand Up @@ -82,13 +81,15 @@ def strip_on_removal(content, header_position)
content
end

def rewrite_contents(existing_text, new_text)
if existing_text == new_text
false
else
def rewrite_contents(existing_text, new_text, frozen)
content_changed = (existing_text != new_text)

if content_changed
abort "annotate error. #{routes_file} needs to be updated, but annotate was run with `--frozen`." if frozen
File.open(routes_file, 'wb') { |f| f.puts(new_text) }
true
end

content_changed
end

def annotate_routes(header, content, header_position, options = {})
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