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.

66 lines
2.2 KiB

  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. "code.gitea.io/gitea/models"
  9. code_indexer "code.gitea.io/gitea/modules/indexer/code"
  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. defer prepareTestEnv(t)()
  27. repo, err := models.GetRepositoryByOwnerAndName("user2", "repo1")
  28. assert.NoError(t, err)
  29. executeIndexer(t, repo, code_indexer.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, code_indexer.DeleteRepoFromIndexer)
  36. executeIndexer(t, repo, code_indexer.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)) {
  49. op(repo)
  50. }