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