pull_request_test.go 3.0 KB

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