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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. 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. "github.com/PuerkitoBio/goquery"
  12. "github.com/stretchr/testify/assert"
  13. )
  14. func resultFilenames(t testing.TB, 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 prepareTestEnv(t)()
  24. repo, err := repo_model.GetRepositoryByOwnerAndName("user2", "repo1")
  25. assert.NoError(t, err)
  26. executeIndexer(t, repo, code_indexer.UpdateRepoIndexer)
  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("user2", "glob")
  31. assert.NoError(t, err)
  32. executeIndexer(t, repo, code_indexer.UpdateRepoIndexer)
  33. testSearch(t, "/user2/glob/search?q=loren&page=1", []string{"a.txt"})
  34. testSearch(t, "/user2/glob/search?q=file3&page=1", []string{"x/b.txt"})
  35. testSearch(t, "/user2/glob/search?q=file4&page=1", []string{})
  36. testSearch(t, "/user2/glob/search?q=file5&page=1", []string{})
  37. }
  38. func testSearch(t *testing.T, url string, expected []string) {
  39. req := NewRequestf(t, "GET", url)
  40. resp := MakeRequest(t, req, http.StatusOK)
  41. filenames := resultFilenames(t, NewHTMLParser(t, resp.Body))
  42. assert.EqualValues(t, expected, filenames)
  43. }
  44. func executeIndexer(t *testing.T, repo *repo_model.Repository, op func(*repo_model.Repository)) {
  45. op(repo)
  46. }