2020-01-13 17:17:54 +00:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package gitea
|
|
|
|
|
|
|
|
import (
|
2020-01-26 02:46:38 +00:00
|
|
|
"log"
|
2020-01-13 17:17:54 +00:00
|
|
|
"testing"
|
2020-09-06 18:20:00 +01:00
|
|
|
"time"
|
2020-01-13 17:17:54 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCreateRepo(t *testing.T) {
|
2020-01-26 02:46:38 +00:00
|
|
|
log.Println("== TestCreateRepo ==")
|
2020-01-13 17:17:54 +00:00
|
|
|
c := newTestClient()
|
2020-09-14 03:37:09 +01:00
|
|
|
user, _, err := c.GetMyUserInfo()
|
2020-01-13 17:17:54 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
var repoName = "test1"
|
2020-09-14 03:37:09 +01:00
|
|
|
_, _, err = c.GetRepo(user.UserName, repoName)
|
2020-01-13 17:17:54 +00:00
|
|
|
if err != nil {
|
2020-09-14 03:37:09 +01:00
|
|
|
repo, _, err := c.CreateRepo(CreateRepoOption{
|
2020-01-13 17:17:54 +00:00
|
|
|
Name: repoName,
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, repo)
|
|
|
|
}
|
|
|
|
|
2020-09-14 03:37:09 +01:00
|
|
|
_, err = c.DeleteRepo(user.UserName, repoName)
|
2020-01-13 17:17:54 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2020-01-26 02:46:38 +00:00
|
|
|
|
2020-09-06 18:20:00 +01:00
|
|
|
func TestRepoMigrateAndLanguages(t *testing.T) {
|
|
|
|
log.Println("== TestMigrateRepo ==")
|
|
|
|
c := newTestClient()
|
2020-09-14 03:37:09 +01:00
|
|
|
user, _, uErr := c.GetMyUserInfo()
|
2020-09-06 18:20:00 +01:00
|
|
|
assert.NoError(t, uErr)
|
2020-09-14 03:37:09 +01:00
|
|
|
_, _, err := c.GetRepo(user.UserName, "sdk-mirror")
|
2020-09-06 18:20:00 +01:00
|
|
|
if err == nil {
|
2020-09-14 03:37:09 +01:00
|
|
|
_, _ = c.DeleteRepo(user.UserName, "sdk-mirror")
|
2020-09-06 18:20:00 +01:00
|
|
|
}
|
|
|
|
|
2020-09-14 03:37:09 +01:00
|
|
|
repoM, _, err := c.MigrateRepo(MigrateRepoOption{
|
2020-09-06 18:20:00 +01:00
|
|
|
CloneAddr: "https://gitea.com/gitea/go-sdk.git",
|
|
|
|
RepoName: "sdk-mirror",
|
2020-09-12 14:58:08 +01:00
|
|
|
RepoOwner: user.UserName,
|
2020-09-06 18:20:00 +01:00
|
|
|
Mirror: true,
|
|
|
|
Private: false,
|
|
|
|
Description: "mirror sdk",
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2020-09-14 03:37:09 +01:00
|
|
|
repoG, _, err := c.GetRepo(repoM.Owner.UserName, repoM.Name)
|
2020-09-06 18:20:00 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, repoM.ID, repoG.ID)
|
|
|
|
assert.EqualValues(t, "master", repoG.DefaultBranch)
|
|
|
|
assert.True(t, repoG.Mirror)
|
|
|
|
assert.False(t, repoG.Empty)
|
|
|
|
assert.EqualValues(t, 1, repoG.Watchers)
|
|
|
|
|
|
|
|
log.Println("== TestRepoLanguages ==")
|
|
|
|
time.Sleep(time.Second)
|
2020-09-14 03:37:09 +01:00
|
|
|
lang, _, err := c.GetRepoLanguages(repoM.Owner.UserName, repoM.Name)
|
2020-09-06 18:20:00 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, lang, 2)
|
|
|
|
assert.True(t, 217441 < lang["Go"])
|
2020-11-12 20:59:04 +00:00
|
|
|
assert.True(t, 3614 < lang["Makefile"] && 6000 > lang["Makefile"])
|
2020-09-06 18:20:00 +01:00
|
|
|
}
|
|
|
|
|
2020-02-01 06:26:12 +00:00
|
|
|
func TestSearchRepo(t *testing.T) {
|
|
|
|
log.Println("== TestSearchRepo ==")
|
|
|
|
c := newTestClient()
|
|
|
|
|
|
|
|
repo, err := createTestRepo(t, "RepoSearch1", c)
|
|
|
|
assert.NoError(t, err)
|
2020-09-14 03:37:09 +01:00
|
|
|
_, err = c.AddRepoTopic(repo.Owner.UserName, repo.Name, "TestTopic1")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = c.AddRepoTopic(repo.Owner.UserName, repo.Name, "TestTopic2")
|
|
|
|
assert.NoError(t, err)
|
2020-02-01 06:26:12 +00:00
|
|
|
|
|
|
|
repo, err = createTestRepo(t, "RepoSearch2", c)
|
|
|
|
assert.NoError(t, err)
|
2020-09-14 03:37:09 +01:00
|
|
|
_, err = c.AddRepoTopic(repo.Owner.UserName, repo.Name, "TestTopic1")
|
|
|
|
assert.NoError(t, err)
|
2020-02-01 06:26:12 +00:00
|
|
|
|
2020-09-14 03:37:09 +01:00
|
|
|
repos, _, err := c.SearchRepos(SearchRepoOptions{
|
2020-07-11 20:54:57 +01:00
|
|
|
Keyword: "Search1",
|
|
|
|
KeywordInDescription: true,
|
2020-02-01 06:26:12 +00:00
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, repos)
|
|
|
|
assert.Len(t, repos, 1)
|
|
|
|
|
2020-09-14 03:37:09 +01:00
|
|
|
repos, _, err = c.SearchRepos(SearchRepoOptions{
|
2020-07-11 20:54:57 +01:00
|
|
|
Keyword: "Search",
|
|
|
|
KeywordInDescription: true,
|
2020-02-01 06:26:12 +00:00
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, repos)
|
|
|
|
assert.Len(t, repos, 2)
|
|
|
|
|
2020-09-14 03:37:09 +01:00
|
|
|
repos, _, err = c.SearchRepos(SearchRepoOptions{
|
2020-07-11 20:54:57 +01:00
|
|
|
Keyword: "TestTopic1",
|
|
|
|
KeywordInDescription: true,
|
2020-02-01 06:26:12 +00:00
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, repos)
|
|
|
|
assert.Len(t, repos, 2)
|
|
|
|
|
2020-09-14 03:37:09 +01:00
|
|
|
repos, _, err = c.SearchRepos(SearchRepoOptions{
|
2020-07-11 20:54:57 +01:00
|
|
|
Keyword: "TestTopic2",
|
|
|
|
KeywordInDescription: true,
|
2020-02-01 06:26:12 +00:00
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, repos)
|
|
|
|
assert.Len(t, repos, 1)
|
|
|
|
|
2020-09-14 03:37:09 +01:00
|
|
|
_, err = c.DeleteRepo(repo.Owner.UserName, repo.Name)
|
2020-02-01 06:26:12 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2020-01-26 02:46:38 +00:00
|
|
|
func TestDeleteRepo(t *testing.T) {
|
|
|
|
log.Println("== TestDeleteRepo ==")
|
|
|
|
c := newTestClient()
|
|
|
|
repo, _ := createTestRepo(t, "TestDeleteRepo", c)
|
2020-09-14 03:37:09 +01:00
|
|
|
_, err := c.DeleteRepo(repo.Owner.UserName, repo.Name)
|
|
|
|
assert.NoError(t, err)
|
2020-01-26 02:46:38 +00:00
|
|
|
}
|
|
|
|
|
2020-09-06 23:14:39 +01:00
|
|
|
func TestGetArchive(t *testing.T) {
|
|
|
|
log.Println("== TestGetArchive ==")
|
|
|
|
c := newTestClient()
|
|
|
|
repo, _ := createTestRepo(t, "ToDownload", c)
|
|
|
|
time.Sleep(time.Second / 2)
|
2020-09-14 03:37:09 +01:00
|
|
|
archive, _, err := c.GetArchive(repo.Owner.UserName, repo.Name, "master", ZipArchive)
|
2020-09-06 23:14:39 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, 1620, len(archive))
|
|
|
|
}
|
|
|
|
|
2020-01-26 02:46:38 +00:00
|
|
|
// standard func to create a init repo for test routines
|
|
|
|
func createTestRepo(t *testing.T, name string, c *Client) (*Repository, error) {
|
2020-09-14 03:37:09 +01:00
|
|
|
user, _, uErr := c.GetMyUserInfo()
|
2020-01-26 02:46:38 +00:00
|
|
|
assert.NoError(t, uErr)
|
2020-09-14 03:37:09 +01:00
|
|
|
_, _, err := c.GetRepo(user.UserName, name)
|
2020-01-26 02:46:38 +00:00
|
|
|
if err == nil {
|
2020-09-14 03:37:09 +01:00
|
|
|
_, _ = c.DeleteRepo(user.UserName, name)
|
2020-01-26 02:46:38 +00:00
|
|
|
}
|
2020-09-14 03:37:09 +01:00
|
|
|
repo, _, err := c.CreateRepo(CreateRepoOption{
|
2020-01-26 02:46:38 +00:00
|
|
|
Name: name,
|
|
|
|
Description: "A test Repo: " + name,
|
|
|
|
AutoInit: true,
|
|
|
|
Gitignores: "C,C++",
|
|
|
|
License: "MIT",
|
|
|
|
Readme: "Default",
|
|
|
|
IssueLabels: "Default",
|
|
|
|
Private: false,
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, repo)
|
|
|
|
|
|
|
|
return repo, err
|
|
|
|
}
|