Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jesse/newline bug fix #1016

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions lib/annotate/annotate_models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ module AnnotateModels
}
}.freeze

MAGIC_COMMENT_MATCHER = Regexp.new(/(^#\s*encoding:.*(?:\n|r\n))|(^# coding:.*(?:\n|\r\n))|(^# -\*- coding:.*(?:\n|\r\n))|(^# -\*- encoding\s?:.*(?:\n|\r\n))|(^#\s*frozen_string_literal:.+(?:\n|\r\n))|(^# -\*- frozen_string_literal\s*:.+-\*-(?:\n|\r\n))/).freeze
MAGIC_COMMENT_MATCHER = Regexp.new(/(^#\s*encoding:.*(?:\n|r\n))|(^# coding:.*(?:\n|\r\n))|(^# -\*- coding:.*(?:\n|\r\n))|(^# -\*- encoding\s?:.*(?:\n|\r\n))|(^#\s*frozen_string_literal:.+(?:\n|\r\n))|(^# -\*- frozen_string_literal\s*:.+-\*-(?:\n|\r\n))|(^#\s*typed:.+(?:\n|\r\n))/).freeze

class << self
def annotate_pattern(options = {})
Expand Down Expand Up @@ -208,6 +208,10 @@ def get_schema_info(klass, header, options = {}) # rubocop:disable Metrics/Metho
end

info << get_schema_footer_text(klass, options)

info.chomp!("#\n") if options[:with_trailing_newline]

info
end

def get_schema_header_text(klass, options = {})
Expand Down Expand Up @@ -459,6 +463,8 @@ def annotate_one_file(file_name, info_block, position, options = {})
magic_comments_block + (old_content.rstrip + "\n\n" + wrapped_info_block)
elsif magic_comments_block.empty?
magic_comments_block + wrapped_info_block + old_content.lstrip
elsif options[:with_trailing_newline]
magic_comments_block + "\n" + wrapped_info_block + old_content
else
magic_comments_block + "\n" + wrapped_info_block + old_content.lstrip
end
Expand Down Expand Up @@ -492,7 +498,8 @@ def remove_annotation_of_file(file_name, options = {})
return false if content =~ /#{SKIP_ANNOTATION_PREFIX}.*\n/

wrapper_open = options[:wrapper_open] ? "# #{options[:wrapper_open]}\n" : ''
content.sub!(/(#{wrapper_open})?#{annotate_pattern(options)}/, '')
content_end = options[:with_trailing_newline] ? "\n" : ''
content.sub!(/(#{wrapper_open})?#{annotate_pattern(options)}/, content_end)

File.open(file_name, 'wb') { |f| f.puts content }

Expand Down
2 changes: 1 addition & 1 deletion lib/annotate/constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module Constants
OTHER_OPTIONS = [
:additional_file_patterns, :ignore_columns, :skip_on_db_migrate, :wrapper_open, :wrapper_close,
:wrapper, :routes, :models, :hide_limit_column_types, :hide_default_column_types,
:ignore_routes, :active_admin
:ignore_routes, :active_admin, :with_trailing_newline
].freeze

PATH_OPTIONS = [
Expand Down
5 changes: 5 additions & 0 deletions lib/annotate/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,11 @@ def add_options_to_parser(option_parser) # rubocop:disable Metrics/MethodLength,
"include database comments in model annotations, as its own column, after all others") do
env['with_comment_column'] = 'true'
end

option_parser.on('--with-trailing-newline',
"include a trailing newline in the annotation") do
env['with_trailing_newline'] = 'true'
end
end
end
end
78 changes: 76 additions & 2 deletions spec/lib/annotate/annotate_models_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@
"# frozen_string_literal: true\n# encoding: utf-8",
'# frozen_string_literal: true',
'#frozen_string_literal: false',
'# -*- frozen_string_literal : true -*-'
'# -*- frozen_string_literal : true -*-',
'# typed: false',
'# typed: true',
'# typed: strict',
'# typed: strong',
].freeze unless const_defined?(:MAGIC_COMMENTS)

def mock_index(name, params = {})
Expand Down Expand Up @@ -1449,6 +1453,35 @@ def mock_column(name, type, options = {})
end
end
end

context 'when "with_trailing_newline" is specified in options' do
let :options do
{ with_trailing_newline: true }
end

let :columns do
[
mock_column(:id, :integer, limit: 8),
mock_column(:name, :string, limit: 50)
]
end

let :expected_result do
<<~EOS
# Schema Info
#
# Table name: users
#
# id :integer not null, primary key
# name :string(50) not null

EOS
end

it 'works with option "with_trailing_newline"' do
is_expected.to eq expected_result
end
end
end
end
end
Expand Down Expand Up @@ -2527,7 +2560,7 @@ class Foo < ActiveRecord::Base

describe '.remove_annotation_of_file' do
subject do
AnnotateModels.remove_annotation_of_file(path)
AnnotateModels.remove_annotation_of_file(path, options)
end

after :each do
Expand Down Expand Up @@ -2558,6 +2591,10 @@ class Foo < ActiveRecord::Base
EOS
end

let :options do
{}
end

context 'when annotation is before main content' do
let :filename do
'before.rb'
Expand Down Expand Up @@ -2759,6 +2796,43 @@ class Foo < ActiveRecord::Base
expect(file_content_after_removal).to eq expected_result
end
end

context 'when option "with_trailing_newline" is specified' do
let :options do
{ with_trailing_newline: true }
end

let :filename do
'before.rb'
end

let :file_content do
<<~EOS
# == Schema Information
#
# Table name: foo
#
# id :integer not null, primary key
# created_at :datetime
# updated_at :datetime

class Foo < ActiveRecord::Base
end
EOS
end

let :expected_result do
<<~EOS

class Foo < ActiveRecord::Base
end
EOS
end

it 'removes annotation and leaves newline before class declaration' do
expect(file_content_after_removal).to eq expected_result
end
end
end

describe '.resolve_filename' do
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/annotate/annotate_routes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
'# frozen_string_literal: true',
'#frozen_string_literal: false',
'# -*- frozen_string_literal : true -*-'
].freeze unless const_defined?(:MAGIC_COMMENTS)
].freeze

let :stubs do
{}
Expand Down
Loading