# encoding: utf-8 require 'spec_helper' describe GitStorage do let(:git_repo) { File.join(working_directory, 'git_repo.git') } let(:compressor) do Class.new do def prepare(file) end end end let(:creator) do Class.new do attr_reader :content, :path, :name attr_accessor :compressed_content def initialize(path, content) @path = path @content = content @name = path.sub(/\..*$/, '').camelize.downcase.to_sym end def extension?(ext) ::File.extname(path) == ext end end end context '#[]' do it 'finds by name' do repo = GitRepository.create(git_repo) repo.add_content('file1.txt', 'asdf') repo.add_content('file2.pac', 'data file2.pac') storage = GitStorage.new(repo.storage_path, compressor) expect(storage[:file2].content).to eq('data file2.pac') end it 'supports directories as well' do repo = GitRepository.create(git_repo) repo.add_content('file1.txt', 'asdf') repo.add_content('dir/file2.pac', 'data file2.pac') storage = GitStorage.new(repo.storage_path, compressor) expect(storage['dir::file2'.to_sym].content).to eq('data file2.pac') end it 'supports deeply nested directories as well' do repo = GitRepository.create(git_repo) repo.add_content('file1.txt', 'asdf') repo.add_content('dir/dir1/file3.pac', 'data asdf.pac') storage = GitStorage.new(repo.storage_path, compressor) expect(storage['dir::dir1::file3'.to_sym].content).to eq('data asdf.pac') end end context '#each_pac_file' do it 'iterates over all found pac files' do arbitrary_file = double('arbitrary file') expect(arbitrary_file).to receive(:extension?).with('.pac').and_return(false) pac_file = double('pac_file') allow(pac_file).to receive(:extension?).with('.pac').and_return(true) allow(pac_file).to receive(:path).and_return('proxy.pac') allow(pac_file).to receive(:name).and_return(:proxy) vcs = double('vcs') allow(vcs).to receive(:all_files).and_return([pac_file, arbitrary_file]) vcs_engine = double('vcs_engine') allow(vcs_engine).to receive(:new).and_return(vcs) storage = GitStorage.new(git_repo, compressor, vcs_engine) result = [] storage.each_pac_file { |file| result << file} expect(result).to eq([pac_file]) end end end