Skip to content

Commit 0e1a56d

Browse files
committed
Add non ASCII encoding schema support to RailsSchemaUpToDate hook
1 parent a2ab659 commit 0e1a56d

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

lib/overcommit/hook/pre_commit/rails_schema_up_to_date.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ def run # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplex
3434

3535
private
3636

37+
def encoding
38+
{ encoding: @config['encoding'] }.compact
39+
end
40+
3741
def migration_files
3842
@migration_files ||= applicable_files.select do |file|
3943
file.match %r{db/migrate/.*\.rb}
@@ -47,7 +51,7 @@ def schema_files
4751
end
4852

4953
def schema
50-
@schema ||= schema_files.map { |file| File.read(file) }.join
54+
@schema ||= schema_files.map { |file| File.read(file, encoding) }.join
5155
@schema.tr('_', '')
5256
end
5357

spec/overcommit/hook/pre_commit/rails_schema_up_to_date_spec.rb

+27
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,33 @@
7070
end
7171

7272
it { should fail_hook }
73+
74+
context 'when non ASCII encoding is required' do
75+
let!(:config) do
76+
super().merge(Overcommit::Configuration.new(
77+
'PreCommit' => {
78+
'RailsSchemaUpToDate' => {
79+
'encoding' => 'utf-8'
80+
}
81+
}
82+
))
83+
end
84+
85+
before do
86+
subject.stub(:applicable_files).and_return([sql_schema_file])
87+
end
88+
89+
around do |example|
90+
repo do
91+
FileUtils.mkdir_p('db/migrate')
92+
File.open(sql_schema_file, 'w') { |f| f.write("version: 12345678901234\nVALUES ('字')") }
93+
`git add #{sql_schema_file}`
94+
example.run
95+
end
96+
end
97+
98+
it { should fail_hook }
99+
end
73100
end
74101

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

0 commit comments

Comments
 (0)