aboutsummaryrefslogtreecommitdiffstats
path: root/services/repository/adopt_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'services/repository/adopt_test.go')
-rw-r--r--services/repository/adopt_test.go35
1 files changed, 31 insertions, 4 deletions
diff --git a/services/repository/adopt_test.go b/services/repository/adopt_test.go
index 123cedc1f2..86f586c748 100644
--- a/services/repository/adopt_test.go
+++ b/services/repository/adopt_test.go
@@ -14,6 +14,7 @@ import (
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/setting"
+ "code.gitea.io/gitea/modules/util"
"github.com/stretchr/testify/assert"
)
@@ -28,7 +29,7 @@ func TestCheckUnadoptedRepositories_Add(t *testing.T) {
}
total := 30
- for i := 0; i < total; i++ {
+ for range total {
unadopted.add("something")
}
@@ -71,7 +72,7 @@ func TestListUnadoptedRepositories_ListOptions(t *testing.T) {
username := "user2"
unadoptedList := []string{path.Join(username, "unadopted1"), path.Join(username, "unadopted2")}
for _, unadopted := range unadoptedList {
- _ = os.Mkdir(path.Join(setting.RepoRootPath, unadopted+".git"), 0o755)
+ _ = os.Mkdir(filepath.Join(setting.RepoRootPath, unadopted+".git"), 0o755)
}
opts := db.ListOptions{Page: 1, PageSize: 1}
@@ -89,10 +90,36 @@ func TestListUnadoptedRepositories_ListOptions(t *testing.T) {
func TestAdoptRepository(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
- assert.NoError(t, unittest.SyncDirs(filepath.Join(setting.RepoRootPath, "user2", "repo1.git"), filepath.Join(setting.RepoRootPath, "user2", "test-adopt.git")))
+
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
- _, err := AdoptRepository(db.DefaultContext, user2, user2, CreateRepoOptions{Name: "test-adopt"})
+
+ // a successful adopt
+ destDir := filepath.Join(setting.RepoRootPath, user2.Name, "test-adopt.git")
+ assert.NoError(t, unittest.SyncDirs(filepath.Join(setting.RepoRootPath, user2.Name, "repo1.git"), destDir))
+
+ adoptedRepo, err := AdoptRepository(db.DefaultContext, user2, user2, CreateRepoOptions{Name: "test-adopt"})
assert.NoError(t, err)
repoTestAdopt := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{Name: "test-adopt"})
assert.Equal(t, "sha1", repoTestAdopt.ObjectFormatName)
+
+ // just delete the adopted repo's db records
+ err = deleteFailedAdoptRepository(adoptedRepo.ID)
+ assert.NoError(t, err)
+
+ unittest.AssertNotExistsBean(t, &repo_model.Repository{OwnerName: user2.Name, Name: "test-adopt"})
+
+ // a failed adopt because some mock data
+ // remove the hooks directory and create a file so that we cannot create the hooks successfully
+ _ = os.RemoveAll(filepath.Join(destDir, "hooks", "update.d"))
+ assert.NoError(t, os.WriteFile(filepath.Join(destDir, "hooks", "update.d"), []byte("tests"), os.ModePerm))
+
+ adoptedRepo, err = AdoptRepository(db.DefaultContext, user2, user2, CreateRepoOptions{Name: "test-adopt"})
+ assert.Error(t, err)
+ assert.Nil(t, adoptedRepo)
+
+ unittest.AssertNotExistsBean(t, &repo_model.Repository{OwnerName: user2.Name, Name: "test-adopt"})
+
+ exist, err := util.IsExist(repo_model.RepoPath(user2.Name, "test-adopt"))
+ assert.NoError(t, err)
+ assert.True(t, exist) // the repository should be still in the disk
}