code.gitea.io/gitea@v1.19.3/modules/git/repo_commit_test.go (about) 1 // Copyright 2018 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package git 5 6 import ( 7 "path/filepath" 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 ) 12 13 func TestRepository_GetCommitBranches(t *testing.T) { 14 bareRepo1Path := filepath.Join(testReposDir, "repo1_bare") 15 bareRepo1, err := openRepositoryWithDefaultContext(bareRepo1Path) 16 assert.NoError(t, err) 17 defer bareRepo1.Close() 18 19 // these test case are specific to the repo1_bare test repo 20 testCases := []struct { 21 CommitID string 22 ExpectedBranches []string 23 }{ 24 {"2839944139e0de9737a044f78b0e4b40d989a9e3", []string{"branch1"}}, 25 {"5c80b0245c1c6f8343fa418ec374b13b5d4ee658", []string{"branch2"}}, 26 {"37991dec2c8e592043f47155ce4808d4580f9123", []string{"master"}}, 27 {"95bb4d39648ee7e325106df01a621c530863a653", []string{"branch1", "branch2"}}, 28 {"8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2", []string{"branch2", "master"}}, 29 {"master", []string{"master"}}, 30 } 31 for _, testCase := range testCases { 32 commit, err := bareRepo1.GetCommit(testCase.CommitID) 33 assert.NoError(t, err) 34 branches, err := bareRepo1.getBranches(commit, 2) 35 assert.NoError(t, err) 36 assert.Equal(t, testCase.ExpectedBranches, branches) 37 } 38 } 39 40 func TestGetTagCommitWithSignature(t *testing.T) { 41 bareRepo1Path := filepath.Join(testReposDir, "repo1_bare") 42 bareRepo1, err := openRepositoryWithDefaultContext(bareRepo1Path) 43 assert.NoError(t, err) 44 defer bareRepo1.Close() 45 46 // both the tag and the commit are signed here, this validates only the commit signature 47 commit, err := bareRepo1.GetCommit("28b55526e7100924d864dd89e35c1ea62e7a5a32") 48 assert.NoError(t, err) 49 assert.NotNil(t, commit) 50 assert.NotNil(t, commit.Signature) 51 // test that signature is not in message 52 assert.Equal(t, "signed-commit\n", commit.CommitMessage) 53 } 54 55 func TestGetCommitWithBadCommitID(t *testing.T) { 56 bareRepo1Path := filepath.Join(testReposDir, "repo1_bare") 57 bareRepo1, err := openRepositoryWithDefaultContext(bareRepo1Path) 58 assert.NoError(t, err) 59 defer bareRepo1.Close() 60 61 commit, err := bareRepo1.GetCommit("bad_branch") 62 assert.Nil(t, commit) 63 assert.Error(t, err) 64 assert.True(t, IsErrNotExist(err)) 65 } 66 67 func TestIsCommitInBranch(t *testing.T) { 68 bareRepo1Path := filepath.Join(testReposDir, "repo1_bare") 69 bareRepo1, err := openRepositoryWithDefaultContext(bareRepo1Path) 70 assert.NoError(t, err) 71 defer bareRepo1.Close() 72 73 result, err := bareRepo1.IsCommitInBranch("2839944139e0de9737a044f78b0e4b40d989a9e3", "branch1") 74 assert.NoError(t, err) 75 assert.True(t, result) 76 77 result, err = bareRepo1.IsCommitInBranch("2839944139e0de9737a044f78b0e4b40d989a9e3", "branch2") 78 assert.NoError(t, err) 79 assert.False(t, result) 80 } 81 82 func TestRepository_CommitsBetweenIDs(t *testing.T) { 83 bareRepo1Path := filepath.Join(testReposDir, "repo4_commitsbetween") 84 bareRepo1, err := openRepositoryWithDefaultContext(bareRepo1Path) 85 assert.NoError(t, err) 86 defer bareRepo1.Close() 87 88 cases := []struct { 89 OldID string 90 NewID string 91 ExpectedCommits int 92 }{ 93 {"fdc1b615bdcff0f0658b216df0c9209e5ecb7c78", "78a445db1eac62fe15e624e1137965969addf344", 1}, // com1 -> com2 94 {"78a445db1eac62fe15e624e1137965969addf344", "fdc1b615bdcff0f0658b216df0c9209e5ecb7c78", 0}, // reset HEAD~, com2 -> com1 95 {"78a445db1eac62fe15e624e1137965969addf344", "a78e5638b66ccfe7e1b4689d3d5684e42c97d7ca", 1}, // com2 -> com2_new 96 } 97 for i, c := range cases { 98 commits, err := bareRepo1.CommitsBetweenIDs(c.NewID, c.OldID) 99 assert.NoError(t, err) 100 assert.Equal(t, c.ExpectedCommits, len(commits), "case %d", i) 101 } 102 }