# encoding: utf-8 require 'spec_helper' describe GitRepository do context '#initialize' do it 'requires a storage path' do file_creator = double('file_creator') null_file = double('null_file') Rugged::Repository.init_at(File.join(working_directory, 'git_repo')) expect { GitRepository.new(File.join(working_directory, 'git_repo'), file_creator, null_file) }.not_to raise_error end end context '#find_file' do it 'returns a file if exists in repository' do repo_path = File.join(working_directory, 'git_repo') Rugged::Repository.init_at(repo_path) file = double('file') allow(file).to receive(:name).and_return(:file) file_creator = double('file_creator') allow(file_creator).to receive(:new).with('file.txt', 'asdf').and_return(file) null_file = double('null_file') repo = GitRepository.new(repo_path, file_creator, null_file) repo.add_content('file.txt', 'asdf') f = repo.find_file(:file) expect(f).to be(file) end it 'returns a null file if does not exist in repository' do repo_path = File.join(working_directory, 'git_repo') Rugged::Repository.init_at(repo_path) file_creator = double('file_creator') null_file = double('null_file') repo = GitRepository.new(repo_path, file_creator, null_file) f = repo.find_file(:file) expect(f).to be(null_file) end it 'handles uninitialized repositories' do repo_path = File.join(working_directory, 'git_repo') Rugged::Repository.init_at(repo_path) file_creator = double('file_creator') null_file = double('null_file') repo = GitRepository.new(repo_path, file_creator, null_file) f = repo.find_file(:file) expect(f).to be(null_file) end end context '#each_file' do it 'iterates over all found files' do repo_path = File.join(working_directory, 'git_repo') Rugged::Repository.init_at(repo_path) file = double('file') allow(file).to receive(:name).and_return(:file1) file_creator = double('file_creator') allow(file_creator).to receive(:new).with('file1.pac', 'asdf1').and_return(file) null_file = double('null_file') repo = GitRepository.new(repo_path, file_creator, null_file) repo.add_content('file1.pac', 'asdf1') result = [] repo.each_file { |f| result << f.name } expect(result).to eq([:file1]) end end context '.clone' do it 'clones existing repository' do repo_path = File.join(working_directory, 'git_repo.git') repo = GitRepository.create(repo_path) repo.add_content('file1.pac', 'asdf1') clone_path = File.join(working_directory, 'git_repo_clone') clone = GitRepository.clone(repo_path, clone_path) expect(clone.all_files.first.name).to eq(:file1) end it 'creates cloned bare repository' do repo_path = File.join(working_directory, 'git_repo.git') repo = GitRepository.create(repo_path) repo.add_content('file1.pac', 'asdf1') clone_path = File.join(working_directory, 'git_repo_clone') GitRepository.clone(repo_path, clone_path, bare: true) expect(Rugged::Repository.new(clone_path).bare?).to be true end end end