2017-10-27 14:10:54 +08:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-28 02:20:29 +08:00
|
|
|
// SPDX-License-Identifier: MIT
|
2017-10-27 14:10:54 +08:00
|
|
|
|
2022-09-03 03:18:23 +08:00
|
|
|
package integration
|
2017-10-27 14:10:54 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
2019-05-22 03:11:09 +08:00
|
|
|
|
2022-12-03 10:48:26 +08:00
|
|
|
"code.gitea.io/gitea/models/db"
|
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"
|
2022-09-03 03:18:23 +08:00
|
|
|
"code.gitea.io/gitea/tests"
|
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 {
|
2022-01-27 16:30:51 +08:00
|
|
|
filenameSelections := doc.doc.Find(".repository.search").Find(".repo-search-result").Find(".header").Find("span.file")
|
2017-10-27 14:10:54 +08:00
|
|
|
result := make([]string, filenameSelections.Length())
|
|
|
|
filenameSelections.Each(func(i int, selection *goquery.Selection) {
|
|
|
|
result[i] = selection.Text()
|
|
|
|
})
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSearchRepo(t *testing.T) {
|
2022-09-03 03:18:23 +08:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
2017-10-27 14:10:54 +08:00
|
|
|
|
2022-12-03 10:48:26 +08:00
|
|
|
repo, err := repo_model.GetRepositoryByOwnerAndName(db.DefaultContext, "user2", "repo1")
|
2019-05-22 03:11:09 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2024-03-23 23:45:13 +08:00
|
|
|
code_indexer.UpdateRepoIndexer(repo)
|
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/**")
|
|
|
|
|
2022-12-03 10:48:26 +08:00
|
|
|
repo, err = repo_model.GetRepositoryByOwnerAndName(db.DefaultContext, "user2", "glob")
|
2019-09-12 01:26:28 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2024-03-23 23:45:13 +08:00
|
|
|
code_indexer.UpdateRepoIndexer(repo)
|
2019-09-12 01:26:28 +08:00
|
|
|
|
|
|
|
testSearch(t, "/user2/glob/search?q=loren&page=1", []string{"a.txt"})
|
2024-03-23 23:45:13 +08:00
|
|
|
testSearch(t, "/user2/glob/search?q=loren&page=1&t=match", []string{"a.txt"})
|
|
|
|
testSearch(t, "/user2/glob/search?q=file3&page=1", []string{"x/b.txt", "a.txt"})
|
|
|
|
testSearch(t, "/user2/glob/search?q=file3&page=1&t=match", []string{"x/b.txt", "a.txt"})
|
|
|
|
testSearch(t, "/user2/glob/search?q=file4&page=1&t=match", []string{"x/b.txt", "a.txt"})
|
|
|
|
testSearch(t, "/user2/glob/search?q=file5&page=1&t=match", []string{"x/b.txt", "a.txt"})
|
2019-09-12 01:26:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func testSearch(t *testing.T, url string, expected []string) {
|
2023-12-22 07:59:59 +08:00
|
|
|
req := NewRequest(t, "GET", url)
|
2019-09-12 01:26:28 +08:00
|
|
|
resp := MakeRequest(t, req, http.StatusOK)
|
|
|
|
|
|
|
|
filenames := resultFilenames(t, NewHTMLParser(t, resp.Body))
|
|
|
|
assert.EqualValues(t, expected, filenames)
|
|
|
|
}
|