diff --git a/lib/annotate_rb/model_annotator/annotation_diff_generator.rb b/lib/annotate_rb/model_annotator/annotation_diff_generator.rb index bef8e9cf..95012e61 100644 --- a/lib/annotate_rb/model_annotator/annotation_diff_generator.rb +++ b/lib/annotate_rb/model_annotator/annotation_diff_generator.rb @@ -4,8 +4,9 @@ module AnnotateRb module ModelAnnotator # Compares the current file content and new annotation block and generates the column annotation differences class AnnotationDiffGenerator - HEADER_PATTERN = /(^# Table name:.*?\n(#.*\r?\n)*\r?)/ - COLUMN_PATTERN = /^#[\t ]+[\w*.`\[\]():]+[\t ]+.+$/ + HEADER_PATTERN = /(^# Table name:.*?\n(#.*\r?\n)*\r?)/.freeze + # COLUMN_PATTERN = /^#[\t ]+[\w\*\.`\[\]():]+[\t ]+.+$/.freeze + COLUMN_PATTERN = /^#[\t ]+[^\t ]+[\t ]+.+$/.freeze class << self def call(file_content, annotation_block) diff --git a/spec/lib/annotate_rb/model_annotator/annotation_diff_generator_spec.rb b/spec/lib/annotate_rb/model_annotator/annotation_diff_generator_spec.rb index 8449ed7d..f1882f8a 100644 --- a/spec/lib/annotate_rb/model_annotator/annotation_diff_generator_spec.rb +++ b/spec/lib/annotate_rb/model_annotator/annotation_diff_generator_spec.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true RSpec.describe AnnotateRb::ModelAnnotator::AnnotationDiffGenerator do + include AnnotateTestHelpers + def test_columns_match_expected remove_whitespace = proc { |str| str.delete(" \t\r\n") } @@ -142,5 +144,53 @@ class User < ApplicationRecord expect(subject.changed?).to eq(true) end end + + context "when model file has existing annotations with column multi-byte comments" do + let(:annotation_block) do + klass = mock_class(:users, + :id, + [ + mock_column(:id, :bigint, comment: "ID"), + mock_column(:active, :boolean, limit: 1, comment: "ACTIVE") + ], + [], + []) + options = AnnotateRb::Options.from({}) + AnnotateRb::ModelAnnotator::AnnotationBuilder.new(klass, options).build + end + + let(:file_content) do + <<~FILE + # == Schema Information + # + # Table name: users + # + # id(ID) :bigint not null, primary key + # + class User < ApplicationRecord + end + FILE + end + + let(:current_columns) do + [ + "# id(ID) :bigint not null, primary key", + "# Table name: users" + ] + end + let(:new_columns) do + [ + "# id(ID) :bigint not null, primary key", + "# active(ACTIVE) :boolean not null", + "# Table name: users" + ] + end + + it "returns an AnnotationDiff object with the expected old and new columns" do + test_columns_match_expected + + expect(subject.changed?).to eq(true) + end + end end end