Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'spec/lib/gitlab/git/commit_spec.rb')
-rw-r--r--spec/lib/gitlab/git/commit_spec.rb108
1 files changed, 106 insertions, 2 deletions
diff --git a/spec/lib/gitlab/git/commit_spec.rb b/spec/lib/gitlab/git/commit_spec.rb
index dd9f77f0211..5c4be1003c3 100644
--- a/spec/lib/gitlab/git/commit_spec.rb
+++ b/spec/lib/gitlab/git/commit_spec.rb
@@ -3,15 +3,16 @@
require "spec_helper"
RSpec.describe Gitlab::Git::Commit, feature_category: :source_code_management do
- let(:repository) { create(:project, :repository).repository.raw }
+ let_it_be(:repository) { create(:project, :repository).repository.raw }
let(:commit) { described_class.find(repository, SeedRepo::Commit::ID) }
describe "Commit info from gitaly commit" do
let(:subject) { (+"My commit").force_encoding('ASCII-8BIT') }
let(:body) { subject + (+"My body").force_encoding('ASCII-8BIT') }
let(:body_size) { body.length }
- let(:gitaly_commit) { build(:gitaly_commit, subject: subject, body: body, body_size: body_size) }
+ let(:gitaly_commit) { build(:gitaly_commit, subject: subject, body: body, body_size: body_size, tree_id: tree_id) }
let(:id) { gitaly_commit.id }
+ let(:tree_id) { 'd7f32d821c9cc7b1a9166ca7c4ba95b5c2d0d000' }
let(:committer) { gitaly_commit.committer }
let(:author) { gitaly_commit.author }
let(:commit) { described_class.new(repository, gitaly_commit) }
@@ -26,6 +27,7 @@ RSpec.describe Gitlab::Git::Commit, feature_category: :source_code_management do
it { expect(commit.committer_name).to eq(committer.name) }
it { expect(commit.committer_email).to eq(committer.email) }
it { expect(commit.parent_ids).to eq(gitaly_commit.parent_ids) }
+ it { expect(commit.tree_id).to eq(tree_id) }
context 'non-UTC dates' do
let(:seconds) { Time.now.to_i }
@@ -577,6 +579,14 @@ RSpec.describe Gitlab::Git::Commit, feature_category: :source_code_management do
it { is_expected.to eq(sample_commit_hash[:message]) }
end
+
+ describe '#tree_id' do
+ subject { super().tree_id }
+
+ it "doesn't return tree id for non-Gitaly commits" do
+ is_expected.to be_nil
+ end
+ end
end
describe '#stats' do
@@ -681,6 +691,100 @@ RSpec.describe Gitlab::Git::Commit, feature_category: :source_code_management do
end
end
+ describe 'SHA patterns' do
+ shared_examples 'a SHA-matching pattern' do
+ let(:expected_match) { sha }
+
+ shared_examples 'a match' do
+ it 'matches the pattern' do
+ expect(value).to match(pattern)
+ expect(pattern.match(value).to_a).to eq([expected_match])
+ end
+ end
+
+ shared_examples 'no match' do
+ it 'does not match the pattern' do
+ expect(value).not_to match(pattern)
+ end
+ end
+
+ shared_examples 'a SHA pattern' do
+ context "with too short value" do
+ let(:value) { sha[0, described_class::MIN_SHA_LENGTH - 1] }
+
+ it_behaves_like 'no match'
+ end
+
+ context "with full length" do
+ let(:value) { sha }
+
+ it_behaves_like 'a match'
+ end
+
+ context "with exceeeding length" do
+ let(:value) { sha + sha }
+
+ # This case is not exactly pretty for SHA1 as we would still match the full SHA256 length. It's arguable what
+ # the correct behaviour would be, but without starting to distinguish SHA1 and SHA256 hashes this is the best
+ # we can do.
+ let(:expected_match) { (sha + sha)[0, described_class::MAX_SHA_LENGTH] }
+
+ it_behaves_like 'a match'
+ end
+
+ context "with embedded SHA" do
+ let(:value) { "xxx#{sha}xxx" }
+
+ it_behaves_like 'a match'
+ end
+ end
+
+ context 'abbreviated SHA pattern' do
+ let(:pattern) { described_class::SHA_PATTERN }
+
+ context "with minimum length" do
+ let(:value) { sha[0, described_class::MIN_SHA_LENGTH] }
+ let(:expected_match) { value }
+
+ it_behaves_like 'a match'
+ end
+
+ context "with medium length" do
+ let(:value) { sha[0, described_class::MIN_SHA_LENGTH + 20] }
+ let(:expected_match) { value }
+
+ it_behaves_like 'a match'
+ end
+
+ it_behaves_like 'a SHA pattern'
+ end
+
+ context 'full SHA pattern' do
+ let(:pattern) { described_class::FULL_SHA_PATTERN }
+
+ context 'with abbreviated length' do
+ let(:value) { sha[0, described_class::SHA1_LENGTH - 1] }
+
+ it_behaves_like 'no match'
+ end
+
+ it_behaves_like 'a SHA pattern'
+ end
+ end
+
+ context 'SHA1' do
+ let(:sha) { "5716ca5987cbf97d6bb54920bea6adde242d87e6" }
+
+ it_behaves_like 'a SHA-matching pattern'
+ end
+
+ context 'SHA256' do
+ let(:sha) { "a52e146ac2ab2d0efbb768ab8ebd1e98a6055764c81fe424fbae4522f5b4cb92" }
+
+ it_behaves_like 'a SHA-matching pattern'
+ end
+ end
+
def sample_commit_hash
{
author_email: "dmitriy.zaporozhets@gmail.com",