debian-mirror-gitlab/spec/lib/gitlab/git/branch_spec.rb

122 lines
3.7 KiB
Ruby
Raw Normal View History

2020-01-01 13:55:28 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
require "spec_helper"
2022-08-27 11:52:29 +05:30
RSpec.describe Gitlab::Git::Branch do
let(:project) { create(:project, :repository) }
let(:repository) { project.repository.raw }
2017-08-17 22:00:37 +05:30
subject { repository.branches }
it { is_expected.to be_kind_of Array }
2018-03-17 18:26:18 +05:30
describe '.find' do
subject { described_class.find(repository, branch) }
before do
allow(repository).to receive(:find_branch).with(branch)
.and_call_original
end
context 'when finding branch via branch name' do
let(:branch) { 'master' }
it 'returns a branch object' do
expect(subject).to be_a(described_class)
expect(subject.name).to eq(branch)
expect(repository).to have_received(:find_branch).with(branch)
end
end
context 'when the branch is already a branch' do
let(:commit) { repository.commit('master') }
let(:branch) { described_class.new(repository, 'master', commit.sha, commit) }
it 'returns a branch object' do
expect(subject).to be_a(described_class)
expect(subject).to eq(branch)
expect(repository).not_to have_received(:find_branch).with(branch)
end
end
end
2021-06-08 01:23:25 +05:30
describe "#cache_key" do
subject { repository.branches.first }
it "returns a cache key that changes based on changeable values" do
digest = Digest::SHA1.hexdigest([subject.name, subject.target, subject.dereferenced_target.sha].join(":"))
expect(subject.cache_key).to eq("branch:#{digest}")
end
end
2017-08-17 22:00:37 +05:30
describe '#size' do
subject { super().size }
2019-12-21 20:55:43 +05:30
2022-08-27 11:52:29 +05:30
it { is_expected.to eq(TestEnv::BRANCH_SHA.size) }
2017-08-17 22:00:37 +05:30
end
describe 'first branch' do
let(:branch) { repository.branches.first }
2022-08-27 11:52:29 +05:30
it { expect(branch.name).to eq(TestEnv::BRANCH_SHA.keys.min) }
it { expect(branch.dereferenced_target.sha).to start_with(TestEnv::BRANCH_SHA[TestEnv::BRANCH_SHA.keys.min]) }
2017-08-17 22:00:37 +05:30
end
describe 'master branch' do
let(:branch) do
repository.branches.find { |branch| branch.name == 'master' }
end
2022-08-27 11:52:29 +05:30
it { expect(branch.dereferenced_target.sha).to start_with(TestEnv::BRANCH_SHA['master']) }
2017-08-17 22:00:37 +05:30
end
2018-03-27 19:54:05 +05:30
context 'with active, stale and future branches' do
let(:user) { create(:user) }
2021-01-03 14:25:43 +05:30
let(:stale_sha) { travel_to(Gitlab::Git::Branch::STALE_BRANCH_THRESHOLD.ago - 5.days) { create_commit } }
let(:active_sha) { travel_to(Gitlab::Git::Branch::STALE_BRANCH_THRESHOLD.ago + 5.days) { create_commit } }
let(:future_sha) { travel_to(100.days.since) { create_commit } }
2018-03-27 19:54:05 +05:30
before do
repository.create_branch('stale-1', stale_sha)
repository.create_branch('active-1', active_sha)
repository.create_branch('future-1', future_sha)
end
describe 'examine if the branch is active or stale' do
let(:stale_branch) { repository.find_branch('stale-1') }
let(:active_branch) { repository.find_branch('active-1') }
let(:future_branch) { repository.find_branch('future-1') }
describe '#active?' do
it { expect(stale_branch.active?).to be_falsey }
it { expect(active_branch.active?).to be_truthy }
it { expect(future_branch.active?).to be_truthy }
end
describe '#stale?' do
it { expect(stale_branch.stale?).to be_truthy }
it { expect(active_branch.stale?).to be_falsey }
it { expect(future_branch.stale?).to be_falsey }
end
describe '#state' do
it { expect(stale_branch.state).to eq(:stale) }
it { expect(active_branch.state).to eq(:active) }
it { expect(future_branch.state).to eq(:active) }
end
end
end
def create_commit
2022-10-11 01:57:18 +05:30
repository.commit_files(
2022-08-13 15:12:31 +05:30
user,
branch_name: 'HEAD',
message: 'commit message',
actions: []
).newrev
2018-03-27 19:54:05 +05:30
end
2017-08-17 22:00:37 +05:30
end