Skip to content

Commit 10a7a76

Browse files
kg8mctran
andauthored
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: kg8m <[email protected]> Co-authored-by: Cuong Tran <[email protected]>
1 parent a28fef3 commit 10a7a76

File tree

3 files changed

+76
-9
lines changed

3 files changed

+76
-9
lines changed

lib/annotate/annotate_routes.rb

+10-9
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ def do_annotations(options = {})
2929
content, header_position = Helpers.strip_annotations(existing_text)
3030
new_content = annotate_routes(HeaderGenerator.generate(options), content, header_position, options)
3131
new_text = new_content.join("\n")
32-
33-
if rewrite_contents(existing_text, new_text)
32+
if rewrite_contents(existing_text, new_text, options[:frozen])
3433
puts "#{routes_file} was annotated."
3534
else
3635
puts "#{routes_file} was not changed."
@@ -40,13 +39,13 @@ def do_annotations(options = {})
4039
end
4140
end
4241

43-
def remove_annotations(_options={})
42+
def remove_annotations(options={})
4443
if routes_file_exist?
4544
existing_text = File.read(routes_file)
4645
content, header_position = Helpers.strip_annotations(existing_text)
4746
new_content = strip_on_removal(content, header_position)
4847
new_text = new_content.join("\n")
49-
if rewrite_contents(existing_text, new_text)
48+
if rewrite_contents(existing_text, new_text, options[:frozen])
5049
puts "Annotations were removed from #{routes_file}."
5150
else
5251
puts "#{routes_file} was not changed (Annotation did not exist)."
@@ -82,13 +81,15 @@ def strip_on_removal(content, header_position)
8281
content
8382
end
8483

85-
def rewrite_contents(existing_text, new_text)
86-
if existing_text == new_text
87-
false
88-
else
84+
def rewrite_contents(existing_text, new_text, frozen)
85+
content_changed = (existing_text != new_text)
86+
87+
if content_changed
88+
abort "annotate error. #{routes_file} needs to be updated, but annotate was run with `--frozen`." if frozen
8989
File.open(routes_file, 'wb') { |f| f.puts(new_text) }
90-
true
9190
end
91+
92+
content_changed
9293
end
9394

9495
def annotate_routes(header, content, header_position, options = {})

lib/tasks/annotate_routes.rake

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ task :annotate_routes => :environment do
1414
options[:position_in_routes] = Annotate::Helpers.fallback(ENV['position_in_routes'], ENV['position'])
1515
options[:ignore_routes] = Annotate::Helpers.fallback(ENV['ignore_routes'], nil)
1616
options[:require] = ENV['require'] ? ENV['require'].split(',') : []
17+
options[:frozen] = Annotate::Helpers.true?(ENV['frozen'])
1718
options[:wrapper_open] = Annotate::Helpers.fallback(ENV['wrapper_open'], ENV['wrapper'])
1819
options[:wrapper_close] = Annotate::Helpers.fallback(ENV['wrapper_close'], ENV['wrapper'])
1920
AnnotateRoutes.do_annotations(options)

spec/lib/annotate/annotate_routes_spec.rb

+65
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,71 @@
556556
end
557557
end
558558
end
559+
560+
describe 'frozen option' do
561+
let :aborted_message do
562+
"annotate error. #{ROUTE_FILE} needs to be updated, but annotate was run with `--frozen`."
563+
end
564+
565+
let :rake_routes_result do
566+
<<-EOS
567+
Prefix Verb URI Pattern Controller#Action
568+
myaction1 GET /url1(.:format) mycontroller1#action
569+
myaction2 POST /url2(.:format) mycontroller2#action
570+
myaction3 DELETE|GET /url3(.:format) mycontroller3#action
571+
EOS
572+
end
573+
574+
before :each do
575+
expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true).once
576+
expect(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content).once
577+
578+
expect(AnnotateRoutes::HeaderGenerator).to receive(:`).with('rake routes').and_return(rake_routes_result).once
579+
end
580+
581+
context 'when annotation does not exists' do
582+
let :route_file_content do
583+
''
584+
end
585+
586+
it 'aborts' do
587+
expect { AnnotateRoutes.do_annotations(frozen: true) }.to raise_error SystemExit, aborted_message
588+
end
589+
end
590+
591+
context 'when annotation exists but is not updated' do
592+
let :route_file_content do
593+
<<~EOS
594+
# == Route Map
595+
#
596+
# Prefix Verb URI Pattern Controller#Action
597+
# myaction2 POST /url2(.:format) mycontroller2#action
598+
# myaction3 DELETE|GET /url3(.:format) mycontroller3#action
599+
EOS
600+
end
601+
602+
it 'aborts' do
603+
expect { AnnotateRoutes.do_annotations(frozen: true) }.to raise_error SystemExit, aborted_message
604+
end
605+
end
606+
607+
context 'when annotation exists and is already updated' do
608+
let :route_file_content do
609+
<<~EOS
610+
# == Route Map
611+
#
612+
# Prefix Verb URI Pattern Controller#Action
613+
# myaction1 GET /url1(.:format) mycontroller1#action
614+
# myaction2 POST /url2(.:format) mycontroller2#action
615+
# myaction3 DELETE|GET /url3(.:format) mycontroller3#action
616+
EOS
617+
end
618+
619+
it 'does NOT abort' do
620+
expect { AnnotateRoutes.do_annotations(frozen: true) }.not_to raise_error
621+
end
622+
end
623+
end
559624
end
560625

561626
describe '.remove_annotations' do

0 commit comments

Comments
 (0)