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