mirror of https://github.com/go-gitea/gitea
Git with a cup of tea, painless self-hosted git service
Mirror for internal git.with.parts use
https://git.with.parts
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
177 lines
6.2 KiB
177 lines
6.2 KiB
7 years ago
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
||
2 years ago
|
// SPDX-License-Identifier: MIT
|
||
7 years ago
|
|
||
2 years ago
|
package git_test
|
||
7 years ago
|
|
||
|
import (
|
||
|
"testing"
|
||
|
|
||
3 years ago
|
"code.gitea.io/gitea/models/db"
|
||
2 years ago
|
git_model "code.gitea.io/gitea/models/git"
|
||
2 years ago
|
issues_model "code.gitea.io/gitea/models/issues"
|
||
3 years ago
|
repo_model "code.gitea.io/gitea/models/repo"
|
||
3 years ago
|
"code.gitea.io/gitea/models/unittest"
|
||
1 year ago
|
"code.gitea.io/gitea/modules/util"
|
||
3 years ago
|
|
||
7 years ago
|
"github.com/stretchr/testify/assert"
|
||
|
)
|
||
|
|
||
|
func TestAddDeletedBranch(t *testing.T) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
2 years ago
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
||
1 year ago
|
firstBranch := unittest.AssertExistsAndLoadBean(t, &git_model.Branch{ID: 1})
|
||
7 years ago
|
|
||
1 year ago
|
assert.True(t, firstBranch.IsDeleted)
|
||
|
assert.NoError(t, git_model.AddDeletedBranch(db.DefaultContext, repo.ID, firstBranch.Name, firstBranch.DeletedByID))
|
||
|
assert.NoError(t, git_model.AddDeletedBranch(db.DefaultContext, repo.ID, "branch2", int64(1)))
|
||
|
|
||
|
secondBranch := unittest.AssertExistsAndLoadBean(t, &git_model.Branch{RepoID: repo.ID, Name: "branch2"})
|
||
|
assert.True(t, secondBranch.IsDeleted)
|
||
|
|
||
|
err := git_model.UpdateBranch(db.DefaultContext, repo.ID, secondBranch.Name, secondBranch.CommitID, secondBranch.CommitMessage, secondBranch.PusherID, secondBranch.CommitTime.AsLocalTime())
|
||
|
assert.NoError(t, err)
|
||
7 years ago
|
}
|
||
7 years ago
|
|
||
7 years ago
|
func TestGetDeletedBranches(t *testing.T) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
2 years ago
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
||
7 years ago
|
|
||
1 year ago
|
branches, err := git_model.FindBranches(db.DefaultContext, git_model.FindBranchOptions{
|
||
|
ListOptions: db.ListOptions{
|
||
|
ListAll: true,
|
||
|
},
|
||
|
RepoID: repo.ID,
|
||
|
IsDeletedBranch: util.OptionalBoolTrue,
|
||
|
})
|
||
7 years ago
|
assert.NoError(t, err)
|
||
|
assert.Len(t, branches, 2)
|
||
|
}
|
||
|
|
||
|
func TestGetDeletedBranch(t *testing.T) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
1 year ago
|
firstBranch := unittest.AssertExistsAndLoadBean(t, &git_model.Branch{ID: 1})
|
||
7 years ago
|
|
||
7 years ago
|
assert.NotNil(t, getDeletedBranch(t, firstBranch))
|
||
|
}
|
||
|
|
||
|
func TestDeletedBranchLoadUser(t *testing.T) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
7 years ago
|
|
||
1 year ago
|
firstBranch := unittest.AssertExistsAndLoadBean(t, &git_model.Branch{ID: 1})
|
||
|
secondBranch := unittest.AssertExistsAndLoadBean(t, &git_model.Branch{ID: 2})
|
||
7 years ago
|
|
||
7 years ago
|
branch := getDeletedBranch(t, firstBranch)
|
||
|
assert.Nil(t, branch.DeletedBy)
|
||
1 year ago
|
branch.LoadDeletedBy(db.DefaultContext)
|
||
7 years ago
|
assert.NotNil(t, branch.DeletedBy)
|
||
|
assert.Equal(t, "user1", branch.DeletedBy.Name)
|
||
|
|
||
|
branch = getDeletedBranch(t, secondBranch)
|
||
|
assert.Nil(t, branch.DeletedBy)
|
||
1 year ago
|
branch.LoadDeletedBy(db.DefaultContext)
|
||
7 years ago
|
assert.NotNil(t, branch.DeletedBy)
|
||
|
assert.Equal(t, "Ghost", branch.DeletedBy.Name)
|
||
|
}
|
||
|
|
||
|
func TestRemoveDeletedBranch(t *testing.T) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
2 years ago
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
||
7 years ago
|
|
||
1 year ago
|
firstBranch := unittest.AssertExistsAndLoadBean(t, &git_model.Branch{ID: 1})
|
||
7 years ago
|
|
||
2 years ago
|
err := git_model.RemoveDeletedBranchByID(db.DefaultContext, repo.ID, 1)
|
||
7 years ago
|
assert.NoError(t, err)
|
||
3 years ago
|
unittest.AssertNotExistsBean(t, firstBranch)
|
||
1 year ago
|
unittest.AssertExistsAndLoadBean(t, &git_model.Branch{ID: 2})
|
||
7 years ago
|
}
|
||
|
|
||
1 year ago
|
func getDeletedBranch(t *testing.T, branch *git_model.Branch) *git_model.Branch {
|
||
2 years ago
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
||
7 years ago
|
|
||
2 years ago
|
deletedBranch, err := git_model.GetDeletedBranchByID(db.DefaultContext, repo.ID, branch.ID)
|
||
7 years ago
|
assert.NoError(t, err)
|
||
|
assert.Equal(t, branch.ID, deletedBranch.ID)
|
||
|
assert.Equal(t, branch.Name, deletedBranch.Name)
|
||
1 year ago
|
assert.Equal(t, branch.CommitID, deletedBranch.CommitID)
|
||
7 years ago
|
assert.Equal(t, branch.DeletedByID, deletedBranch.DeletedByID)
|
||
|
|
||
|
return deletedBranch
|
||
|
}
|
||
3 years ago
|
|
||
|
func TestFindRenamedBranch(t *testing.T) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
2 years ago
|
branch, exist, err := git_model.FindRenamedBranch(db.DefaultContext, 1, "dev")
|
||
3 years ago
|
assert.NoError(t, err)
|
||
2 years ago
|
assert.True(t, exist)
|
||
3 years ago
|
assert.Equal(t, "master", branch.To)
|
||
|
|
||
2 years ago
|
_, exist, err = git_model.FindRenamedBranch(db.DefaultContext, 1, "unknow")
|
||
3 years ago
|
assert.NoError(t, err)
|
||
2 years ago
|
assert.False(t, exist)
|
||
3 years ago
|
}
|
||
|
|
||
|
func TestRenameBranch(t *testing.T) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
2 years ago
|
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
||
3 years ago
|
_isDefault := false
|
||
|
|
||
2 years ago
|
ctx, committer, err := db.TxContext(db.DefaultContext)
|
||
3 years ago
|
defer committer.Close()
|
||
|
assert.NoError(t, err)
|
||
2 years ago
|
assert.NoError(t, git_model.UpdateProtectBranch(ctx, repo1, &git_model.ProtectedBranch{
|
||
2 years ago
|
RepoID: repo1.ID,
|
||
|
RuleName: "master",
|
||
2 years ago
|
}, git_model.WhitelistOptions{}))
|
||
3 years ago
|
assert.NoError(t, committer.Commit())
|
||
3 years ago
|
|
||
2 years ago
|
assert.NoError(t, git_model.RenameBranch(db.DefaultContext, repo1, "master", "main", func(isDefault bool) error {
|
||
3 years ago
|
_isDefault = isDefault
|
||
|
return nil
|
||
|
}))
|
||
|
|
||
2 years ago
|
assert.True(t, _isDefault)
|
||
2 years ago
|
repo1 = unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
||
3 years ago
|
assert.Equal(t, "main", repo1.DefaultBranch)
|
||
|
|
||
2 years ago
|
pull := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 1}) // merged
|
||
3 years ago
|
assert.Equal(t, "master", pull.BaseBranch)
|
||
|
|
||
2 years ago
|
pull = unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 2}) // open
|
||
3 years ago
|
assert.Equal(t, "main", pull.BaseBranch)
|
||
|
|
||
2 years ago
|
renamedBranch := unittest.AssertExistsAndLoadBean(t, &git_model.RenamedBranch{ID: 2})
|
||
3 years ago
|
assert.Equal(t, "master", renamedBranch.From)
|
||
|
assert.Equal(t, "main", renamedBranch.To)
|
||
|
assert.Equal(t, int64(1), renamedBranch.RepoID)
|
||
|
|
||
2 years ago
|
unittest.AssertExistsAndLoadBean(t, &git_model.ProtectedBranch{
|
||
2 years ago
|
RepoID: repo1.ID,
|
||
|
RuleName: "main",
|
||
3 years ago
|
})
|
||
|
}
|
||
3 years ago
|
|
||
|
func TestOnlyGetDeletedBranchOnCorrectRepo(t *testing.T) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
3 years ago
|
|
||
|
// Get deletedBranch with ID of 1 on repo with ID 2.
|
||
|
// This should return a nil branch as this deleted branch
|
||
|
// is actually on repo with ID 1.
|
||
2 years ago
|
repo2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2})
|
||
3 years ago
|
|
||
2 years ago
|
deletedBranch, err := git_model.GetDeletedBranchByID(db.DefaultContext, repo2.ID, 1)
|
||
3 years ago
|
|
||
1 year ago
|
// Expect error, and the returned branch is nil.
|
||
|
assert.Error(t, err)
|
||
3 years ago
|
assert.Nil(t, deletedBranch)
|
||
|
|
||
|
// Now get the deletedBranch with ID of 1 on repo with ID 1.
|
||
|
// This should return the deletedBranch.
|
||
2 years ago
|
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
||
3 years ago
|
|
||
2 years ago
|
deletedBranch, err = git_model.GetDeletedBranchByID(db.DefaultContext, repo1.ID, 1)
|
||
3 years ago
|
|
||
|
// Expect no error, and the returned branch to be not nil.
|
||
|
assert.NoError(t, err)
|
||
|
assert.NotNil(t, deletedBranch)
|
||
|
}
|