2019-04-11 08:17:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-01-11 18:12:34 -05:00
|
|
|
require "spec_helper"
|
|
|
|
|
|
|
|
describe Lfs::FileTransformer do
|
|
|
|
let(:project) { create(:project, :repository) }
|
|
|
|
let(:repository) { project.repository }
|
|
|
|
let(:file_content) { 'Test file content' }
|
|
|
|
let(:branch_name) { 'lfs' }
|
|
|
|
let(:file_path) { 'test_file.lfs' }
|
|
|
|
|
|
|
|
subject { described_class.new(project, branch_name) }
|
|
|
|
|
|
|
|
describe '#new_file' do
|
|
|
|
context 'with lfs disabled' do
|
|
|
|
it 'skips gitattributes check' do
|
|
|
|
expect(repository.raw).not_to receive(:blob_at)
|
|
|
|
|
|
|
|
subject.new_file(file_path, file_content)
|
|
|
|
end
|
2018-03-12 20:55:49 -04:00
|
|
|
|
|
|
|
it 'returns untransformed content' do
|
|
|
|
result = subject.new_file(file_path, file_content)
|
|
|
|
|
|
|
|
expect(result.content).to eq(file_content)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns untransformed encoding' do
|
|
|
|
result = subject.new_file(file_path, file_content, encoding: 'base64')
|
|
|
|
|
|
|
|
expect(result.encoding).to eq('base64')
|
|
|
|
end
|
2018-01-11 18:12:34 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'with lfs enabled' do
|
|
|
|
before do
|
|
|
|
allow(project).to receive(:lfs_enabled?).and_return(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'reuses cached gitattributes' do
|
|
|
|
second_file = 'another_file.lfs'
|
|
|
|
|
|
|
|
expect(repository.raw).to receive(:blob_at).with(branch_name, '.gitattributes').once
|
|
|
|
|
|
|
|
subject.new_file(file_path, file_content)
|
|
|
|
subject.new_file(second_file, file_content)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "creates an LfsObject with the file's content" do
|
|
|
|
subject.new_file(file_path, file_content)
|
|
|
|
|
|
|
|
expect(LfsObject.last.file.read).to eq file_content
|
|
|
|
end
|
|
|
|
|
2018-03-12 20:55:49 -04:00
|
|
|
it 'returns an LFS pointer' do
|
|
|
|
result = subject.new_file(file_path, file_content)
|
|
|
|
|
2018-03-16 09:35:03 -04:00
|
|
|
expect(result.content).to start_with(Gitlab::Git::LfsPointerFile::VERSION_LINE)
|
2018-03-12 20:55:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns LFS pointer encoding as text' do
|
|
|
|
result = subject.new_file(file_path, file_content, encoding: 'base64')
|
2018-01-11 18:12:34 -05:00
|
|
|
|
2018-03-12 20:55:49 -04:00
|
|
|
expect(result.encoding).to eq('text')
|
2018-01-11 18:12:34 -05:00
|
|
|
end
|
|
|
|
|
2019-05-06 08:51:49 -04:00
|
|
|
context 'when an actual file is passed' do
|
|
|
|
let(:file) { Tempfile.new(file_path) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
file.write(file_content)
|
|
|
|
file.rewind
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
file.unlink
|
|
|
|
end
|
|
|
|
|
|
|
|
it "creates an LfsObject with the file's content" do
|
|
|
|
subject.new_file(file_path, file)
|
|
|
|
|
|
|
|
expect(LfsObject.last.file.read).to eq file_content
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-11 18:12:34 -05:00
|
|
|
context "when doesn't use LFS" do
|
|
|
|
let(:file_path) { 'other.filetype' }
|
|
|
|
|
|
|
|
it "doesn't create LFS pointers" do
|
2018-03-12 20:55:49 -04:00
|
|
|
new_content = subject.new_file(file_path, file_content).content
|
2018-01-11 18:12:34 -05:00
|
|
|
|
2018-03-16 09:35:03 -04:00
|
|
|
expect(new_content).not_to start_with(Gitlab::Git::LfsPointerFile::VERSION_LINE)
|
2018-01-11 18:12:34 -05:00
|
|
|
expect(new_content).to eq(file_content)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-14 19:45:57 -04:00
|
|
|
it 'links LfsObjects to project' do
|
|
|
|
expect do
|
|
|
|
subject.new_file(file_path, file_content)
|
|
|
|
end.to change { project.lfs_objects.count }.by(1)
|
2018-01-11 18:12:34 -05:00
|
|
|
end
|
|
|
|
|
2018-03-14 19:45:57 -04:00
|
|
|
context 'when LfsObject already exists' do
|
|
|
|
let(:lfs_pointer) { Gitlab::Git::LfsPointerFile.new(file_content) }
|
2018-01-11 18:12:34 -05:00
|
|
|
|
2018-03-14 19:45:57 -04:00
|
|
|
before do
|
|
|
|
create(:lfs_object, oid: lfs_pointer.sha256, size: lfs_pointer.size)
|
2018-01-11 18:12:34 -05:00
|
|
|
end
|
|
|
|
|
2018-03-14 19:45:57 -04:00
|
|
|
it 'links LfsObjects to project' do
|
2018-01-11 18:12:34 -05:00
|
|
|
expect do
|
2018-03-14 19:45:57 -04:00
|
|
|
subject.new_file(file_path, file_content)
|
|
|
|
end.to change { project.lfs_objects.count }.by(1)
|
2018-01-11 18:12:34 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|