mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-08-29 21:53:53 +00:00
Backport of https://codeberg.org/forgejo/forgejo/pulls/8842
Contains a partial cherry-pick of 184e068f37
, for the parts the PR depends on. The whole commit is way too involved to cherry-pick as a whole.
Co-authored-by: Danko Aleksejevs <danko@very.lv>
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/8862
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: BtbN <btbn@btbn.de>
Co-committed-by: BtbN <btbn@btbn.de>
126 lines
4.7 KiB
Go
126 lines
4.7 KiB
Go
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package pull
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"forgejo.org/models"
|
|
issues_model "forgejo.org/models/issues"
|
|
repo_model "forgejo.org/models/repo"
|
|
"forgejo.org/models/unittest"
|
|
user_model "forgejo.org/models/user"
|
|
"forgejo.org/modules/gitrepo"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func Test_expandDefaultMergeMessage(t *testing.T) {
|
|
type args struct {
|
|
template string
|
|
vars map[string]string
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
want string
|
|
wantBody string
|
|
}{
|
|
{
|
|
name: "single line",
|
|
args: args{
|
|
template: "Merge ${PullRequestTitle}",
|
|
vars: map[string]string{
|
|
"PullRequestTitle": "PullRequestTitle",
|
|
"PullRequestDescription": "Pull\nRequest\nDescription\n",
|
|
},
|
|
},
|
|
want: "Merge PullRequestTitle",
|
|
wantBody: "",
|
|
},
|
|
{
|
|
name: "multiple lines",
|
|
args: args{
|
|
template: "Merge ${PullRequestTitle}\nDescription:\n\n${PullRequestDescription}\n",
|
|
vars: map[string]string{
|
|
"PullRequestTitle": "PullRequestTitle",
|
|
"PullRequestDescription": "Pull\nRequest\nDescription\n",
|
|
},
|
|
},
|
|
want: "Merge PullRequestTitle",
|
|
wantBody: "Description:\n\nPull\nRequest\nDescription\n",
|
|
},
|
|
{
|
|
name: "leading newlines",
|
|
args: args{
|
|
template: "\n\n\nMerge ${PullRequestTitle}\n\n\nDescription:\n\n${PullRequestDescription}\n",
|
|
vars: map[string]string{
|
|
"PullRequestTitle": "PullRequestTitle",
|
|
"PullRequestDescription": "Pull\nRequest\nDescription\n",
|
|
},
|
|
},
|
|
want: "Merge PullRequestTitle",
|
|
wantBody: "Description:\n\nPull\nRequest\nDescription\n",
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, got1 := expandDefaultMergeMessage(tt.args.template, tt.args.vars)
|
|
assert.Equalf(t, tt.want, got, "expandDefaultMergeMessage(%v, %v)", tt.args.template, tt.args.vars)
|
|
assert.Equalf(t, tt.wantBody, got1, "expandDefaultMergeMessage(%v, %v)", tt.args.template, tt.args.vars)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestAddCommitMessageTailer(t *testing.T) {
|
|
// add tailer for empty message
|
|
assert.Equal(t, "\n\nTest-tailer: TestValue", AddCommitMessageTrailer("", "Test-tailer", "TestValue"))
|
|
|
|
// add tailer for message without newlines
|
|
assert.Equal(t, "title\n\nTest-tailer: TestValue", AddCommitMessageTrailer("title", "Test-tailer", "TestValue"))
|
|
assert.Equal(t, "title\n\nNot tailer: xxx\n\nTest-tailer: TestValue", AddCommitMessageTrailer("title\n\nNot tailer: xxx", "Test-tailer", "TestValue"))
|
|
assert.Equal(t, "title\n\nNotTailer: xxx\n\nTest-tailer: TestValue", AddCommitMessageTrailer("title\n\nNotTailer: xxx", "Test-tailer", "TestValue"))
|
|
assert.Equal(t, "title\n\nnot-tailer: xxx\n\nTest-tailer: TestValue", AddCommitMessageTrailer("title\n\nnot-tailer: xxx", "Test-tailer", "TestValue"))
|
|
|
|
// add tailer for message with one EOL
|
|
assert.Equal(t, "title\n\nTest-tailer: TestValue", AddCommitMessageTrailer("title\n", "Test-tailer", "TestValue"))
|
|
|
|
// add tailer for message with two EOLs
|
|
assert.Equal(t, "title\n\nTest-tailer: TestValue", AddCommitMessageTrailer("title\n\n", "Test-tailer", "TestValue"))
|
|
|
|
// add tailer for message with existing tailer (won't duplicate)
|
|
assert.Equal(t, "title\n\nTest-tailer: TestValue", AddCommitMessageTrailer("title\n\nTest-tailer: TestValue", "Test-tailer", "TestValue"))
|
|
assert.Equal(t, "title\n\nTest-tailer: TestValue\n", AddCommitMessageTrailer("title\n\nTest-tailer: TestValue\n", "Test-tailer", "TestValue"))
|
|
|
|
// add tailer for message with existing tailer and different value (will append)
|
|
assert.Equal(t, "title\n\nTest-tailer: v1\nTest-tailer: v2", AddCommitMessageTrailer("title\n\nTest-tailer: v1", "Test-tailer", "v2"))
|
|
assert.Equal(t, "title\n\nTest-tailer: v1\nTest-tailer: v2", AddCommitMessageTrailer("title\n\nTest-tailer: v1\n", "Test-tailer", "v2"))
|
|
}
|
|
|
|
func TestMergeMergedPR(t *testing.T) {
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
|
pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 1})
|
|
doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
|
|
|
require.NoError(t, pr.LoadBaseRepo(t.Context()))
|
|
|
|
gitRepo, err := gitrepo.OpenRepository(t.Context(), pr.BaseRepo)
|
|
require.NoError(t, err)
|
|
defer gitRepo.Close()
|
|
|
|
assert.True(t, pr.HasMerged)
|
|
pr.HasMerged = false
|
|
|
|
err = Merge(t.Context(), pr, doer, gitRepo, repo_model.MergeStyleRebase, "", "I should not exist", false)
|
|
require.Error(t, err)
|
|
assert.True(t, models.IsErrPullRequestHasMerged(err))
|
|
|
|
if mergeErr, ok := err.(models.ErrPullRequestHasMerged); ok {
|
|
assert.Equal(t, pr.ID, mergeErr.ID)
|
|
assert.Equal(t, pr.IssueID, mergeErr.IssueID)
|
|
assert.Equal(t, pr.HeadBranch, mergeErr.HeadBranch)
|
|
assert.Equal(t, pr.BaseBranch, mergeErr.BaseBranch)
|
|
}
|
|
}
|