Commit d02ee542 authored by Nick Thomas's avatar Nick Thomas

Merge branch '61841-fix-encoding-error-in-mr-diffs' into 'master'

Fix encoding error in MR diffs

Closes #61841

See merge request gitlab-org/gitlab-ce!32862
parents 6d437bc7 ce46b402
......@@ -17,7 +17,7 @@ class MergeRequestDiffFile < ApplicationRecord
if merge_request_diff&.stored_externally?
merge_request_diff.opening_external_diff do |file|
file.seek(external_diff_offset)
file.read(external_diff_size)
force_encode_utf8(file.read(external_diff_size))
end
else
super
......
---
title: Fix encoding error in MR diffs when using external diffs
merge_request: 32862
author: Hiroyuki Sato
type: fixed
......@@ -4,6 +4,7 @@ require 'spec_helper'
describe MergeRequestDiffFile do
describe '#diff' do
context 'when diff is not stored' do
let(:unpacked) { 'unpacked' }
let(:packed) { [unpacked].pack('m0') }
......@@ -28,6 +29,38 @@ describe MergeRequestDiffFile do
end
end
context 'when diff is stored in DB' do
let(:file) { create(:merge_request).merge_request_diff.merge_request_diff_files.first }
it 'returns UTF-8 string' do
expect(file.diff.encoding).to eq Encoding::UTF_8
end
end
context 'when diff is stored in external storage' do
let(:file) { create(:merge_request).merge_request_diff.merge_request_diff_files.first }
let(:test_dir) { 'tmp/tests/external-diffs' }
around do |example|
FileUtils.mkdir_p(test_dir)
begin
example.run
ensure
FileUtils.rm_rf(test_dir)
end
end
before do
stub_external_diffs_setting(enabled: true, storage_path: test_dir)
end
it 'returns UTF-8 string' do
expect(file.diff.encoding).to eq Encoding::UTF_8
end
end
end
describe '#utf8_diff' do
it 'does not raise error when the diff is binary' do
subject.diff = "\x05\x00\x68\x65\x6c\x6c\x6f"
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment