2017-10-27 06:10:54 +00:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 18:20:29 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2017-10-27 06:10:54 +00:00
|
|
|
|
2022-09-02 19:18:23 +00:00
|
|
|
package integration
|
2017-10-27 06:10:54 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
2019-05-21 19:11:09 +00:00
|
|
|
|
2022-12-03 02:48:26 +00:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-12-10 01:27:50 +00:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2019-12-08 19:15:35 +00:00
|
|
|
code_indexer "code.gitea.io/gitea/modules/indexer/code"
|
2019-09-11 17:26:28 +00:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2024-04-06 13:25:39 +00:00
|
|
|
"code.gitea.io/gitea/modules/test"
|
2024-05-14 15:41:03 +00:00
|
|
|
"code.gitea.io/gitea/routers"
|
2022-09-02 19:18:23 +00:00
|
|
|
"code.gitea.io/gitea/tests"
|
2017-10-27 06:10:54 +00:00
|
|
|
|
|
|
|
"github.com/PuerkitoBio/goquery"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2024-07-30 19:41:10 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2017-10-27 06:10:54 +00:00
|
|
|
)
|
|
|
|
|
2024-03-28 05:09:41 +00:00
|
|
|
func resultFilenames(t testing.TB, doc *HTMLDoc) []string {
|
2024-07-10 05:25:32 +00:00
|
|
|
resultSelections := doc.
|
|
|
|
Find(".repository.search").
|
|
|
|
Find("details.repo-search-result")
|
|
|
|
|
|
|
|
result := make([]string, resultSelections.Length())
|
|
|
|
resultSelections.Each(func(i int, selection *goquery.Selection) {
|
2024-08-06 05:57:25 +00:00
|
|
|
assert.Positive(t, selection.Find("div ol li").Length(), 0)
|
|
|
|
assert.Positive(t, selection.Find(".code-inner").Find(".search-highlight").Length(), 0)
|
2024-07-10 05:25:32 +00:00
|
|
|
result[i] = selection.
|
|
|
|
Find(".header").
|
|
|
|
Find("span.file a.file-link").
|
|
|
|
First().
|
|
|
|
Text()
|
2017-10-27 06:10:54 +00:00
|
|
|
})
|
2024-07-10 05:25:32 +00:00
|
|
|
|
2017-10-27 06:10:54 +00:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2024-04-06 13:25:39 +00: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 19:18:23 +00:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
2024-04-06 13:25:39 +00:00
|
|
|
defer test.MockVariableValue(&setting.Indexer.RepoIndexerEnabled, indexer)()
|
2024-05-14 15:41:03 +00:00
|
|
|
defer test.MockVariableValue(&testWebRoutes, routers.NormalRoutes())()
|
2017-10-27 06:10:54 +00:00
|
|
|
|
2022-12-03 02:48:26 +00:00
|
|
|
repo, err := repo_model.GetRepositoryByOwnerAndName(db.DefaultContext, "user2", "repo1")
|
2024-07-30 19:41:10 +00:00
|
|
|
require.NoError(t, err)
|
2019-05-21 19:11:09 +00:00
|
|
|
|
2024-04-06 13:25:39 +00:00
|
|
|
if indexer {
|
|
|
|
code_indexer.UpdateRepoIndexer(repo)
|
|
|
|
}
|
2024-02-20 11:05:42 +00:00
|
|
|
|
2024-04-11 15:42:59 +00:00
|
|
|
testSearch(t, "/user2/repo1/search?q=Description&page=1", []string{"README.md"}, indexer)
|
2019-09-11 17:26:28 +00:00
|
|
|
|
2024-05-14 15:41:03 +00:00
|
|
|
req := NewRequest(t, "HEAD", "/user2/repo1/search/branch/"+repo.DefaultBranch)
|
|
|
|
if indexer {
|
|
|
|
MakeRequest(t, req, http.StatusNotFound)
|
|
|
|
} else {
|
|
|
|
MakeRequest(t, req, http.StatusOK)
|
|
|
|
}
|
|
|
|
|
2024-04-06 13:25:39 +00:00
|
|
|
defer test.MockVariableValue(&setting.Indexer.IncludePatterns, setting.IndexerGlobFromString("**.txt"))()
|
|
|
|
defer test.MockVariableValue(&setting.Indexer.ExcludePatterns, setting.IndexerGlobFromString("**/y/**"))()
|
2019-09-11 17:26:28 +00:00
|
|
|
|
2024-03-28 05:09:41 +00:00
|
|
|
repo, err = repo_model.GetRepositoryByOwnerAndName(db.DefaultContext, "user2", "glob")
|
2024-07-30 19:41:10 +00:00
|
|
|
require.NoError(t, err)
|
2019-09-11 17:26:28 +00:00
|
|
|
|
2024-04-06 13:25:39 +00:00
|
|
|
if indexer {
|
|
|
|
code_indexer.UpdateRepoIndexer(repo)
|
|
|
|
}
|
2024-02-20 11:05:42 +00:00
|
|
|
|
2024-04-11 15:42:59 +00:00
|
|
|
testSearch(t, "/user2/glob/search?q=loren&page=1", []string{"a.txt"}, indexer)
|
2024-08-16 13:45:54 +00:00
|
|
|
testSearch(t, "/user2/glob/search?q=loren&page=1&mode=exact", []string{"a.txt"}, indexer)
|
2024-04-06 13:25:39 +00:00
|
|
|
|
|
|
|
if indexer {
|
|
|
|
// fuzzy search: matches both file3 (x/b.txt) and file1 (a.txt)
|
|
|
|
// when indexer is enabled
|
2024-08-16 13:45:54 +00:00
|
|
|
testSearch(t, "/user2/glob/search?q=file3&mode=fuzzy&page=1", []string{"x/b.txt", "a.txt"}, indexer)
|
|
|
|
testSearch(t, "/user2/glob/search?q=file4&mode=fuzzy&page=1", []string{"x/b.txt", "a.txt"}, indexer)
|
|
|
|
testSearch(t, "/user2/glob/search?q=file5&mode=fuzzy&page=1", []string{"x/b.txt", "a.txt"}, indexer)
|
2024-04-06 13:25:39 +00:00
|
|
|
} else {
|
2024-07-10 05:25:32 +00:00
|
|
|
// fuzzy search: Union/OR of all the keywords
|
2024-04-06 13:25:39 +00:00
|
|
|
// when indexer is disabled
|
2024-08-16 13:45:54 +00:00
|
|
|
testSearch(t, "/user2/glob/search?q=file3+file1&mode=union&page=1", []string{"a.txt", "x/b.txt"}, indexer)
|
|
|
|
testSearch(t, "/user2/glob/search?q=file4&mode=union&page=1", []string{}, indexer)
|
|
|
|
testSearch(t, "/user2/glob/search?q=file5&mode=union&page=1", []string{}, indexer)
|
2024-04-06 13:25:39 +00:00
|
|
|
}
|
|
|
|
|
2024-08-28 09:09:37 +00:00
|
|
|
testSearch(t, "/user2/glob/search?q=file3&page=1&mode=exact", []string{"x/b.txt"}, indexer)
|
|
|
|
testSearch(t, "/user2/glob/search?q=file4&page=1&mode=exact", []string{}, indexer)
|
|
|
|
testSearch(t, "/user2/glob/search?q=file5&page=1&mode=exact", []string{}, indexer)
|
2024-02-20 11:05:42 +00:00
|
|
|
}
|
|
|
|
|
2024-04-11 15:42:59 +00:00
|
|
|
func testSearch(t *testing.T, url string, expected []string, indexer bool) {
|
2023-12-21 23:59:59 +00:00
|
|
|
req := NewRequest(t, "GET", url)
|
2019-09-11 17:26:28 +00:00
|
|
|
resp := MakeRequest(t, req, http.StatusOK)
|
|
|
|
|
2024-04-11 15:42:59 +00:00
|
|
|
doc := NewHTMLParser(t, resp.Body)
|
2024-06-29 06:57:34 +00:00
|
|
|
container := doc.Find(".repository").Find(".ui.container")
|
|
|
|
|
|
|
|
grepMsg := container.Find(".ui.message[data-test-tag=grep]")
|
|
|
|
assert.EqualValues(t, indexer, len(grepMsg.Nodes) == 0)
|
|
|
|
|
|
|
|
branchDropdown := container.Find(".js-branch-tag-selector")
|
|
|
|
assert.EqualValues(t, indexer, len(branchDropdown.Nodes) == 0)
|
2024-04-11 15:42:59 +00:00
|
|
|
|
2024-08-16 13:45:54 +00:00
|
|
|
dropdownOptions := container.
|
|
|
|
Find(".menu[data-test-tag=fuzzy-dropdown]").
|
|
|
|
Find("input[type=radio][name=mode]").
|
|
|
|
Map(func(_ int, sel *goquery.Selection) string {
|
|
|
|
attr, exists := sel.Attr("value")
|
|
|
|
assert.True(t, exists)
|
|
|
|
return attr
|
|
|
|
})
|
2024-07-10 05:25:32 +00:00
|
|
|
|
2024-08-16 13:45:54 +00:00
|
|
|
if indexer {
|
|
|
|
assert.EqualValues(t, []string{"exact", "fuzzy"}, dropdownOptions)
|
|
|
|
} else {
|
|
|
|
assert.EqualValues(t, []string{"exact", "union", "regexp"}, dropdownOptions)
|
2024-07-10 05:25:32 +00:00
|
|
|
}
|
|
|
|
|
2024-04-11 15:42:59 +00:00
|
|
|
filenames := resultFilenames(t, doc)
|
2019-09-11 17:26:28 +00:00
|
|
|
assert.EqualValues(t, expected, filenames)
|
|
|
|
}
|