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

Fix backslashes incorrectly escaped #1026

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion lib/annotate/annotate_models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,8 @@ def annotate_one_file(file_name, info_block, position, options = {})
space_match = old_annotation.match(/\A(?<start>\s*).*?\n(?<end>\s*)\z/m)
new_annotation = space_match[:start] + wrapped_info_block + space_match[:end]

new_content = old_content.sub(annotate_pattern(options), new_annotation)
# use the block version of sub to avoid interpreting special characters
new_content = old_content.sub(annotate_pattern(options)) { |_match| new_annotation }
end

File.open(file_name, 'wb') { |f| f.puts new_content }
Expand Down
57 changes: 57 additions & 0 deletions spec/lib/annotate/annotate_models_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2922,6 +2922,63 @@ def annotate_one_file(options = {})
expect(File.read(@model_file_name)).to eq("#{@schema_info}#{@file_content}")
end
end

context 'of an index' do
before do
klass = mock_class(:users,
:id,
[
mock_column(:id, :integer),
mock_column(:foreign_thing_id, :integer)
],
[
mock_index('index_rails_02e851e3b7', columns: ['id']),
mock_index('index_rails_02e851e3b8', columns: ['foreign_thing_id'])
])
@schema_info = AnnotateModels.get_schema_info(klass, '== Schema Info', show_indexes: true)
annotate_one_file
end

it 'should update index' do
klass = mock_class(:users,
:id,
[
mock_column(:id, :integer),
mock_column(:foreign_thing_id, :integer),
mock_column(:another_column, :integer)
],
[
mock_index('index_rails_02e851e3b7', columns: ['id']),
mock_index('index_rails_02e851e3b8', columns: ['foreign_thing_id']),
mock_index('index_rails_02e851e3b9',
columns: ['another_column'],
where: "another_column IS NOT NULL")
])
@schema_info = AnnotateModels.get_schema_info(klass, '== Schema Info', show_indexes: true)
annotate_one_file
expect(File.read(@model_file_name)).to eq("#{@schema_info}#{@file_content}")
end

it 'should update index without escaping backslashes' do
klass = mock_class(:users,
:id,
[
mock_column(:id, :integer),
mock_column(:foreign_thing_id, :integer),
mock_column(:another_column, :text)
],
[
mock_index('index_rails_02e851e3b7', columns: ['id']),
mock_index('index_rails_02e851e3b8', columns: ['foreign_thing_id']),
mock_index('index_rails_02e851e3b9',
columns: ['another_column'],
where: "another_column LIKE '\\\\%'")
])
@schema_info = AnnotateModels.get_schema_info(klass, '== Schema Info', show_indexes: true)
annotate_one_file
expect(File.read(@model_file_name)).to eq("#{@schema_info}#{@file_content}")
end
end
end

describe 'with existing annotation => :before' do
Expand Down
Loading