Commit 0ecf9ab8 authored by Gary Holtz's avatar Gary Holtz

Adjusting some specs based on review

parent d67b22ad
--- ---
name: convert_diff_to_utf8_with_replacement_symbol name: convert_diff_to_utf8_with_replacement_symbol
introduced_by_url: introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/79996
rollout_issue_url: rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/354526
milestone: '14.9' milestone: '14.9'
type: development type: development
group: group::code review group: group::code review
......
...@@ -158,9 +158,7 @@ module Gitlab ...@@ -158,9 +158,7 @@ module Gitlab
raise "Invalid raw diff type: #{raw_diff.class}" raise "Invalid raw diff type: #{raw_diff.class}"
end end
if Feature.enabled?(:convert_diff_to_utf8_with_replacement_symbol, default_enabled: :yaml) encode_diff_to_utf8(replace_invalid_utf8_chars)
@diff = Gitlab::EncodingHelper.encode_utf8_with_replacement_character(@diff) if replace_invalid_utf8_chars && !detect_binary?(@diff)
end
end end
def to_hash def to_hash
...@@ -231,6 +229,13 @@ module Gitlab ...@@ -231,6 +229,13 @@ module Gitlab
private private
def encode_diff_to_utf8(replace_invalid_utf8_chars)
return unless Feature.enabled?(:convert_diff_to_utf8_with_replacement_symbol, default_enabled: :yaml)
return unless replace_invalid_utf8_chars && !detect_binary?(@diff)
@diff = Gitlab::EncodingHelper.encode_utf8_with_replacement_character(@diff)
end
def init_from_hash(hash) def init_from_hash(hash)
raw_diff = hash.symbolize_keys raw_diff = hash.symbolize_keys
......
...@@ -166,23 +166,44 @@ EOT ...@@ -166,23 +166,44 @@ EOT
let(:bad_string) { [0xae].pack("C*") } let(:bad_string) { [0xae].pack("C*") }
let(:bad_string_two) { [0x89].pack("C*") } let(:bad_string_two) { [0x89].pack("C*") }
context 'when replace_invalid_utf8_chars is true' do let(:diff) { described_class.new(@raw_diff_hash.merge({ diff: bad_string })) }
let(:diff) { described_class.new(@raw_diff_hash.merge({ diff: bad_string })) } let(:diff_two) { described_class.new(@raw_diff_hash.merge({ diff: bad_string_two })) }
let(:diff_two) { described_class.new(@raw_diff_hash.merge({ diff: bad_string_two })) }
context 'when replace_invalid_utf8_chars is true' do
it 'will convert invalid characters and not cause an encoding error' do it 'will convert invalid characters and not cause an encoding error' do
expect(diff.diff).to include(Gitlab::EncodingHelper::UNICODE_REPLACEMENT_CHARACTER)
expect(diff_two.diff).to include(Gitlab::EncodingHelper::UNICODE_REPLACEMENT_CHARACTER)
expect { Oj.dump(diff) }.not_to raise_error(EncodingError) expect { Oj.dump(diff) }.not_to raise_error(EncodingError)
expect { Oj.dump(diff_two) }.not_to raise_error(EncodingError) expect { Oj.dump(diff_two) }.not_to raise_error(EncodingError)
end end
context 'when the diff is binary' do
let(:project) { create(:project, :repository) }
it 'will not try to replace characters' do
expect(Gitlab::EncodingHelper).not_to receive(:encode_utf8_with_replacement_character?)
expect(binary_diff(project).diff).not_to be_empty
end
end
context 'when convert_diff_to_utf8_with_replacement_symbol feature flag is disabled' do
before do
stub_feature_flags(convert_diff_to_utf8_with_replacement_symbol: false)
end
it 'will not try to convert invalid characters' do
expect(Gitlab::EncodingHelper).not_to receive(:encode_utf8_with_replacement_character?)
end
end
end end
context 'when replace_invalid_utf8_chars is false' do context 'when replace_invalid_utf8_chars is false' do
let(:diff) { described_class.new(@raw_diff_hash.merge({ diff: bad_string }), replace_invalid_utf8_chars: false) } let(:not_replaced_diff) { described_class.new(@raw_diff_hash.merge({ diff: bad_string, replace_invalid_utf8_chars: false }) ) }
let(:diff_two) { described_class.new(@raw_diff_hash.merge({ diff: bad_string_two }), replace_invalid_utf8_chars: false) } let(:not_replaced_diff_two) { described_class.new(@raw_diff_hash.merge({ diff: bad_string_two, replace_invalid_utf8_chars: false }) ) }
it 'will not convert any invalid characters' do it 'will not try to convert invalid characters' do
expect(diff).not_to include(Gitlab::EncodingHelper::UNICODE_REPLACEMENT_CHARACTER) expect(Gitlab::EncodingHelper).not_to receive(:encode_utf8_with_replacement_character?)
expect(diff_two).not_to include(Gitlab::EncodingHelper::UNICODE_REPLACEMENT_CHARACTER)
end end
end end
end end
...@@ -280,12 +301,11 @@ EOT ...@@ -280,12 +301,11 @@ EOT
let(:project) { create(:project, :repository) } let(:project) { create(:project, :repository) }
it 'fake binary message when it detects binary' do it 'fake binary message when it detects binary' do
# Rugged will not detect this as binary, but we can fake it
diff_message = "Binary files files/images/icn-time-tracking.pdf and files/images/icn-time-tracking.pdf differ\n" diff_message = "Binary files files/images/icn-time-tracking.pdf and files/images/icn-time-tracking.pdf differ\n"
binary_diff = described_class.between(project.repository, 'add-pdf-text-binary', 'add-pdf-text-binary^').first
expect(binary_diff.diff).not_to be_empty diff = binary_diff(project)
expect(binary_diff.json_safe_diff).to eq(diff_message) expect(diff.diff).not_to be_empty
expect(diff.json_safe_diff).to eq(diff_message)
end end
it 'leave non-binary diffs as-is' do it 'leave non-binary diffs as-is' do
...@@ -399,4 +419,9 @@ EOT ...@@ -399,4 +419,9 @@ EOT
expect(diff.line_count).to eq(0) expect(diff.line_count).to eq(0)
end end
end end
def binary_diff(project)
# rugged will not detect this as binary, but we can fake it
described_class.between(project.repository, 'add-pdf-text-binary', 'add-pdf-text-binary^').first
end
end end
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