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.

grep_test.go 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright 2024 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package git
  4. import (
  5. "context"
  6. "path/filepath"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestGrepSearch(t *testing.T) {
  11. repo, err := openRepositoryWithDefaultContext(filepath.Join(testReposDir, "language_stats_repo"))
  12. assert.NoError(t, err)
  13. defer repo.Close()
  14. res, err := GrepSearch(context.Background(), repo, "void", GrepOptions{})
  15. assert.NoError(t, err)
  16. assert.Equal(t, []*GrepResult{
  17. {
  18. Filename: "java-hello/main.java",
  19. LineNumbers: []int{3},
  20. LineCodes: []string{" public static void main(String[] args)"},
  21. },
  22. {
  23. Filename: "main.vendor.java",
  24. LineNumbers: []int{3},
  25. LineCodes: []string{" public static void main(String[] args)"},
  26. },
  27. }, res)
  28. res, err = GrepSearch(context.Background(), repo, "void", GrepOptions{PathspecList: []string{":(glob)java-hello/*"}})
  29. assert.NoError(t, err)
  30. assert.Equal(t, []*GrepResult{
  31. {
  32. Filename: "java-hello/main.java",
  33. LineNumbers: []int{3},
  34. LineCodes: []string{" public static void main(String[] args)"},
  35. },
  36. }, res)
  37. res, err = GrepSearch(context.Background(), repo, "void", GrepOptions{PathspecList: []string{":(glob,exclude)java-hello/*"}})
  38. assert.NoError(t, err)
  39. assert.Equal(t, []*GrepResult{
  40. {
  41. Filename: "main.vendor.java",
  42. LineNumbers: []int{3},
  43. LineCodes: []string{" public static void main(String[] args)"},
  44. },
  45. }, res)
  46. res, err = GrepSearch(context.Background(), repo, "void", GrepOptions{MaxResultLimit: 1})
  47. assert.NoError(t, err)
  48. assert.Equal(t, []*GrepResult{
  49. {
  50. Filename: "java-hello/main.java",
  51. LineNumbers: []int{3},
  52. LineCodes: []string{" public static void main(String[] args)"},
  53. },
  54. }, res)
  55. res, err = GrepSearch(context.Background(), repo, "void", GrepOptions{MaxResultLimit: 1, MaxLineLength: 39})
  56. assert.NoError(t, err)
  57. assert.Equal(t, []*GrepResult{
  58. {
  59. Filename: "java-hello/main.java",
  60. LineNumbers: []int{3},
  61. LineCodes: []string{" public static void main(String[] arg"},
  62. },
  63. }, res)
  64. res, err = GrepSearch(context.Background(), repo, "no-such-content", GrepOptions{})
  65. assert.NoError(t, err)
  66. assert.Len(t, res, 0)
  67. res, err = GrepSearch(context.Background(), &Repository{Path: "no-such-git-repo"}, "no-such-content", GrepOptions{})
  68. assert.Error(t, err)
  69. assert.Len(t, res, 0)
  70. }