require 'spec_helper' describe EmberCliDeployRedis::Revision do let(:application) { double('application', name: 'app_name') } describe '.active_from_redis' do let(:active_revision_name) { 'active_revision' } let(:do_the_thing) { described_class.active_from_redis(application) } before do allow(described_class).to receive(:active_revision_from_redis) .and_return(active_revision_name) end it 'gets the active_revision_from_redis and calls Revision.new with it' do expect(described_class).to receive(:new).with(application, active_revision_name) expect(described_class).to receive(:active_revision_from_redis).with(application) do_the_thing end end describe '#activate!' do subject { described_class.new(application, 'new_revision') } let(:do_the_thing) { subject.activate! } let(:application) { EmberCliDeployRedis::Application.new('application_name') } it 'makes the revision active' do do_the_thing expect(application.active_revision).to eq(subject) end it 'leaves the other revision in existence' do described_class.new(application, 'old_revision').activate! do_the_thing old_revision = application.revision_by_name('old_revision') expect(old_revision).to be_kind_of(EmberCliDeployRedis::Revision) expect(old_revision).to_not be_active end end describe '.active_revision_from_redis' do subject { described_class.active_revision_from_redis(application) } let(:redis_key) { 'revision_list' } let(:most_recent_revision) { 'recent' } let(:oldest_revision) { 'oldest' } before do allow(EmberCliDeployRedis.configuration).to receive(:redis_key_for_revision_list) .and_return(redis_key) end context 'with deployed revisions' do before do EmberCliDeployRedis.configuration.redis.lpush(redis_key, oldest_revision) EmberCliDeployRedis.configuration.redis.lpush(redis_key, most_recent_revision) end it 'gets the redis key using the application' do expect(EmberCliDeployRedis.configuration).to receive(:redis_key_for_revision_list) .with(application) subject end it 'gets the most recent revision from redis' do expect(subject).to eq(most_recent_revision) end end context 'with no deployed revisions' do it 'returns nil' do expect(subject).to be_nil end end end let(:revision) { described_class.new(application, revision_name) } let(:revision_name) { 'revision_name' } describe '#initialize' do subject { revision } it 'stores the application' do expect(subject.application).to eq(application) end it 'stores the name' do expect(subject.name).to eq(revision_name) end end describe '#contents_of' do subject { revision.contents_of(filename) } let(:filename) { 'file' } let(:file_contents) { 'contents' } it 'gets the contents_of_file_from_redis and returns it' do expect(revision).to receive(:contents_of_file_from_redis) .with(filename) .and_return(file_contents) expect(subject).to eq(file_contents) end end describe '#contents_of_file_from_redis' do subject { revision.send(:contents_of_file_from_redis, filename) } let(:redis_key) { 'revision_name/file' } let(:filename) { 'file' } let(:file_contents) { 'contents' } before do allow(EmberCliDeployRedis.configuration).to receive(:redis_key_for_revision_file) .and_return(redis_key) end context 'when the deployed file exists' do before do EmberCliDeployRedis.configuration.redis.set(redis_key, file_contents) end it 'gets the redis key from configuration' do expect(EmberCliDeployRedis.configuration).to receive(:redis_key_for_revision_file) .with(application, revision_name, filename) subject end it 'gets the file contents from redis' do expect(subject).to eq(file_contents) end end context 'when the deployed file does not exist' do it 'raises an error' do expect { subject }.to raise_error end end end end