code.gitea.io/gitea@v1.19.3/modules/git/repo_compare_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 "bytes" 8 "io" 9 "path/filepath" 10 "testing" 11 12 "github.com/stretchr/testify/assert" 13 ) 14 15 func TestGetFormatPatch(t *testing.T) { 16 bareRepo1Path := filepath.Join(testReposDir, "repo1_bare") 17 clonedPath, err := cloneRepo(t, bareRepo1Path) 18 if err != nil { 19 assert.NoError(t, err) 20 return 21 } 22 23 repo, err := openRepositoryWithDefaultContext(clonedPath) 24 if err != nil { 25 assert.NoError(t, err) 26 return 27 } 28 defer repo.Close() 29 30 rd := &bytes.Buffer{} 31 err = repo.GetPatch("8d92fc95^", "8d92fc95", rd) 32 if err != nil { 33 assert.NoError(t, err) 34 return 35 } 36 37 patchb, err := io.ReadAll(rd) 38 if err != nil { 39 assert.NoError(t, err) 40 return 41 } 42 43 patch := string(patchb) 44 assert.Regexp(t, "^From 8d92fc95", patch) 45 assert.Contains(t, patch, "Subject: [PATCH] Add file2.txt") 46 } 47 48 func TestReadPatch(t *testing.T) { 49 // Ensure we can read the patch files 50 bareRepo1Path := filepath.Join(testReposDir, "repo1_bare") 51 repo, err := openRepositoryWithDefaultContext(bareRepo1Path) 52 if err != nil { 53 assert.NoError(t, err) 54 return 55 } 56 defer repo.Close() 57 // This patch doesn't exist 58 noFile, err := repo.ReadPatchCommit(0) 59 assert.Error(t, err) 60 61 // This patch is an empty one (sometimes it's a 404) 62 noCommit, err := repo.ReadPatchCommit(1) 63 assert.Error(t, err) 64 65 // This patch is legit and should return a commit 66 oldCommit, err := repo.ReadPatchCommit(2) 67 if err != nil { 68 assert.NoError(t, err) 69 return 70 } 71 72 assert.Empty(t, noFile) 73 assert.Empty(t, noCommit) 74 assert.Len(t, oldCommit, 40) 75 assert.True(t, oldCommit == "6e8e2a6f9efd71dbe6917816343ed8415ad696c3") 76 } 77 78 func TestReadWritePullHead(t *testing.T) { 79 // Ensure we can write SHA1 head corresponding to PR and open them 80 bareRepo1Path := filepath.Join(testReposDir, "repo1_bare") 81 82 // As we are writing we should clone the repository first 83 clonedPath, err := cloneRepo(t, bareRepo1Path) 84 if err != nil { 85 assert.NoError(t, err) 86 return 87 } 88 89 repo, err := openRepositoryWithDefaultContext(clonedPath) 90 if err != nil { 91 assert.NoError(t, err) 92 return 93 } 94 defer repo.Close() 95 96 // Try to open non-existing Pull 97 _, err = repo.GetRefCommitID(PullPrefix + "0/head") 98 assert.Error(t, err) 99 100 // Write a fake sha1 with only 40 zeros 101 newCommit := "feaf4ba6bc635fec442f46ddd4512416ec43c2c2" 102 err = repo.SetReference(PullPrefix+"1/head", newCommit) 103 if err != nil { 104 assert.NoError(t, err) 105 return 106 } 107 108 // Read the file created 109 headContents, err := repo.GetRefCommitID(PullPrefix + "1/head") 110 if err != nil { 111 assert.NoError(t, err) 112 return 113 } 114 115 assert.Len(t, headContents, 40) 116 assert.True(t, headContents == newCommit) 117 118 // Remove file after the test 119 err = repo.RemoveReference(PullPrefix + "1/head") 120 assert.NoError(t, err) 121 }