code.gitea.io/gitea@v1.21.7/tests/integration/repo_search_test.go (about) 1 // Copyright 2017 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package integration 5 6 import ( 7 "net/http" 8 "testing" 9 10 "code.gitea.io/gitea/models/db" 11 repo_model "code.gitea.io/gitea/models/repo" 12 code_indexer "code.gitea.io/gitea/modules/indexer/code" 13 "code.gitea.io/gitea/modules/setting" 14 "code.gitea.io/gitea/tests" 15 16 "github.com/PuerkitoBio/goquery" 17 "github.com/stretchr/testify/assert" 18 ) 19 20 func resultFilenames(t testing.TB, doc *HTMLDoc) []string { 21 filenameSelections := doc.doc.Find(".repository.search").Find(".repo-search-result").Find(".header").Find("span.file") 22 result := make([]string, filenameSelections.Length()) 23 filenameSelections.Each(func(i int, selection *goquery.Selection) { 24 result[i] = selection.Text() 25 }) 26 return result 27 } 28 29 func TestSearchRepo(t *testing.T) { 30 defer tests.PrepareTestEnv(t)() 31 32 repo, err := repo_model.GetRepositoryByOwnerAndName(db.DefaultContext, "user2", "repo1") 33 assert.NoError(t, err) 34 35 executeIndexer(t, repo, code_indexer.UpdateRepoIndexer) 36 37 testSearch(t, "/user2/repo1/search?q=Description&page=1", []string{"README.md"}) 38 39 setting.Indexer.IncludePatterns = setting.IndexerGlobFromString("**.txt") 40 setting.Indexer.ExcludePatterns = setting.IndexerGlobFromString("**/y/**") 41 42 repo, err = repo_model.GetRepositoryByOwnerAndName(db.DefaultContext, "user2", "glob") 43 assert.NoError(t, err) 44 45 executeIndexer(t, repo, code_indexer.UpdateRepoIndexer) 46 47 testSearch(t, "/user2/glob/search?q=loren&page=1", []string{"a.txt"}) 48 testSearch(t, "/user2/glob/search?q=file3&page=1", []string{"x/b.txt"}) 49 testSearch(t, "/user2/glob/search?q=file4&page=1", []string{}) 50 testSearch(t, "/user2/glob/search?q=file5&page=1", []string{}) 51 } 52 53 func testSearch(t *testing.T, url string, expected []string) { 54 req := NewRequestf(t, "GET", url) 55 resp := MakeRequest(t, req, http.StatusOK) 56 57 filenames := resultFilenames(t, NewHTMLParser(t, resp.Body)) 58 assert.EqualValues(t, expected, filenames) 59 } 60 61 func executeIndexer(t *testing.T, repo *repo_model.Repository, op func(*repo_model.Repository)) { 62 op(repo) 63 }