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

77 lines
1.9 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
describe MergeRequestDiffFile do
2020-04-08 14:13:33 +05:30
it_behaves_like 'a BulkInsertSafe model', MergeRequestDiffFile do
let(:valid_items_for_bulk_insertion) { build_list(:merge_request_diff_file, 10) }
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
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