Unverified Commit 10a7a76c by Takumi KAGIYAMA Committed by GitHub

Support `--frozen` option for routing annotations (#979)

The `--frozen` option previously deal only model annotations. This change will support route annotations as well. --------- Signed-off-by: 's avatarkg8m <takumi.kagiyama@gmail.com> Co-authored-by: 's avatarCuong Tran <ctran@users.noreply.github.com>
parent a28fef34
...@@ -29,8 +29,7 @@ module AnnotateRoutes ...@@ -29,8 +29,7 @@ module AnnotateRoutes
content, header_position = Helpers.strip_annotations(existing_text) content, header_position = Helpers.strip_annotations(existing_text)
new_content = annotate_routes(HeaderGenerator.generate(options), content, header_position, options) new_content = annotate_routes(HeaderGenerator.generate(options), content, header_position, options)
new_text = new_content.join("\n") new_text = new_content.join("\n")
if rewrite_contents(existing_text, new_text, options[:frozen])
if rewrite_contents(existing_text, new_text)
puts "#{routes_file} was annotated." puts "#{routes_file} was annotated."
else else
puts "#{routes_file} was not changed." puts "#{routes_file} was not changed."
...@@ -40,13 +39,13 @@ module AnnotateRoutes ...@@ -40,13 +39,13 @@ module AnnotateRoutes
end end
end end
def remove_annotations(_options={}) def remove_annotations(options={})
if routes_file_exist? if routes_file_exist?
existing_text = File.read(routes_file) existing_text = File.read(routes_file)
content, header_position = Helpers.strip_annotations(existing_text) content, header_position = Helpers.strip_annotations(existing_text)
new_content = strip_on_removal(content, header_position) new_content = strip_on_removal(content, header_position)
new_text = new_content.join("\n") 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}." puts "Annotations were removed from #{routes_file}."
else else
puts "#{routes_file} was not changed (Annotation did not exist)." puts "#{routes_file} was not changed (Annotation did not exist)."
...@@ -82,13 +81,15 @@ module AnnotateRoutes ...@@ -82,13 +81,15 @@ module AnnotateRoutes
content content
end end
def rewrite_contents(existing_text, new_text) def rewrite_contents(existing_text, new_text, frozen)
if existing_text == new_text content_changed = (existing_text != new_text)
false
else 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) } File.open(routes_file, 'wb') { |f| f.puts(new_text) }
true
end end
content_changed
end end
def annotate_routes(header, content, header_position, options = {}) def annotate_routes(header, content, header_position, options = {})
......
...@@ -14,6 +14,7 @@ task :annotate_routes => :environment do ...@@ -14,6 +14,7 @@ task :annotate_routes => :environment do
options[:position_in_routes] = Annotate::Helpers.fallback(ENV['position_in_routes'], ENV['position']) options[:position_in_routes] = Annotate::Helpers.fallback(ENV['position_in_routes'], ENV['position'])
options[:ignore_routes] = Annotate::Helpers.fallback(ENV['ignore_routes'], nil) options[:ignore_routes] = Annotate::Helpers.fallback(ENV['ignore_routes'], nil)
options[:require] = ENV['require'] ? ENV['require'].split(',') : [] 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_open] = Annotate::Helpers.fallback(ENV['wrapper_open'], ENV['wrapper'])
options[:wrapper_close] = Annotate::Helpers.fallback(ENV['wrapper_close'], ENV['wrapper']) options[:wrapper_close] = Annotate::Helpers.fallback(ENV['wrapper_close'], ENV['wrapper'])
AnnotateRoutes.do_annotations(options) AnnotateRoutes.do_annotations(options)
......
...@@ -556,6 +556,71 @@ describe AnnotateRoutes do ...@@ -556,6 +556,71 @@ describe AnnotateRoutes do
end end
end 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 end
describe '.remove_annotations' do describe '.remove_annotations' 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