You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

repo_search_test.go 2.0KB

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