diff options
Diffstat (limited to 'spec/models/commit_spec.rb')
-rw-r--r-- | spec/models/commit_spec.rb | 152 |
1 files changed, 127 insertions, 25 deletions
diff --git a/spec/models/commit_spec.rb b/spec/models/commit_spec.rb index 36d0e37454d..edb856d34df 100644 --- a/spec/models/commit_spec.rb +++ b/spec/models/commit_spec.rb @@ -92,9 +92,11 @@ RSpec.describe Commit do end it 'parses date strings into Time instances' do - commit = described_class.build_from_sidekiq_hash(project, - id: '123', - authored_date: Time.current.to_s) + commit = described_class.build_from_sidekiq_hash( + project, + id: '123', + authored_date: Time.current.to_s + ) expect(commit.authored_date).to be_a_kind_of(Time) end @@ -551,18 +553,22 @@ eos let(:repository) { project.repository } let(:merge_request) do - create(:merge_request, - source_branch: 'video', - target_branch: 'master', - source_project: project, - author: user) + create( + :merge_request, + source_branch: 'video', + target_branch: 'master', + source_project: project, + author: user + ) end let(:merge_commit) do - merge_commit_id = repository.merge(user, - merge_request.diff_head_sha, - merge_request, - 'Test message') + merge_commit_id = repository.merge( + user, + merge_request.diff_head_sha, + merge_request, + 'Test message' + ) repository.commit(merge_commit_id) end @@ -640,17 +646,21 @@ eos let(:user2) { build(:user) } let!(:note1) do - create(:note_on_commit, - commit_id: commit.id, - project: project, - note: 'foo') + create( + :note_on_commit, + commit_id: commit.id, + project: project, + note: 'foo' + ) end let!(:note2) do - create(:note_on_commit, - commit_id: commit.id, - project: project, - note: 'bar') + create( + :note_on_commit, + commit_id: commit.id, + project: project, + note: 'bar' + ) end before do @@ -854,11 +864,13 @@ eos let(:issue) { create(:issue, author: user, project: project) } it 'returns true if the commit has been reverted' do - create(:note_on_issue, - noteable: issue, - system: true, - note: commit.revert_description(user), - project: issue.project) + create( + :note_on_issue, + noteable: issue, + system: true, + note: commit.revert_description(user), + project: issue.project + ) expect_next_instance_of(Commit) do |revert_commit| expect(revert_commit).to receive(:reverts_commit?) @@ -873,4 +885,94 @@ eos expect(commit.has_been_reverted?(user, issue.notes_with_associations)).to eq(false) end end + + describe '#tipping_refs' do + let_it_be(:tag_name) { 'v1.1.0' } + let_it_be(:branch_names) { %w[master not-merged-branch v1.1.0] } + + shared_examples 'tipping ref names' do + context 'when called without limits' do + it 'return tipping refs names' do + expect(called_method.call).to eq(expected) + end + end + + context 'when called with limits' do + it 'return tipping refs names' do + limit = 1 + expect(called_method.call(limit).size).to be <= limit + end + end + + describe '#tipping_branches' do + let(:called_method) { ->(limit = 0) { commit.tipping_branches(limit: limit) } } + let(:expected) { branch_names } + + it_behaves_like 'with tipping ref names' + end + + describe '#tipping_tags' do + let(:called_method) { ->(limit = 0) { commit.tipping_tags(limit: limit) } } + let(:expected) { [tag_name] } + + it_behaves_like 'with tipping ref names' + end + end + end + + context 'containing refs' do + shared_examples 'containing ref names' do + context 'without arguments' do + it 'returns branch names containing the commit' do + expect(ref_containing.call).to eq(containing_refs) + end + end + + context 'with limit argument' do + it 'returns the appropriate amount branch names' do + limit = 2 + expect(ref_containing.call(limit: limit).size).to be <= limit + end + end + + context 'with tipping refs excluded' do + let(:excluded_refs) do + project.repository.refs_by_oid(oid: commit_sha, ref_patterns: [ref_prefix]).map { |n| n.delete_prefix(ref_prefix) } + end + + it 'returns branch names containing the commit without the one with the commit at tip' do + expect(ref_containing.call(excluded_tipped: true)).to eq(containing_refs - excluded_refs) + end + + it 'returns the appropriate amount branch names with limit argument' do + limit = 2 + expect(ref_containing.call(limit: limit, excluded_tipped: true).size).to be <= limit + end + end + end + + describe '#branches_containing' do + let_it_be(:commit_sha) { project.commit.sha } + let_it_be(:containing_refs) { project.repository.branch_names_contains(commit_sha) } + + let(:ref_prefix) { Gitlab::Git::BRANCH_REF_PREFIX } + + let(:ref_containing) { ->(limit: 0, excluded_tipped: false) { commit.branches_containing(exclude_tipped: excluded_tipped, limit: limit) } } + + it_behaves_like 'containing ref names' + end + + describe '#tags_containing' do + let_it_be(:tag_name) { 'v1.1.0' } + let_it_be(:commit_sha) { project.repository.find_tag(tag_name).target_commit.sha } + let_it_be(:containing_refs) { %w[v1.1.0 v1.1.1] } + + let(:ref_prefix) { Gitlab::Git::TAG_REF_PREFIX } + + let(:commit) { project.repository.commit(commit_sha) } + let(:ref_containing) { ->(limit: 0, excluded_tipped: false) { commit.tags_containing(exclude_tipped: excluded_tipped, limit: limit) } } + + it_behaves_like 'containing ref names' + end + end end |