2017-10-27 08:10:54 +02:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2017-10-27 08:10:54 +02:00
|
|
|
|
2022-09-02 21:18:23 +02:00
|
|
|
package integration
|
2017-10-27 08:10:54 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
2019-05-21 21:11:09 +02:00
|
|
|
|
2022-12-03 03:48:26 +01:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-12-10 02:27:50 +01:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2019-12-08 20:15:35 +01:00
|
|
|
code_indexer "code.gitea.io/gitea/modules/indexer/code"
|
2019-09-11 19:26:28 +02:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2024-02-20 12:05:42 +01:00
|
|
|
"code.gitea.io/gitea/modules/test"
|
2022-09-02 21:18:23 +02:00
|
|
|
"code.gitea.io/gitea/tests"
|
2017-10-27 08:10:54 +02:00
|
|
|
|
|
|
|
"github.com/PuerkitoBio/goquery"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2024-02-20 12:05:42 +01:00
|
|
|
func resultFilenames(t testing.TB, doc *goquery.Selection) []string {
|
|
|
|
filenameSelections := doc.Find(".header").Find("span.file")
|
2017-10-27 08:10:54 +02:00
|
|
|
result := make([]string, filenameSelections.Length())
|
|
|
|
filenameSelections.Each(func(i int, selection *goquery.Selection) {
|
|
|
|
result[i] = selection.Text()
|
|
|
|
})
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2024-02-20 12:05:42 +01:00
|
|
|
func checkResultLinks(t *testing.T, substr string, doc *goquery.Selection) {
|
|
|
|
t.Helper()
|
|
|
|
linkSelections := doc.Find("a[href]")
|
|
|
|
linkSelections.Each(func(i int, selection *goquery.Selection) {
|
|
|
|
assert.Contains(t, selection.AttrOr("href", ""), substr)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testSearchRepo(t *testing.T, useExternalIndexer bool) {
|
2022-09-02 21:18:23 +02:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
2024-02-20 12:05:42 +01:00
|
|
|
defer test.MockVariableValue(&setting.Indexer.RepoIndexerEnabled, useExternalIndexer)()
|
2017-10-27 08:10:54 +02:00
|
|
|
|
2022-12-03 03:48:26 +01:00
|
|
|
repo, err := repo_model.GetRepositoryByOwnerAndName(db.DefaultContext, "user2", "repo1")
|
2019-05-21 21:11:09 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2024-02-20 12:05:42 +01:00
|
|
|
gitReference := "/branch/" + repo.DefaultBranch
|
2019-09-11 19:26:28 +02:00
|
|
|
|
2024-02-20 12:05:42 +01:00
|
|
|
if useExternalIndexer {
|
|
|
|
gitReference = "/commit/"
|
|
|
|
executeIndexer(t, repo, code_indexer.UpdateRepoIndexer)
|
|
|
|
}
|
2019-09-11 19:26:28 +02:00
|
|
|
|
2024-02-20 12:05:42 +01:00
|
|
|
testSearch(t, "/user2/repo1/search?q=Description&page=1", gitReference, []string{"README.md"})
|
2019-09-11 19:26:28 +02:00
|
|
|
|
2024-02-20 12:05:42 +01:00
|
|
|
if useExternalIndexer {
|
|
|
|
setting.Indexer.IncludePatterns = setting.IndexerGlobFromString("**.txt")
|
|
|
|
setting.Indexer.ExcludePatterns = setting.IndexerGlobFromString("**/y/**")
|
|
|
|
|
|
|
|
repo, err = repo_model.GetRepositoryByOwnerAndName(db.DefaultContext, "user2", "glob")
|
|
|
|
assert.NoError(t, err)
|
2019-09-11 19:26:28 +02:00
|
|
|
|
2024-02-20 12:05:42 +01:00
|
|
|
executeIndexer(t, repo, code_indexer.UpdateRepoIndexer)
|
2019-09-11 19:26:28 +02:00
|
|
|
|
2024-02-20 12:05:42 +01:00
|
|
|
testSearch(t, "/user2/glob/search?q=loren&page=1", gitReference, []string{"a.txt"})
|
|
|
|
testSearch(t, "/user2/glob/search?q=file3&page=1", gitReference, []string{"x/b.txt"})
|
|
|
|
testSearch(t, "/user2/glob/search?q=file4&page=1", gitReference, []string{})
|
|
|
|
testSearch(t, "/user2/glob/search?q=file5&page=1", gitReference, []string{})
|
|
|
|
}
|
2019-09-11 19:26:28 +02:00
|
|
|
}
|
|
|
|
|
2024-02-20 12:05:42 +01:00
|
|
|
func TestIndexerSearchRepo(t *testing.T) {
|
|
|
|
testSearchRepo(t, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoIndexerSearchRepo(t *testing.T) {
|
|
|
|
testSearchRepo(t, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testSearch(t *testing.T, url, gitRef string, expected []string) {
|
2023-12-22 00:59:59 +01:00
|
|
|
req := NewRequest(t, "GET", url)
|
2019-09-11 19:26:28 +02:00
|
|
|
resp := MakeRequest(t, req, http.StatusOK)
|
|
|
|
|
2024-02-20 12:05:42 +01:00
|
|
|
doc := NewHTMLParser(t, resp.Body).doc.
|
|
|
|
Find(".repository.search").
|
|
|
|
Find(".repo-search-result")
|
|
|
|
|
|
|
|
filenames := resultFilenames(t, doc)
|
2019-09-11 19:26:28 +02:00
|
|
|
assert.EqualValues(t, expected, filenames)
|
2024-02-20 12:05:42 +01:00
|
|
|
|
|
|
|
checkResultLinks(t, gitRef, doc)
|
2019-09-11 19:26:28 +02:00
|
|
|
}
|
|
|
|
|
2021-12-10 02:27:50 +01:00
|
|
|
func executeIndexer(t *testing.T, repo *repo_model.Repository, op func(*repo_model.Repository)) {
|
2020-09-07 17:05:08 +02:00
|
|
|
op(repo)
|
2017-10-27 08:10:54 +02:00
|
|
|
}
|