require 'spec_helper' require 'fileutils' describe Hillary::Slug do let(:version) do double( Hillary::Repo::Version, sluggable?: true, branch: 'dev', name: 'b26206aaf36207304d4fa583ce9837d0bdb48966', sha: 'b26206aaf36207304d4fa583ce9837d0bdb48966', dev?: true, master?: false, production?: false ) end let(:out){StringIO.new} let(:logger) do Logger.new(out).tap do |logger| end end let(:bucket){double(Hillary::Slug::Bucket, write: nil, copy: nil, name: 'gz-slugbucket')} let(:path){File.expand_path('../../tmp/g3', __FILE__)} describe '.build' do context 'when the version is not sluggable' do before(:each){allow(version).to receive(:sluggable?){false}} it 'returns nil' do slug = described_class.build(path, version, logger: logger, bucket: bucket) expect(slug).to be_nil end end context 'when the version is sluggable' do let(:slug){double(Hillary::Slug)} it 'returns a slug' do expect(Hillary::Slug).to receive(:new).with(path, version, logger: logger, bucket: bucket){slug} expect(slug).to receive(:build) described_class.build(path, version, logger: logger, bucket: bucket) end end end describe '#build' do context 'when branch is dev' do subject(:slug){described_class.new(path, version, logger: logger, bucket: bucket)} let(:slug_name){'g3_slug_b26206aaf36207304d4fa583ce9837d0bdb48966.tar.gz'} let(:slug_path){Pathname.new(path).join('..', slug_name).expand_path} describe 'when everything is successful' do before(:each) do FileUtils.mkdir_p(path) allow(bucket).to receive(:write) end after(:each) do FileUtils.rm_rf(slug_path) FileUtils.rm_rf(path) end it 'creates the slug' do slug.build expect(File.exist?(slug_path)).to eq(true) end it 'writes the slug to the bucket' do expect(bucket).to receive(:write).with('dev/' + slug_name, slug_path) slug.build end it 'logs information about what is happening' do slug.build expect(out.string).to eq( "Creating slug g3_slug_b26206aaf36207304d4fa583ce9837d0bdb48966.tar.gz\n"\ 'Uploading g3_slug_b26206aaf36207304d4fa583ce9837d0bdb48966.tar.gz '\ "to gz-slugbucket:/dev/g3_slug_b26206aaf36207304d4fa583ce9837d0bdb48966.tar.gz\n" ) end end describe 'when archiving fails' do it 'raises an error' do expect{slug.build}.to raise_error(Hillary::Shellable::ExecutionError) end end end context 'when branch is master, but not production build' do let(:version) do double( Hillary::Repo::Version, sluggable?: true, branch: 'master', name: 'RC2014_01_01_01_01_01', sha: 'b26206aaf36207304d4fa583ce9837d0bdb48966', dev?: false, master?: true, production?: false ) end subject(:slug){described_class.new(path, version, logger: logger, bucket: bucket)} it 'copies the canonical slug to rc slug name' do expect(bucket).to receive(:copy).with('dev/g3_slug_b26206aaf36207304d4fa583ce9837d0bdb48966.tar.gz', 'rc/g3_slug_RC2014_01_01_01_01_01.tar.gz') slug.build end it 'logs information about what is happening' do slug.build expect(out.string).to eq('Copying gz-slugbucket:/dev/g3_slug_b26206aaf36207304d4fa583ce9837d0bdb48966.tar.gz '\ "to gz-slugbucket:/rc/g3_slug_RC2014_01_01_01_01_01.tar.gz\n" ) end end context 'when branch is master and is a production build' do let(:version) do double( Hillary::Repo::Version, sluggable?: true, branch: 'master', name: '2014_01_01_01_01_01', sha: 'b26206aaf36207304d4fa583ce9837d0bdb48966', dev?: false, master?: true, production?: true ) end subject(:slug){described_class.new(path, version, logger: logger, bucket: bucket)} it 'copies the canonical slug to production slug name' do expect(bucket).to receive(:copy).with( 'dev/g3_slug_b26206aaf36207304d4fa583ce9837d0bdb48966.tar.gz', 'production/g3_slug_2014_01_01_01_01_01.tar.gz' ) slug.build end it 'logs information about what is happening' do slug.build expect(out.string).to eq( 'Copying gz-slugbucket:/dev/g3_slug_b26206aaf36207304d4fa583ce9837d0bdb48966.tar.gz '\ "to gz-slugbucket:/production/g3_slug_2014_01_01_01_01_01.tar.gz\n" ) end end end end