Skip to content

Commit

Permalink
Show regex does not work
Browse files Browse the repository at this point in the history
  • Loading branch information
drwl committed May 23, 2023
1 parent 2472836 commit 5664ea5
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/annotate_rb/model_annotator/annotation_diff_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ 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]?)/.freeze
COLUMN_PATTERN = /^#[\t ]+[\w\*\.`\[\]():]+[\t ]+.+$/.freeze
# COLUMN_PATTERN = /^#[\t ]+[\w\*\.`\[\]():]+[\t ]+.+$/.freeze
COLUMN_PATTERN = /^#[\t ]+[^\t ]+[\t ]+.+$/.freeze

class << self
def call(file_content, annotation_block)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# frozen_string_literal: true

RSpec.describe AnnotateRb::ModelAnnotator::AnnotationDiffGenerator do
include AnnotateTestHelpers

def test_columns_match_expected
remove_whitespace = Proc.new { |str| str.delete(" \t\r\n") }
remove_whitespace = Proc.new { |str| str.delete(" \t\r\n") }

resulting_current_columns_data = subject.current_columns.map(&remove_whitespace)
expected_current_columns_data = current_columns.map(&remove_whitespace)
Expand Down Expand Up @@ -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

0 comments on commit 5664ea5

Please sign in to comment.