2019-11-21 09:06:16 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-09-28 12:46:22 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 18:09:03 +00:00
|
|
|
RSpec.describe Gitlab::Git::Push do
|
2020-02-20 18:08:51 +00:00
|
|
|
let_it_be(:project) { create(:project, :repository) }
|
2021-04-21 06:09:28 +00:00
|
|
|
|
2018-09-28 13:13:04 +00:00
|
|
|
let(:oldrev) { project.commit('HEAD~2').id }
|
2018-09-28 12:46:22 +00:00
|
|
|
let(:newrev) { project.commit.id }
|
|
|
|
let(:ref) { 'refs/heads/some-branch' }
|
|
|
|
|
|
|
|
subject { described_class.new(project, oldrev, newrev, ref) }
|
|
|
|
|
|
|
|
describe '#branch_name' do
|
|
|
|
context 'when it is a branch push' do
|
|
|
|
let(:ref) { 'refs/heads/my-branch' }
|
|
|
|
|
|
|
|
it 'returns branch name' do
|
|
|
|
expect(subject.branch_name).to eq 'my-branch'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is a tag push' do
|
|
|
|
let(:ref) { 'refs/tags/my-branch' }
|
|
|
|
|
|
|
|
it 'returns nil' do
|
|
|
|
expect(subject.branch_name).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#branch_push?' do
|
|
|
|
context 'when pushing a branch ref' do
|
|
|
|
let(:ref) { 'refs/heads/my-branch' }
|
|
|
|
|
|
|
|
it { is_expected.to be_branch_push }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is a tag push' do
|
2018-09-28 13:13:04 +00:00
|
|
|
let(:ref) { 'refs/tags/my-tag' }
|
2018-09-28 12:46:22 +00:00
|
|
|
|
|
|
|
it { is_expected.not_to be_branch_push }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-09-28 13:13:04 +00:00
|
|
|
describe '#branch_updated?' do
|
|
|
|
context 'when it is a branch push with correct old and new revisions' do
|
|
|
|
it { is_expected.to be_branch_updated }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is not a branch push' do
|
|
|
|
let(:ref) { 'refs/tags/my-tag' }
|
|
|
|
|
|
|
|
it { is_expected.not_to be_branch_updated }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when old revision is blank' do
|
|
|
|
let(:oldrev) { Gitlab::Git::BLANK_SHA }
|
|
|
|
|
|
|
|
it { is_expected.not_to be_branch_updated }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is not a branch push' do
|
|
|
|
let(:newrev) { Gitlab::Git::BLANK_SHA }
|
|
|
|
|
|
|
|
it { is_expected.not_to be_branch_updated }
|
|
|
|
end
|
2018-10-02 11:01:55 +00:00
|
|
|
|
|
|
|
context 'when oldrev is nil' do
|
|
|
|
let(:oldrev) { nil }
|
|
|
|
|
|
|
|
it { is_expected.not_to be_branch_updated }
|
|
|
|
end
|
2018-09-28 13:13:04 +00:00
|
|
|
end
|
|
|
|
|
2018-09-28 12:46:22 +00:00
|
|
|
describe '#force_push?' do
|
|
|
|
context 'when old revision is an ancestor of the new revision' do
|
|
|
|
let(:oldrev) { 'HEAD~3' }
|
|
|
|
let(:newrev) { 'HEAD~1' }
|
|
|
|
|
|
|
|
it { is_expected.not_to be_force_push }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when old revision is not an ancestor of the new revision' do
|
|
|
|
let(:oldrev) { 'HEAD~3' }
|
|
|
|
let(:newrev) { '123456' }
|
|
|
|
|
|
|
|
it { is_expected.to be_force_push }
|
|
|
|
end
|
2021-02-12 09:08:48 +00:00
|
|
|
|
2021-03-04 09:10:52 +00:00
|
|
|
context 'when called mulitiple times' do
|
2021-02-12 09:08:48 +00:00
|
|
|
it 'does not make make multiple calls to the force push check' do
|
|
|
|
expect(Gitlab::Checks::ForcePush).to receive(:force_push?).once
|
|
|
|
|
|
|
|
2.times do
|
|
|
|
subject.force_push?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-09-28 12:46:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#branch_added?' do
|
|
|
|
context 'when old revision is defined' do
|
|
|
|
it { is_expected.not_to be_branch_added }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when old revision is not defined' do
|
|
|
|
let(:oldrev) { Gitlab::Git::BLANK_SHA }
|
|
|
|
|
|
|
|
it { is_expected.to be_branch_added }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#branch_removed?' do
|
|
|
|
context 'when new revision is defined' do
|
|
|
|
it { is_expected.not_to be_branch_removed }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when new revision is not defined' do
|
|
|
|
let(:newrev) { Gitlab::Git::BLANK_SHA }
|
|
|
|
|
|
|
|
it { is_expected.to be_branch_removed }
|
|
|
|
end
|
|
|
|
end
|
2018-09-28 13:13:04 +00:00
|
|
|
|
|
|
|
describe '#modified_paths' do
|
|
|
|
context 'when a push is a branch update' do
|
2018-09-28 13:38:36 +00:00
|
|
|
let(:newrev) { '498214d' }
|
|
|
|
let(:oldrev) { '281d3a7' }
|
2018-09-28 13:13:04 +00:00
|
|
|
|
|
|
|
it 'returns modified paths' do
|
|
|
|
expect(subject.modified_paths).to eq ['bar/branch-test.txt',
|
|
|
|
'files/js/commit.coffee',
|
|
|
|
'with space/README.md']
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a push is not a branch update' do
|
|
|
|
let(:oldrev) { Gitlab::Git::BLANK_SHA }
|
|
|
|
|
|
|
|
it 'raises an error' do
|
|
|
|
expect { subject.modified_paths }.to raise_error(ArgumentError)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-10-02 11:01:55 +00:00
|
|
|
|
|
|
|
describe '#oldrev' do
|
|
|
|
context 'when a valid oldrev is provided' do
|
|
|
|
it 'returns oldrev' do
|
|
|
|
expect(subject.oldrev).to eq oldrev
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a nil valud is provided' do
|
|
|
|
let(:oldrev) { nil }
|
|
|
|
|
|
|
|
it 'returns blank SHA' do
|
|
|
|
expect(subject.oldrev).to eq Gitlab::Git::BLANK_SHA
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#newrev' do
|
|
|
|
context 'when valid newrev is provided' do
|
|
|
|
it 'returns newrev' do
|
|
|
|
expect(subject.newrev).to eq newrev
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a nil valud is provided' do
|
|
|
|
let(:newrev) { nil }
|
|
|
|
|
|
|
|
it 'returns blank SHA' do
|
|
|
|
expect(subject.newrev).to eq Gitlab::Git::BLANK_SHA
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-09-28 12:46:22 +00:00
|
|
|
end
|