From 5e9e1d8533701709ad56e27a23c71d6d9cd9e5db Mon Sep 17 00:00:00 2001 From: Kyle Zhao Date: Sat, 5 Oct 2019 12:19:23 -0700 Subject: [PATCH] FIX: Ensure only one line is around the annotation --- lib/annotate/annotate_models.rb | 4 ++-- spec/lib/annotate/annotate_models_spec.rb | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/annotate/annotate_models.rb b/lib/annotate/annotate_models.rb index b306d0b84..6c1c7c1c3 100644 --- a/lib/annotate/annotate_models.rb +++ b/lib/annotate/annotate_models.rb @@ -540,9 +540,9 @@ def annotate_one_file(file_name, info_block, position, options = {}) new_content = if %w(after bottom).include?(options[position].to_s) magic_comments_block + (old_content.rstrip + "\n\n" + wrapped_info_block) elsif magic_comments_block.empty? - magic_comments_block + wrapped_info_block + "\n" + old_content + magic_comments_block + wrapped_info_block + "\n" + old_content.lstrip else - magic_comments_block + "\n" + wrapped_info_block + "\n" + old_content + magic_comments_block + "\n" + wrapped_info_block + "\n" + old_content.lstrip end else # replace the old annotation with the new one diff --git a/spec/lib/annotate/annotate_models_spec.rb b/spec/lib/annotate/annotate_models_spec.rb index f95a9e290..7d177536b 100644 --- a/spec/lib/annotate/annotate_models_spec.rb +++ b/spec/lib/annotate/annotate_models_spec.rb @@ -1781,6 +1781,18 @@ class User < ActiveRecord::Base end end + it 'only keeps a single empty line around the annotation (position :before)' do + content = "class User < ActiveRecord::Base\nend\n" + magic_comments_list_each do |magic_comment| + schema_info = AnnotateModels.get_schema_info(@klass, '== Schema Info') + model_file_name, = write_model 'user.rb', "#{magic_comment}\n\n\n\n#{content}" + + annotate_one_file position: :before + + expect(File.read(model_file_name)).to eq("#{magic_comment}\n\n#{schema_info}\n#{content}") + end + end + it 'does not change whitespace between magic comments and model file content (position :after)' do content = "class User < ActiveRecord::Base\nend\n" magic_comments_list_each do |magic_comment|