diff --git a/lib/annotate/annotate_routes.rb b/lib/annotate/annotate_routes.rb index 75cc421e..c9a2218a 100644 --- a/lib/annotate/annotate_routes.rb +++ b/lib/annotate/annotate_routes.rb @@ -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." @@ -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)." @@ -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 = {}) diff --git a/lib/tasks/annotate_routes.rake b/lib/tasks/annotate_routes.rake index ae682933..b2832d44 100644 --- a/lib/tasks/annotate_routes.rake +++ b/lib/tasks/annotate_routes.rake @@ -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) diff --git a/spec/lib/annotate/annotate_routes_spec.rb b/spec/lib/annotate/annotate_routes_spec.rb index 805aec66..2d27b745 100644 --- a/spec/lib/annotate/annotate_routes_spec.rb +++ b/spec/lib/annotate/annotate_routes_spec.rb @@ -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