diff --git a/lib/annotate/annotate_routes/helpers.rb b/lib/annotate/annotate_routes/helpers.rb index 1dba65b..0fa66cc 100644 --- a/lib/annotate/annotate_routes/helpers.rb +++ b/lib/annotate/annotate_routes/helpers.rb @@ -16,7 +16,7 @@ def strip_annotations(content) header_position = 0 content.split(/\n/, -1).each_with_index do |line, line_number| - if mode == :header && line !~ /\s*#/ + if mode == :header && !line.include?('#') mode = :content real_content << line unless line.blank? elsif mode == :content diff --git a/spec/lib/annotate/annotate_routes/helpers_spec.rb b/spec/lib/annotate/annotate_routes/helpers_spec.rb new file mode 100644 index 0000000..cee334a --- /dev/null +++ b/spec/lib/annotate/annotate_routes/helpers_spec.rb @@ -0,0 +1,30 @@ +require_relative '../../../spec_helper' +require 'annotate/annotate_routes' + +RSpec.describe AnnotateRoutes::Helpers do + describe '.strip_annotations' do + it 'removes the route annotation block and keeps the content after it' do + content = "# == Route Map\n#\n# root GET / home#index\n\nRails.application.routes.draw do\nend\n" + + real_content, header_position = described_class.strip_annotations(content) + + expect(real_content).to eq(['Rails.application.routes.draw do', 'end', '']) + expect(header_position).to eq(:before) + end + + it 'handles a long whitespace-only line in the annotation block in linear time' do + content = "# == Route Map\n#{' ' * 100_000}\nRails.application.routes.draw do\nend\n" + + (real_content,), elapsed = timed { described_class.strip_annotations(content) } + + expect(real_content).to eq(['Rails.application.routes.draw do', 'end', '']) + expect(elapsed).to be < 1 + end + + def timed + started = Process.clock_gettime(Process::CLOCK_MONOTONIC) + result = yield + [result, Process.clock_gettime(Process::CLOCK_MONOTONIC) - started] + end + end +end