debian-mirror-gitlab/spec/models/merge_request_diff_file_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

90 lines
2.2 KiB
Ruby
Raw Normal View History

2019-07-07 11:18:12 +05:30
# frozen_string_literal: true
2019-12-04 20:38:33 +05:30
require 'spec_helper'
2017-09-10 17:25:29 +05:30
2020-07-28 23:09:34 +05:30
RSpec.describe MergeRequestDiffFile do
2020-04-08 14:13:33 +05:30
it_behaves_like 'a BulkInsertSafe model', MergeRequestDiffFile do
2020-11-24 15:15:51 +05:30
let(:valid_items_for_bulk_insertion) do
build_list(:merge_request_diff_file, 10) do |mr_diff_file|
mr_diff_file.merge_request_diff = create(:merge_request_diff)
end
end
2020-04-08 14:13:33 +05:30
let(:invalid_items_for_bulk_insertion) { [] } # class does not have any validations defined
end
2020-03-13 15:44:24 +05:30
2017-09-10 17:25:29 +05:30
describe '#diff' do
2019-12-04 20:38:33 +05:30
context 'when diff is not stored' do
let(:unpacked) { 'unpacked' }
let(:packed) { [unpacked].pack('m0') }
2017-09-10 17:25:29 +05:30
before do
2019-12-04 20:38:33 +05:30
subject.diff = packed
end
context 'when the diff is marked as binary' do
before do
subject.binary = true
end
it 'unpacks from base 64' do
expect(subject.diff).to eq(unpacked)
end
2020-11-24 15:15:51 +05:30
context 'invalid base64' do
let(:packed) { '---/dev/null' }
it 'returns the raw diff' do
expect(subject.diff).to eq(packed)
end
end
2019-12-04 20:38:33 +05:30
end
context 'when the diff is not marked as binary' do
it 'returns the raw diff' do
expect(subject.diff).to eq(packed)
end
2017-09-10 17:25:29 +05:30
end
2019-12-04 20:38:33 +05:30
end
2017-09-10 17:25:29 +05:30
2019-12-04 20:38:33 +05:30
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
2017-09-10 17:25:29 +05:30
end
end
2019-12-04 20:38:33 +05:30
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
2017-09-10 17:25:29 +05:30
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"
expect { subject.utf8_diff }.not_to raise_error
end
end
end