gitea源码

repo_search_test.go 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. repo_model "code.gitea.io/gitea/models/repo"
  8. code_indexer "code.gitea.io/gitea/modules/indexer/code"
  9. "code.gitea.io/gitea/modules/setting"
  10. "code.gitea.io/gitea/tests"
  11. "github.com/PuerkitoBio/goquery"
  12. "github.com/stretchr/testify/assert"
  13. )
  14. func resultFilenames(doc *HTMLDoc) []string {
  15. filenameSelections := doc.doc.Find(".repository.search").Find(".repo-search-result").Find(".header").Find("span.file")
  16. result := make([]string, filenameSelections.Length())
  17. filenameSelections.Each(func(i int, selection *goquery.Selection) {
  18. result[i] = selection.Text()
  19. })
  20. return result
  21. }
  22. func TestSearchRepo(t *testing.T) {
  23. defer tests.PrepareTestEnv(t)()
  24. repo, err := repo_model.GetRepositoryByOwnerAndName(t.Context(), "user2", "repo1")
  25. assert.NoError(t, err)
  26. code_indexer.UpdateRepoIndexer(repo)
  27. testSearch(t, "/user2/repo1/search?q=Description&page=1", []string{"README.md"})
  28. setting.Indexer.IncludePatterns = setting.IndexerGlobFromString("**.txt")
  29. setting.Indexer.ExcludePatterns = setting.IndexerGlobFromString("**/y/**")
  30. repo, err = repo_model.GetRepositoryByOwnerAndName(t.Context(), "user2", "glob")
  31. assert.NoError(t, err)
  32. code_indexer.UpdateRepoIndexer(repo)
  33. testSearch(t, "/user2/glob/search?q=loren&page=1", []string{"a.txt"})
  34. testSearch(t, "/user2/glob/search?q=loren&page=1&t=match", []string{"a.txt"})
  35. testSearch(t, "/user2/glob/search?q=file3&page=1", []string{"x/b.txt", "a.txt"})
  36. testSearch(t, "/user2/glob/search?q=file3&page=1&t=match", []string{"x/b.txt", "a.txt"})
  37. testSearch(t, "/user2/glob/search?q=file4&page=1&t=match", []string{"x/b.txt", "a.txt"})
  38. testSearch(t, "/user2/glob/search?q=file5&page=1&t=match", []string{"x/b.txt", "a.txt"})
  39. }
  40. func testSearch(t *testing.T, url string, expected []string) {
  41. req := NewRequest(t, "GET", url)
  42. resp := MakeRequest(t, req, http.StatusOK)
  43. filenames := resultFilenames(NewHTMLParser(t, resp.Body))
  44. assert.Equal(t, expected, filenames)
  45. }