From 1cfbaac93c885787287329832d79c5e12210129d Mon Sep 17 00:00:00 2001 From: Alex Pech Date: Mon, 5 Aug 2024 13:12:07 +1000 Subject: [PATCH 1/2] Add failing test for bug where annotations incorrectly escape backslashes --- spec/lib/annotate/annotate_models_spec.rb | 59 +++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/spec/lib/annotate/annotate_models_spec.rb b/spec/lib/annotate/annotate_models_spec.rb index 09647461..aa069f29 100644 --- a/spec/lib/annotate/annotate_models_spec.rb +++ b/spec/lib/annotate/annotate_models_spec.rb @@ -2922,6 +2922,65 @@ 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 From 37b073594730215b10f5a0af4e6577ab756f010f Mon Sep 17 00:00:00 2001 From: Alex Pech Date: Mon, 5 Aug 2024 13:17:39 +1000 Subject: [PATCH 2/2] Fix issue where updating annotations that include text that looks like special characters (e.g backslash-something) is incorrectly escaped --- lib/annotate/annotate_models.rb | 3 ++- spec/lib/annotate/annotate_models_spec.rb | 26 +++++++++++------------ 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/lib/annotate/annotate_models.rb b/lib/annotate/annotate_models.rb index dc2901a3..2aceb639 100644 --- a/lib/annotate/annotate_models.rb +++ b/lib/annotate/annotate_models.rb @@ -469,7 +469,8 @@ def annotate_one_file(file_name, info_block, position, options = {}) space_match = old_annotation.match(/\A(?\s*).*?\n(?\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 } diff --git a/spec/lib/annotate/annotate_models_spec.rb b/spec/lib/annotate/annotate_models_spec.rb index aa069f29..a8b391e9 100644 --- a/spec/lib/annotate/annotate_models_spec.rb +++ b/spec/lib/annotate/annotate_models_spec.rb @@ -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 @@ -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 @@ -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