require 'spec_helper' require 'thegarage/gitx/cli/buildtag_command' describe Thegarage::Gitx::Cli::BuildtagCommand do let(:args) { [] } let(:options) { {} } let(:config) do { pretend: true } end let(:cli) { Thegarage::Gitx::Cli::BuildtagCommand.new(args, options, config) } let(:branch) { double('fake branch', name: 'feature-branch') } before do allow(cli).to receive(:current_branch).and_return(branch) end describe '#buildtag' do let(:env_travis_branch) { nil } let(:env_travis_build_number) { nil } let(:env_ci_branch) { nil } let(:env_ci_build_number) { nil } before do ENV['TRAVIS_BRANCH'] = env_travis_branch ENV['TRAVIS_BUILD_NUMBER'] = env_travis_build_number ENV['CI_BRANCH'] = env_ci_branch ENV['CI_BUILD_NUMBER'] = env_ci_build_number end context 'when TRAVIS_BRANCH is nil' do it 'raises Unknown Branch error' do expect { cli.buildtag }.to raise_error(/Unknown branch/) end end context 'when TRAVIS_BRANCH is NOT master or staging' do let(:env_travis_branch) { 'random-branch' } it 'raises unsupported branch error' do expect { cli.buildtag }.to raise_error(/Branch must be one of the supported taggable branches/) end end context 'when TRAVIS_BRANCH is master' do let(:env_travis_branch) { 'master' } let(:env_travis_build_number) { '24' } before do Timecop.freeze(Time.utc(2013, 10, 30, 10, 21, 28)) do expect(cli).to receive(:run_cmd).with("git tag build-master-2013-10-30-10-21-28 -a -m 'buildtag generated by build 24'").ordered expect(cli).to receive(:run_cmd).with("git push origin build-master-2013-10-30-10-21-28").ordered cli.buildtag end end it 'creates a tag for the branch and push it to github' do should meet_expectations end end context 'when CI_BRANCH is master' do let(:env_ci_branch) { 'master' } let(:env_ci_build_number) { '24' } before do Timecop.freeze(Time.utc(2013, 10, 30, 10, 21, 28)) do expect(cli).to receive(:run_cmd).with("git tag build-master-2013-10-30-10-21-28 -a -m 'buildtag generated by build 24'").ordered expect(cli).to receive(:run_cmd).with("git push origin build-master-2013-10-30-10-21-28").ordered cli.buildtag end end it 'creates a tag for the branch and push it to github' do should meet_expectations end end end end