1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2024-12-23 12:59:11 -05:00
forgejo/tests/integration/repo_search_test.go
6543 ab5f0b7558
Determine fuzziness of bleve indexer by keyword length (#29706)
also bleve did match on fuzzy search and the other way around. this also fix that bug.

(cherry picked from commit b9c57fb78e8e0d80d786d8e1da433b6c7ebf2f1c)

Conflicts:
	tests/integration/repo_search_test.go
	simple conflict resolution in the tests
2024-03-26 19:04:27 +01:00

92 lines
2.9 KiB
Go

// Copyright 2017 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package integration
import (
"net/http"
"testing"
"code.gitea.io/gitea/models/db"
repo_model "code.gitea.io/gitea/models/repo"
code_indexer "code.gitea.io/gitea/modules/indexer/code"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/test"
"code.gitea.io/gitea/tests"
"github.com/PuerkitoBio/goquery"
"github.com/stretchr/testify/assert"
)
func resultFilenames(t testing.TB, doc *goquery.Selection) []string {
filenameSelections := doc.Find(".header").Find("span.file")
result := make([]string, filenameSelections.Length())
filenameSelections.Each(func(i int, selection *goquery.Selection) {
result[i] = selection.Text()
})
return result
}
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) {
defer tests.PrepareTestEnv(t)()
defer test.MockVariableValue(&setting.Indexer.RepoIndexerEnabled, useExternalIndexer)()
repo, err := repo_model.GetRepositoryByOwnerAndName(db.DefaultContext, "user2", "repo1")
assert.NoError(t, err)
gitReference := "/branch/" + repo.DefaultBranch
if useExternalIndexer {
gitReference = "/commit/"
code_indexer.UpdateRepoIndexer(repo)
}
testSearch(t, "/user2/repo1/search?q=Description&page=1", gitReference, []string{"README.md"})
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)
code_indexer.UpdateRepoIndexer(repo)
testSearch(t, "/user2/glob/search?q=loren&page=1", gitReference, []string{"a.txt"})
testSearch(t, "/user2/glob/search?q=loren&page=1&t=match", gitReference, []string{"a.txt"})
testSearch(t, "/user2/glob/search?q=file3&page=1", gitReference, []string{"x/b.txt", "a.txt"})
testSearch(t, "/user2/glob/search?q=file3&page=1&t=match", gitReference, []string{"x/b.txt", "a.txt"})
testSearch(t, "/user2/glob/search?q=file4&page=1&t=match", gitReference, []string{"x/b.txt", "a.txt"})
testSearch(t, "/user2/glob/search?q=file5&page=1&t=match", gitReference, []string{"x/b.txt", "a.txt"})
}
}
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) {
req := NewRequest(t, "GET", url)
resp := MakeRequest(t, req, http.StatusOK)
doc := NewHTMLParser(t, resp.Body).doc.
Find(".repository.search").
Find(".repo-search-result")
filenames := resultFilenames(t, doc)
assert.EqualValues(t, expected, filenames)
checkResultLinks(t, gitRef, doc)
}