require 'spec_helper' describe Hillary::Repo::Version do include_context 'git repository' describe '.create!' do context 'when the repository is on master, but not a production build' do let!(:version){Hillary::Repo::Version.create!(repo)} it 'creates an rc tag' do expect(version.rc_tag).to eq(repo.last_rc_tag.name) expect(version.rc_tag).to match(Hillary::Repo::RC_TAG_REGEX) end end context 'when the repository is on master and is a production build' do before(:each){repo.create_rc_tag} let!(:version){Hillary::Repo::Version.create!(repo, production: true)} it 'creates a production tag' do expect(version.production_tag).to eq(repo.last_production_tag.name) expect(version.production_tag).to match(Hillary::Repo::PRODUCTION_TAG_REGEX) end end context 'when the repository is on dev' do before(:each){local_repo.git.checkout({b: true}, 'dev')} let!(:version){Hillary::Repo::Version.create!(repo)} it 'does not create a tag' do expect(version.rc_tag).to be_nil expect(version.production_tag).to be_nil end end end describe '#name' do let(:version){Hillary::Repo::Version.new(repo)} context 'when branch is dev' do before(:each){local_repo.git.checkout({b: true}, 'dev')} it 'returns the commit sha of the current branch' do expect(version.name).to eq(local_repo.commits.last.sha) end end context 'when branch is master, but not a production build' do before(:each){repo.create_rc_tag} it 'returns the last rc tag' do expect(version.name).to eq(repo.last_rc_tag.name) end end context 'when branch is master, but not a production build' do before(:each) do repo.create_rc_tag repo.create_production_tag end let(:version){Hillary::Repo::Version.new(repo, production: true)} it 'returns the last production tag' do expect(version.name).to eq(repo.last_production_tag.name) end end end describe '#branch' do let(:version){Hillary::Repo::Version.new(repo)} context 'when branch is dev' do before(:each){local_repo.git.checkout({b: true}, 'dev')} it 'returns "dev"' do expect(version.branch).to eq('dev') end end context 'when branch is master' do it 'returns "master"' do expect(version.branch).to eq('master') end end end describe '#tag' do let(:version){Hillary::Repo::Version.new(repo)} context 'when the branch is dev and no rc tag points to the same commit' do it 'returns nil' do expect(version.tag).to be_nil end end context 'when the branch is master, but not a production build' do before(:each){repo.create_rc_tag} it 'returns last rc tag' do expect(version.tag).to eq(repo.last_rc_tag.name) end end context 'when the branch is master and is a production build' do before(:each) do repo.create_rc_tag repo.create_production_tag end let(:version){Hillary::Repo::Version.new(repo, production: true)} it 'returns last production tag' do expect(version.tag).to eq(repo.last_production_tag.name) end end end describe '#sha' do let(:version){Hillary::Repo::Version.new(repo)} it 'returns the sha of the current branch' do expect(version.sha).to eq(local_repo.commits.last.sha) end end describe '#rc_tag' do let(:version){Hillary::Repo::Version.new(repo)} before(:each){repo.create_rc_tag} context 'when current head matches latest rc tag' do it 'returns the rc tag' do expect(version.rc_tag).to eq(repo.last_rc_tag.name) end end context 'when current head does not match the latest rc tag' do before(:each) do add_file_to_local('pizza.md', 'add pizza') end it 'returns nil' do expect(version.rc_tag).to be_nil end end end describe '#production_tag' do let(:version){Hillary::Repo::Version.new(repo)} before(:each) do repo.create_rc_tag repo.create_production_tag end context 'when lastest rc tag matches latest production tag' do it 'returns the production tag' do expect(version.production_tag).to eq(repo.last_production_tag.name) end end context 'when latest rc tag does not match the latest production tag' do before(:each) do add_file_to_local('pizza.md', 'add pizza') Timecop.freeze(Time.now + 30) do repo.create_rc_tag end end it 'returns nil' do # HERE expect(version.production_tag).to be_nil end end end describe '#sluggable?' do let(:version){Hillary::Repo::Version.new(repo)} context 'when the branch is dev' do before(:each){local_repo.git.checkout({b: true}, 'dev')} it 'is sluggable' do expect(version).to be_sluggable end end context 'when the branch is master' do it 'is sluggable' do expect(version).to be_sluggable end end context 'when the branch is anything else' do before(:each){local_repo.git.checkout({b: true}, 'anything_else')} it 'is not sluggable' do expect(version).to_not be_sluggable end end end describe '#master?' do let(:version){Hillary::Repo::Version.new(repo)} context 'when branch is master' do it 'is true' do expect(version).to be_master end end context 'when branch is not master' do before(:each){local_repo.git.checkout({b: true}, 'anything_else')} it 'is false' do expect(version).to_not be_master end end end describe '#dev?' do let(:version){Hillary::Repo::Version.new(repo)} context 'when branch is dev' do before(:each){local_repo.git.checkout({b: true}, 'dev')} it 'is true' do expect(version).to be_dev end end context 'when branch is not dev' do it 'is false' do expect(version).to_not be_dev end end end end