Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 37b0735

Browse files
committedAug 5, 2024
Fix issue where updating annotations that include text that looks like special characters (e.g backslash-something) is incorrectly escaped
1 parent 1cfbaac commit 37b0735

File tree

2 files changed

+14
-15
lines changed

2 files changed

+14
-15
lines changed
 

‎lib/annotate/annotate_models.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,8 @@ def annotate_one_file(file_name, info_block, position, options = {})
469469
space_match = old_annotation.match(/\A(?<start>\s*).*?\n(?<end>\s*)\z/m)
470470
new_annotation = space_match[:start] + wrapped_info_block + space_match[:end]
471471

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

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

‎spec/lib/annotate/annotate_models_spec.rb

+12-14
Original file line numberDiff line numberDiff line change
@@ -2932,8 +2932,8 @@ def annotate_one_file(options = {})
29322932
mock_column(:foreign_thing_id, :integer)
29332933
],
29342934
[
2935-
mock_index('index_rails_02e851e3b7', columns: ['id']),
2936-
mock_index('index_rails_02e851e3b8', columns: ['foreign_thing_id'])
2935+
mock_index('index_rails_02e851e3b7', columns: ['id']),
2936+
mock_index('index_rails_02e851e3b8', columns: ['foreign_thing_id'])
29372937
])
29382938
@schema_info = AnnotateModels.get_schema_info(klass, '== Schema Info', show_indexes: true)
29392939
annotate_one_file
@@ -2948,12 +2948,11 @@ def annotate_one_file(options = {})
29482948
mock_column(:another_column, :integer)
29492949
],
29502950
[
2951-
mock_index('index_rails_02e851e3b7', columns: ['id']),
2952-
mock_index('index_rails_02e851e3b8', columns: ['foreign_thing_id']),
2953-
mock_index('index_rails_02e851e3b9',
2954-
columns: ['another_column'],
2955-
where: "another_column IS NOT NULL"
2956-
)
2951+
mock_index('index_rails_02e851e3b7', columns: ['id']),
2952+
mock_index('index_rails_02e851e3b8', columns: ['foreign_thing_id']),
2953+
mock_index('index_rails_02e851e3b9',
2954+
columns: ['another_column'],
2955+
where: "another_column IS NOT NULL")
29572956
])
29582957
@schema_info = AnnotateModels.get_schema_info(klass, '== Schema Info', show_indexes: true)
29592958
annotate_one_file
@@ -2969,12 +2968,11 @@ def annotate_one_file(options = {})
29692968
mock_column(:another_column, :text)
29702969
],
29712970
[
2972-
mock_index('index_rails_02e851e3b7', columns: ['id']),
2973-
mock_index('index_rails_02e851e3b8', columns: ['foreign_thing_id']),
2974-
mock_index('index_rails_02e851e3b9',
2975-
columns: ['another_column'],
2976-
where: "another_column LIKE '\\\\%'"
2977-
)
2971+
mock_index('index_rails_02e851e3b7', columns: ['id']),
2972+
mock_index('index_rails_02e851e3b8', columns: ['foreign_thing_id']),
2973+
mock_index('index_rails_02e851e3b9',
2974+
columns: ['another_column'],
2975+
where: "another_column LIKE '\\\\%'")
29782976
])
29792977
@schema_info = AnnotateModels.get_schema_info(klass, '== Schema Info', show_indexes: true)
29802978
annotate_one_file

0 commit comments

Comments
 (0)