pull_request_test.go 3.0 KB

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