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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package integrations
  5. import (
  6. "net/http"
  7. "testing"
  8. "time"
  9. "code.gitea.io/gitea/models"
  10. code_indexer "code.gitea.io/gitea/modules/indexer/code"
  11. "code.gitea.io/gitea/modules/setting"
  12. "github.com/PuerkitoBio/goquery"
  13. "github.com/stretchr/testify/assert"
  14. )
  15. func resultFilenames(t testing.TB, doc *HTMLDoc) []string {
  16. resultsSelection := doc.doc.Find(".repository.search")
  17. assert.EqualValues(t, 1, resultsSelection.Length(),
  18. "Invalid template (repo search template has changed?)")
  19. filenameSelections := resultsSelection.Find(".repo-search-result").Find(".header").Find("span.file")
  20. result := make([]string, filenameSelections.Length())
  21. filenameSelections.Each(func(i int, selection *goquery.Selection) {
  22. result[i] = selection.Text()
  23. })
  24. return result
  25. }
  26. func TestSearchRepo(t *testing.T) {
  27. defer prepareTestEnv(t)()
  28. repo, err := models.GetRepositoryByOwnerAndName("user2", "repo1")
  29. assert.NoError(t, err)
  30. executeIndexer(t, repo, code_indexer.UpdateRepoIndexer)
  31. testSearch(t, "/user2/repo1/search?q=Description&page=1", []string{"README.md"})
  32. setting.Indexer.IncludePatterns = setting.IndexerGlobFromString("**.txt")
  33. setting.Indexer.ExcludePatterns = setting.IndexerGlobFromString("**/y/**")
  34. repo, err = models.GetRepositoryByOwnerAndName("user2", "glob")
  35. assert.NoError(t, err)
  36. executeIndexer(t, repo, code_indexer.DeleteRepoFromIndexer)
  37. executeIndexer(t, repo, code_indexer.UpdateRepoIndexer)
  38. testSearch(t, "/user2/glob/search?q=loren&page=1", []string{"a.txt"})
  39. testSearch(t, "/user2/glob/search?q=file3&page=1", []string{"x/b.txt"})
  40. testSearch(t, "/user2/glob/search?q=file4&page=1", []string{})
  41. testSearch(t, "/user2/glob/search?q=file5&page=1", []string{})
  42. }
  43. func testSearch(t *testing.T, url string, expected []string) {
  44. req := NewRequestf(t, "GET", url)
  45. resp := MakeRequest(t, req, http.StatusOK)
  46. filenames := resultFilenames(t, NewHTMLParser(t, resp.Body))
  47. assert.EqualValues(t, expected, filenames)
  48. }
  49. func executeIndexer(t *testing.T, repo *models.Repository, op func(*models.Repository, ...chan<- error)) {
  50. waiter := make(chan error, 1)
  51. op(repo, waiter)
  52. select {
  53. case err := <-waiter:
  54. assert.NoError(t, err)
  55. case <-time.After(1 * time.Minute):
  56. assert.Fail(t, "Repository indexer took too long")
  57. }
  58. }