pull_request_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2020 The Gogs 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 gitutil
  5. import (
  6. "fmt"
  7. "testing"
  8. "github.com/gogs/git-module"
  9. "github.com/pkg/errors"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. func TestModuler_PullRequestMeta(t *testing.T) {
  13. headPath := "/head/path"
  14. basePath := "/base/path"
  15. headBranch := "head_branch"
  16. baseBranch := "base_branch"
  17. mergeBase := "MERGE-BASE"
  18. changedFiles := []string{"a.go", "b.txt"}
  19. commits := []*git.Commit{
  20. {ID: git.MustIDFromString("adfd6da3c0a3fb038393144becbf37f14f780087")},
  21. }
  22. SetMockModuleStore(t, &MockModuleStore{
  23. repoAddRemote: func(repoPath, name, url string, opts ...git.AddRemoteOptions) error {
  24. if repoPath != headPath {
  25. return fmt.Errorf("repoPath: want %q but got %q", headPath, repoPath)
  26. } else if name == "" {
  27. return errors.New("empty name")
  28. } else if url != basePath {
  29. return fmt.Errorf("url: want %q but got %q", basePath, url)
  30. }
  31. if len(opts) == 0 {
  32. return errors.New("no options")
  33. } else if !opts[0].Fetch {
  34. return fmt.Errorf("opts.Fetch: want %v but got %v", true, opts[0].Fetch)
  35. }
  36. return nil
  37. },
  38. repoMergeBase: func(repoPath, base, head string, opts ...git.MergeBaseOptions) (string, error) {
  39. if repoPath != headPath {
  40. return "", fmt.Errorf("repoPath: want %q but got %q", headPath, repoPath)
  41. } else if base == "" {
  42. return "", errors.New("empty base")
  43. } else if head != headBranch {
  44. return "", fmt.Errorf("head: want %q but got %q", headBranch, head)
  45. }
  46. return mergeBase, nil
  47. },
  48. repoLog: func(repoPath, rev string, opts ...git.LogOptions) ([]*git.Commit, error) {
  49. if repoPath != headPath {
  50. return nil, fmt.Errorf("repoPath: want %q but got %q", headPath, repoPath)
  51. }
  52. expRev := mergeBase + "..." + headBranch
  53. if rev != expRev {
  54. return nil, fmt.Errorf("rev: want %q but got %q", expRev, rev)
  55. }
  56. return commits, nil
  57. },
  58. repoDiffNameOnly: func(repoPath, base, head string, opts ...git.DiffNameOnlyOptions) ([]string, error) {
  59. if repoPath != headPath {
  60. return nil, fmt.Errorf("repoPath: want %q but got %q", headPath, repoPath)
  61. } else if base == "" {
  62. return nil, errors.New("empty base")
  63. } else if head != headBranch {
  64. return nil, fmt.Errorf("head: want %q but got %q", headBranch, head)
  65. }
  66. if len(opts) == 0 {
  67. return nil, errors.New("no options")
  68. } else if !opts[0].NeedsMergeBase {
  69. return nil, fmt.Errorf("opts.NeedsMergeBase: want %v but got %v", true, opts[0].NeedsMergeBase)
  70. }
  71. return changedFiles, nil
  72. },
  73. repoRemoveRemote: func(repoPath, name string, opts ...git.RemoveRemoteOptions) error {
  74. if repoPath != headPath {
  75. return fmt.Errorf("repoPath: want %q but got %q", headPath, repoPath)
  76. } else if name == "" {
  77. return errors.New("empty name")
  78. }
  79. return nil
  80. },
  81. pullRequestMeta: Module.PullRequestMeta,
  82. })
  83. meta, err := Module.PullRequestMeta(headPath, basePath, headBranch, baseBranch)
  84. if err != nil {
  85. t.Fatal(err)
  86. }
  87. expMeta := &PullRequestMeta{
  88. MergeBase: mergeBase,
  89. Commits: commits,
  90. NumFiles: 2,
  91. }
  92. assert.Equal(t, expMeta, meta)
  93. }