Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

repo_search_test.go 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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.gitea.io/gitea/modules/setting"
  11. "github.com/PuerkitoBio/goquery"
  12. "github.com/stretchr/testify/assert"
  13. )
  14. func resultFilenames(t testing.TB, doc *HTMLDoc) []string {
  15. resultsSelection := doc.doc.Find(".repository.search")
  16. assert.EqualValues(t, 1, resultsSelection.Length(),
  17. "Invalid template (repo search template has changed?)")
  18. filenameSelections := resultsSelection.Find(".repo-search-result").Find(".header").Find("span.file")
  19. result := make([]string, filenameSelections.Length())
  20. filenameSelections.Each(func(i int, selection *goquery.Selection) {
  21. result[i] = selection.Text()
  22. })
  23. return result
  24. }
  25. func TestSearchRepo(t *testing.T) {
  26. prepareTestEnv(t)
  27. repo, err := models.GetRepositoryByOwnerAndName("user2", "repo1")
  28. assert.NoError(t, err)
  29. executeIndexer(t, repo, models.UpdateRepoIndexer)
  30. testSearch(t, "/user2/repo1/search?q=Description&page=1", []string{"README.md"})
  31. setting.Indexer.IncludePatterns = setting.IndexerGlobFromString("**.txt")
  32. setting.Indexer.ExcludePatterns = setting.IndexerGlobFromString("**/y/**")
  33. repo, err = models.GetRepositoryByOwnerAndName("user2", "glob")
  34. assert.NoError(t, err)
  35. executeIndexer(t, repo, models.DeleteRepoFromIndexer)
  36. executeIndexer(t, repo, models.UpdateRepoIndexer)
  37. testSearch(t, "/user2/glob/search?q=loren&page=1", []string{"a.txt"})
  38. testSearch(t, "/user2/glob/search?q=file3&page=1", []string{"x/b.txt"})
  39. testSearch(t, "/user2/glob/search?q=file4&page=1", []string{})
  40. testSearch(t, "/user2/glob/search?q=file5&page=1", []string{})
  41. }
  42. func testSearch(t *testing.T, url string, expected []string) {
  43. req := NewRequestf(t, "GET", url)
  44. resp := MakeRequest(t, req, http.StatusOK)
  45. filenames := resultFilenames(t, NewHTMLParser(t, resp.Body))
  46. assert.EqualValues(t, expected, filenames)
  47. }
  48. func executeIndexer(t *testing.T, repo *models.Repository, op func(*models.Repository, ...chan<- error)) {
  49. waiter := make(chan error, 1)
  50. op(repo, waiter)
  51. select {
  52. case err := <-waiter:
  53. assert.NoError(t, err)
  54. case <-time.After(1 * time.Minute):
  55. assert.Fail(t, "Repository indexer took too long")
  56. }
  57. }