Skip to content

Commit 1e2047d

Browse files
sashabelozerovctran
authored andcommitted
Fix #436 where extra line break is added when using windows CRLF (#490)
Fix #436 where extra line break is added when using windows CRLF (#490)
1 parent 9f3a217 commit 1e2047d

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

lib/annotate/annotate_models.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ module AnnotateModels
8383
class << self
8484
def annotate_pattern(options = {})
8585
if options[:wrapper_open]
86-
return /(?:^\n?# (?:#{options[:wrapper_open]}).*\n?# (?:#{COMPAT_PREFIX}|#{COMPAT_PREFIX_MD}).*?\n(#.*\n)*\n*)|^\n?# (?:#{COMPAT_PREFIX}|#{COMPAT_PREFIX_MD}).*?\n(#.*\n)*\n*/
86+
return /(?:^(\n|\r\n)?# (?:#{options[:wrapper_open]}).*(\n|\r\n)?# (?:#{COMPAT_PREFIX}|#{COMPAT_PREFIX_MD}).*?(\n|\r\n)(#.*(\n|\r\n))*(\n|\r\n)*)|^(\n|\r\n)?# (?:#{COMPAT_PREFIX}|#{COMPAT_PREFIX_MD}).*?(\n|\r\n)(#.*(\n|\r\n))*(\n|\r\n)*/
8787
end
88-
/^\n?# (?:#{COMPAT_PREFIX}|#{COMPAT_PREFIX_MD}).*?\n(#.*\n)*\n*/
88+
/^(\n|\r\n)?# (?:#{COMPAT_PREFIX}|#{COMPAT_PREFIX_MD}).*?(\n|\r\n)(#.*(\n|\r\n))*(\n|\r\n)*/
8989
end
9090

9191
def model_dir

spec/annotate/annotate_models_spec.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,6 +1153,28 @@ def content(path)
11531153
# updated_at :datetime
11541154
#
11551155
1156+
class Foo < ActiveRecord::Base
1157+
end
1158+
EOS
1159+
1160+
AnnotateModels.remove_annotation_of_file(path)
1161+
1162+
expect(content(path)).to eq <<-EOS
1163+
class Foo < ActiveRecord::Base
1164+
end
1165+
EOS
1166+
end
1167+
1168+
it 'should remove annotate if CRLF is used for line breaks' do
1169+
path = create 'before.rb', <<-EOS
1170+
# == Schema Information
1171+
#
1172+
# Table name: foo\r\n#
1173+
# id :integer not null, primary key
1174+
# created_at :datetime
1175+
# updated_at :datetime
1176+
#
1177+
\r\n
11561178
class Foo < ActiveRecord::Base
11571179
end
11581180
EOS
@@ -1201,6 +1223,29 @@ class Foo < ActiveRecord::Base
12011223
# updated_at :datetime
12021224
#
12031225
1226+
class Foo < ActiveRecord::Base
1227+
end
1228+
EOS
1229+
1230+
AnnotateModels.remove_annotation_of_file(path, wrapper_open: 'wrapper')
1231+
1232+
expect(content(path)).to eq <<-EOS
1233+
class Foo < ActiveRecord::Base
1234+
end
1235+
EOS
1236+
end
1237+
1238+
it 'should remove wrapper if CRLF is used for line breaks' do
1239+
path = create 'opening_wrapper.rb', <<-EOS
1240+
# wrapper\r\n# == Schema Information
1241+
#
1242+
# Table name: foo
1243+
#
1244+
# id :integer not null, primary key
1245+
# created_at :datetime
1246+
# updated_at :datetime
1247+
#
1248+
12041249
class Foo < ActiveRecord::Base
12051250
end
12061251
EOS

0 commit comments

Comments
 (0)