Files
Gitea/services/repository/create_test.go
T
silverwind eb4468ff4e enhance!: raise minimum git version to 2.34 (#39565)
Raise the minimum git version to 2.34, the version in Ubuntu 22.04,
Debian 12 and RHEL 8 ship newer, and remove the fallbacks it makes
obsolete.

- Always enable AGit
- Use `diff --skip-to` and `apply -3` unconditionally
- Set the default branch of new repos and wikis via `git init
--initial-branch`
- Detect rebase conflicts via `REBASE_HEAD`
2026-10-04 16:26:14 +08:00

69 lines
2.2 KiB
Go

// Copyright 2025 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package repository
import (
"os"
"testing"
repo_model "gitea.dev/models/repo"
"gitea.dev/models/unittest"
user_model "gitea.dev/models/user"
"gitea.dev/modules/git"
"gitea.dev/modules/git/gitrepo"
"github.com/stretchr/testify/assert"
)
func TestCreateRepositoryDirectly(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
// a successful creating repository
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
testRepoName := "created-repo"
t.Run("Success", func(t *testing.T) {
createdRepo, err := CreateRepositoryDirectly(t.Context(), user2, user2, CreateRepoOptions{
Name: testRepoName,
DefaultBranch: "custom-default",
}, true)
assert.NoError(t, err)
assert.NotNil(t, createdRepo)
exist, err := git.IsRepositoryExist(t.Context(), gitrepo.CodeRepoByName(user2.Name, createdRepo.Name))
assert.NoError(t, err)
assert.True(t, exist)
branch, err := git.GetDefaultBranch(t.Context(), createdRepo)
assert.NoError(t, err)
assert.Equal(t, "custom-default", branch)
unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerName: user2.Name, Name: createdRepo.Name})
err = DeleteRepositoryDirectly(t.Context(), createdRepo.ID)
assert.NoError(t, err)
})
t.Run("Failure", func(t *testing.T) {
// a failed creating because some mock data
// create the repository directory so that the creation will fail after database record created.
testFailureRepoName := testRepoName
testFailureRepo := gitrepo.CodeRepoByName(user2.Name, testFailureRepoName)
testFailurePath := gitrepo.RepoLocalPath(testFailureRepo)
assert.NoError(t, os.MkdirAll(testFailurePath, os.ModePerm))
createdRepo2, err := CreateRepositoryDirectly(t.Context(), user2, user2, CreateRepoOptions{
Name: testFailureRepoName,
}, true)
assert.Nil(t, createdRepo2)
assert.Error(t, err)
// assert the cleanup is successful
unittest.AssertNotExistsBean(t, &repo_model.Repository{OwnerName: user2.Name, Name: testFailureRepoName})
exist, err := git.IsRepositoryExist(t.Context(), testFailureRepo)
assert.NoError(t, err)
assert.False(t, exist)
})
}