Skip to content

Commit

Permalink
Fix issue where updating annotations that include text that looks lik…
Browse files Browse the repository at this point in the history
…e special characters (e.g backslash-something) is incorrectly escaped
  • Loading branch information
alexpech12 committed Aug 5, 2024
1 parent 1cfbaac commit 37b0735
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
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
26 changes: 12 additions & 14 deletions spec/lib/annotate/annotate_models_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2932,8 +2932,8 @@ def annotate_one_file(options = {})
mock_column(:foreign_thing_id, :integer)
],
[
mock_index('index_rails_02e851e3b7', columns: ['id']),
mock_index('index_rails_02e851e3b8', columns: ['foreign_thing_id'])
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
Expand All @@ -2948,12 +2948,11 @@ def annotate_one_file(options = {})
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"
)
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
Expand All @@ -2969,12 +2968,11 @@ def annotate_one_file(options = {})
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 '\\\\%'"
)
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
Expand Down

0 comments on commit 37b0735

Please sign in to comment.