Skip to content

Commit

Permalink
Add non ASCII encoding schema support to RailsSchemaUpToDate hook (sd…
Browse files Browse the repository at this point in the history
…s#786)

* Add non ASCII encoding schema support to RailsSchemaUpToDate hook

* Add guard to check presence of @config['encoding']

Co-authored-by: Paul Wilson <[email protected]>
  • Loading branch information
ttrmw and pajw committed Jan 20, 2023
1 parent 4d204bd commit 8bc2eec
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/overcommit/hook/pre_commit/rails_schema_up_to_date.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ def run # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplex

private

def encoding
return unless @config.key?('encoding')

{ encoding: @config['encoding'] }.compact
end

def migration_files
@migration_files ||= applicable_files.select do |file|
file.match %r{db/migrate/.*\.rb}
Expand All @@ -47,7 +53,7 @@ def schema_files
end

def schema
@schema ||= schema_files.map { |file| File.read(file) }.join
@schema ||= schema_files.map { |file| File.read(file, encoding) }.join
@schema.tr('_', '')
end

Expand Down
27 changes: 27 additions & 0 deletions spec/overcommit/hook/pre_commit/rails_schema_up_to_date_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,33 @@
end

it { should fail_hook }

context 'when non ASCII encoding is required' do
let!(:config) do
super().merge(Overcommit::Configuration.new(
'PreCommit' => {
'RailsSchemaUpToDate' => {
'encoding' => 'utf-8'
}
}
))
end

before do
subject.stub(:applicable_files).and_return([sql_schema_file])
end

around do |example|
repo do
FileUtils.mkdir_p('db/migrate')
File.open(sql_schema_file, 'w') { |f| f.write("version: 12345678901234\nVALUES ('字')") }
`git add #{sql_schema_file}`
example.run
end
end

it { should fail_hook }
end
end

context 'when a Ruby schema file with the latest version and migrations are added' do
Expand Down

0 comments on commit 8bc2eec

Please sign in to comment.