2017-10-27 14:10:54 +08:00
|
|
|
// Copyright 2017 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 integrations
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
2019-05-22 03:11:09 +08:00
|
|
|
|
2021-12-10 09:27:50 +08:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2019-12-09 03:15:35 +08:00
|
|
|
code_indexer "code.gitea.io/gitea/modules/indexer/code"
|
2019-09-12 01:26:28 +08:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2017-10-27 14:10:54 +08:00
|
|
|
|
|
|
|
"github.com/PuerkitoBio/goquery"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resultFilenames(t testing.TB, doc *HTMLDoc) []string {
|
|
|
|
resultsSelection := doc.doc.Find(".repository.search")
|
|
|
|
assert.EqualValues(t, 1, resultsSelection.Length(),
|
|
|
|
"Invalid template (repo search template has changed?)")
|
|
|
|
filenameSelections := resultsSelection.Find(".repo-search-result").Find(".header").Find("span.file")
|
|
|
|
result := make([]string, filenameSelections.Length())
|
|
|
|
filenameSelections.Each(func(i int, selection *goquery.Selection) {
|
|
|
|
result[i] = selection.Text()
|
|
|
|
})
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSearchRepo(t *testing.T) {
|
2019-11-26 07:21:37 +08:00
|
|
|
defer prepareTestEnv(t)()
|
2017-10-27 14:10:54 +08:00
|
|
|
|
2021-12-10 09:27:50 +08:00
|
|
|
repo, err := repo_model.GetRepositoryByOwnerAndName("user2", "repo1")
|
2019-05-22 03:11:09 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-12-09 03:15:35 +08:00
|
|
|
executeIndexer(t, repo, code_indexer.UpdateRepoIndexer)
|
2019-09-12 01:26:28 +08:00
|
|
|
|
|
|
|
testSearch(t, "/user2/repo1/search?q=Description&page=1", []string{"README.md"})
|
|
|
|
|
|
|
|
setting.Indexer.IncludePatterns = setting.IndexerGlobFromString("**.txt")
|
|
|
|
setting.Indexer.ExcludePatterns = setting.IndexerGlobFromString("**/y/**")
|
|
|
|
|
2021-12-10 09:27:50 +08:00
|
|
|
repo, err = repo_model.GetRepositoryByOwnerAndName("user2", "glob")
|
2019-09-12 01:26:28 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-12-09 03:15:35 +08:00
|
|
|
executeIndexer(t, repo, code_indexer.UpdateRepoIndexer)
|
2019-09-12 01:26:28 +08:00
|
|
|
|
|
|
|
testSearch(t, "/user2/glob/search?q=loren&page=1", []string{"a.txt"})
|
|
|
|
testSearch(t, "/user2/glob/search?q=file3&page=1", []string{"x/b.txt"})
|
|
|
|
testSearch(t, "/user2/glob/search?q=file4&page=1", []string{})
|
|
|
|
testSearch(t, "/user2/glob/search?q=file5&page=1", []string{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testSearch(t *testing.T, url string, expected []string) {
|
|
|
|
req := NewRequestf(t, "GET", url)
|
|
|
|
resp := MakeRequest(t, req, http.StatusOK)
|
|
|
|
|
|
|
|
filenames := resultFilenames(t, NewHTMLParser(t, resp.Body))
|
|
|
|
assert.EqualValues(t, expected, filenames)
|
|
|
|
}
|
|
|
|
|
2021-12-10 09:27:50 +08:00
|
|
|
func executeIndexer(t *testing.T, repo *repo_model.Repository, op func(*repo_model.Repository)) {
|
2020-09-07 23:05:08 +08:00
|
|
|
op(repo)
|
2017-10-27 14:10:54 +08:00
|
|
|
}
|