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