2017-10-27 09:10:54 +03:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 21:20:29 +03:00
|
|
|
// SPDX-License-Identifier: MIT
|
2017-10-27 09:10:54 +03:00
|
|
|
|
2022-09-02 22:18:23 +03:00
|
|
|
package integration
|
2017-10-27 09:10:54 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
2019-05-21 22:11:09 +03:00
|
|
|
|
2022-12-03 05:48:26 +03:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-12-10 04:27:50 +03:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2019-12-08 22:15:35 +03:00
|
|
|
code_indexer "code.gitea.io/gitea/modules/indexer/code"
|
2019-09-11 20:26:28 +03:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2024-04-06 16:25:39 +03:00
|
|
|
"code.gitea.io/gitea/modules/test"
|
2022-09-02 22:18:23 +03:00
|
|
|
"code.gitea.io/gitea/tests"
|
2017-10-27 09:10:54 +03:00
|
|
|
|
|
|
|
"github.com/PuerkitoBio/goquery"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2024-03-28 08:09:41 +03:00
|
|
|
func resultFilenames(t testing.TB, doc *HTMLDoc) []string {
|
2024-04-14 17:21:31 +03:00
|
|
|
filenameSelections := doc.Find(".repository.search").Find(".repo-search-result").Find(".header").Find("span.file")
|
2017-10-27 09:10:54 +03:00
|
|
|
result := make([]string, filenameSelections.Length())
|
|
|
|
filenameSelections.Each(func(i int, selection *goquery.Selection) {
|
|
|
|
result[i] = selection.Text()
|
|
|
|
})
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2024-04-06 16:25:39 +03:00
|
|
|
func TestSearchRepoIndexer(t *testing.T) {
|
|
|
|
testSearchRepo(t, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSearchRepoNoIndexer(t *testing.T) {
|
|
|
|
testSearchRepo(t, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testSearchRepo(t *testing.T, indexer bool) {
|
2022-09-02 22:18:23 +03:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
2024-04-06 16:25:39 +03:00
|
|
|
defer test.MockVariableValue(&setting.Indexer.RepoIndexerEnabled, indexer)()
|
2017-10-27 09:10:54 +03:00
|
|
|
|
2022-12-03 05:48:26 +03:00
|
|
|
repo, err := repo_model.GetRepositoryByOwnerAndName(db.DefaultContext, "user2", "repo1")
|
2019-05-21 22:11:09 +03:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2024-04-06 16:25:39 +03:00
|
|
|
if indexer {
|
|
|
|
code_indexer.UpdateRepoIndexer(repo)
|
|
|
|
}
|
2024-02-20 14:05:42 +03:00
|
|
|
|
2024-04-14 17:21:31 +03:00
|
|
|
testSearch(t, "/user2/repo1/search?q=Description&page=1", []string{"README.md"}, indexer)
|
2019-09-11 20:26:28 +03:00
|
|
|
|
2024-04-06 16:25:39 +03:00
|
|
|
defer test.MockVariableValue(&setting.Indexer.IncludePatterns, setting.IndexerGlobFromString("**.txt"))()
|
|
|
|
defer test.MockVariableValue(&setting.Indexer.ExcludePatterns, setting.IndexerGlobFromString("**/y/**"))()
|
2019-09-11 20:26:28 +03:00
|
|
|
|
2024-03-28 08:09:41 +03:00
|
|
|
repo, err = repo_model.GetRepositoryByOwnerAndName(db.DefaultContext, "user2", "glob")
|
|
|
|
assert.NoError(t, err)
|
2019-09-11 20:26:28 +03:00
|
|
|
|
2024-04-06 16:25:39 +03:00
|
|
|
if indexer {
|
|
|
|
code_indexer.UpdateRepoIndexer(repo)
|
|
|
|
}
|
2024-02-20 14:05:42 +03:00
|
|
|
|
2024-04-14 17:21:31 +03:00
|
|
|
testSearch(t, "/user2/glob/search?q=loren&page=1", []string{"a.txt"}, indexer)
|
|
|
|
testSearch(t, "/user2/glob/search?q=loren&page=1&fuzzy=false", []string{"a.txt"}, indexer)
|
2024-04-06 16:25:39 +03:00
|
|
|
|
|
|
|
if indexer {
|
|
|
|
// fuzzy search: matches both file3 (x/b.txt) and file1 (a.txt)
|
|
|
|
// when indexer is enabled
|
2024-04-14 17:21:31 +03:00
|
|
|
testSearch(t, "/user2/glob/search?q=file3&page=1", []string{"x/b.txt", "a.txt"}, indexer)
|
|
|
|
testSearch(t, "/user2/glob/search?q=file4&page=1", []string{"x/b.txt", "a.txt"}, indexer)
|
|
|
|
testSearch(t, "/user2/glob/search?q=file5&page=1", []string{"x/b.txt", "a.txt"}, indexer)
|
2024-04-06 16:25:39 +03:00
|
|
|
} else {
|
|
|
|
// fuzzy search: OR of all the keywords
|
|
|
|
// when indexer is disabled
|
2024-04-14 17:21:31 +03:00
|
|
|
testSearch(t, "/user2/glob/search?q=file3+file1&page=1", []string{"a.txt", "x/b.txt"}, indexer)
|
|
|
|
testSearch(t, "/user2/glob/search?q=file4&page=1", []string{}, indexer)
|
|
|
|
testSearch(t, "/user2/glob/search?q=file5&page=1", []string{}, indexer)
|
2024-04-06 16:25:39 +03:00
|
|
|
}
|
|
|
|
|
2024-04-14 17:21:31 +03:00
|
|
|
testSearch(t, "/user2/glob/search?q=file3&page=1&fuzzy=false", []string{"x/b.txt"}, indexer)
|
|
|
|
testSearch(t, "/user2/glob/search?q=file4&page=1&fuzzy=false", []string{}, indexer)
|
|
|
|
testSearch(t, "/user2/glob/search?q=file5&page=1&fuzzy=false", []string{}, indexer)
|
2024-02-20 14:05:42 +03:00
|
|
|
}
|
|
|
|
|
2024-04-14 17:21:31 +03:00
|
|
|
func testSearch(t *testing.T, url string, expected []string, indexer bool) {
|
2023-12-22 02:59:59 +03:00
|
|
|
req := NewRequest(t, "GET", url)
|
2019-09-11 20:26:28 +03:00
|
|
|
resp := MakeRequest(t, req, http.StatusOK)
|
|
|
|
|
2024-04-14 17:21:31 +03:00
|
|
|
doc := NewHTMLParser(t, resp.Body)
|
|
|
|
msg := doc.Find(".repository").Find(".ui.container").Find(".ui.message[data-test-tag=grep]")
|
|
|
|
assert.EqualValues(t, indexer, len(msg.Nodes) == 0)
|
|
|
|
|
|
|
|
filenames := resultFilenames(t, doc)
|
2019-09-11 20:26:28 +03:00
|
|
|
assert.EqualValues(t, expected, filenames)
|
|
|
|
}
|