mirror of
https://github.com/go-gitea/gitea.git
synced 2026-11-19 15:14:40 +09:00
If the stderr output is not handled, then it needs to use RunWithStderr to make sure the git error message is included in the returned error. Otherwise, when the git command fails, the error log won't show useful information. Ref: * #39795 : no useful information in log messages * https://github.com/go-gitea/gitea/pull/40160#pullrequestreview-5752720866
175 lines
4.2 KiB
Go
175 lines
4.2 KiB
Go
// Copyright 2018 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package git
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"gitea.dev/modules/git/gitcmd"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestGetFormatPatch(t *testing.T) {
|
|
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
|
|
clonedPath, err := cloneRepo(t, bareRepo1Path)
|
|
if err != nil {
|
|
assert.NoError(t, err)
|
|
return
|
|
}
|
|
|
|
repo, err := OpenRepositoryLocal(t.Context(), clonedPath)
|
|
if err != nil {
|
|
assert.NoError(t, err)
|
|
return
|
|
}
|
|
defer repo.Close()
|
|
|
|
rd := &bytes.Buffer{}
|
|
err = repo.GetPatch(t.Context(), "8d92fc95^...8d92fc95", rd)
|
|
if err != nil {
|
|
assert.NoError(t, err)
|
|
return
|
|
}
|
|
|
|
patchb, err := io.ReadAll(rd)
|
|
if err != nil {
|
|
assert.NoError(t, err)
|
|
return
|
|
}
|
|
|
|
patch := string(patchb)
|
|
assert.Regexp(t, "^From 8d92fc95", patch)
|
|
assert.Contains(t, patch, "Subject: [PATCH] Add file2.txt")
|
|
|
|
err = repo.GetPatch(t.Context(), "gitea-missing-revision", io.Discard)
|
|
assert.ErrorContains(t, err, "fatal: ambiguous argument 'gitea-missing-revision'")
|
|
}
|
|
|
|
func TestReadPatch(t *testing.T) {
|
|
// Ensure we can read the patch files
|
|
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
|
|
repo, err := OpenRepositoryLocal(t.Context(), bareRepo1Path)
|
|
if err != nil {
|
|
assert.NoError(t, err)
|
|
return
|
|
}
|
|
defer repo.Close()
|
|
// This patch doesn't exist
|
|
noFile, err := repo.ReadPatchCommit(0)
|
|
assert.Error(t, err)
|
|
|
|
// This patch is an empty one (sometimes it's a 404)
|
|
noCommit, err := repo.ReadPatchCommit(1)
|
|
assert.Error(t, err)
|
|
|
|
// This patch is legit and should return a commit
|
|
oldCommit, err := repo.ReadPatchCommit(2)
|
|
if err != nil {
|
|
assert.NoError(t, err)
|
|
return
|
|
}
|
|
|
|
assert.Empty(t, noFile)
|
|
assert.Empty(t, noCommit)
|
|
assert.Len(t, oldCommit, 40)
|
|
assert.Equal(t, "6e8e2a6f9efd71dbe6917816343ed8415ad696c3", oldCommit)
|
|
}
|
|
|
|
func TestReadWritePullHead(t *testing.T) {
|
|
// Ensure we can write SHA1 head corresponding to PR and open them
|
|
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
|
|
|
|
// As we are writing we should clone the repository first
|
|
clonedPath, err := cloneRepo(t, bareRepo1Path)
|
|
if err != nil {
|
|
assert.NoError(t, err)
|
|
return
|
|
}
|
|
|
|
repo, err := OpenRepositoryLocal(t.Context(), clonedPath)
|
|
if err != nil {
|
|
assert.NoError(t, err)
|
|
return
|
|
}
|
|
defer repo.Close()
|
|
|
|
// Try to open non-existing Pull
|
|
_, err = repo.GetRefCommitID(t.Context(), PullPrefix+"0/head")
|
|
assert.Error(t, err)
|
|
|
|
// Write a fake sha1 with only 40 zeros
|
|
newCommit := "feaf4ba6bc635fec442f46ddd4512416ec43c2c2"
|
|
_, _, err = gitcmd.NewCommand("update-ref").
|
|
AddDynamicArguments(PullPrefix+"1/head", newCommit).
|
|
WithRepo(repo).
|
|
RunStdString(t.Context())
|
|
if err != nil {
|
|
assert.NoError(t, err)
|
|
return
|
|
}
|
|
|
|
// Read the file created
|
|
headContents, err := repo.GetRefCommitID(t.Context(), PullPrefix+"1/head")
|
|
if err != nil {
|
|
assert.NoError(t, err)
|
|
return
|
|
}
|
|
|
|
assert.Len(t, headContents, 40)
|
|
assert.Equal(t, headContents, newCommit)
|
|
|
|
// Remove file after the test
|
|
_, _, err = gitcmd.NewCommand("update-ref", "--no-deref", "-d").
|
|
AddDynamicArguments(PullPrefix + "1/head").
|
|
WithRepo(repo).
|
|
RunStdString(t.Context())
|
|
assert.NoError(t, err)
|
|
}
|
|
|
|
func TestGetCommitFilesChanged(t *testing.T) {
|
|
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
|
|
repo, err := OpenRepositoryLocal(t.Context(), bareRepo1Path)
|
|
assert.NoError(t, err)
|
|
defer repo.Close()
|
|
|
|
objectFormat, err := repo.GetObjectFormat(t.Context())
|
|
assert.NoError(t, err)
|
|
|
|
testCases := []struct {
|
|
base, head string
|
|
files []string
|
|
}{
|
|
{
|
|
objectFormat.EmptyObjectID().String(),
|
|
"95bb4d39648ee7e325106df01a621c530863a653",
|
|
[]string{"file1.txt"},
|
|
},
|
|
{
|
|
objectFormat.EmptyObjectID().String(),
|
|
"8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2",
|
|
[]string{"file2.txt"},
|
|
},
|
|
{
|
|
"95bb4d39648ee7e325106df01a621c530863a653",
|
|
"8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2",
|
|
[]string{"file2.txt"},
|
|
},
|
|
{
|
|
objectFormat.EmptyTree().String(),
|
|
"8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2",
|
|
[]string{"file1.txt", "file2.txt"},
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
changedFiles, err := repo.GetFilesChangedBetween(t.Context(), tc.base, tc.head)
|
|
assert.NoError(t, err)
|
|
assert.ElementsMatch(t, tc.files, changedFiles)
|
|
}
|
|
}
|