pull_request_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. MockModule.RepoAddRemote = func(repoPath, name, url string, opts ...git.AddRemoteOptions) error {
  23. if repoPath != headPath {
  24. return fmt.Errorf("repoPath: want %q but got %q", headPath, repoPath)
  25. } else if name == "" {
  26. return errors.New("empty name")
  27. } else if url != basePath {
  28. return fmt.Errorf("url: want %q but got %q", basePath, url)
  29. }
  30. if len(opts) == 0 {
  31. return errors.New("no options")
  32. } else if !opts[0].Fetch {
  33. return fmt.Errorf("opts.Fetch: want %v but got %v", true, opts[0].Fetch)
  34. }
  35. return nil
  36. }
  37. MockModule.RepoMergeBase = func(repoPath, base, head string, opts ...git.MergeBaseOptions) (string, error) {
  38. if repoPath != headPath {
  39. return "", fmt.Errorf("repoPath: want %q but got %q", headPath, repoPath)
  40. } else if base == "" {
  41. return "", errors.New("empty base")
  42. } else if head != headBranch {
  43. return "", fmt.Errorf("head: want %q but got %q", headBranch, head)
  44. }
  45. return mergeBase, nil
  46. }
  47. MockModule.RepoLog = func(repoPath, rev string, opts ...git.LogOptions) ([]*git.Commit, error) {
  48. if repoPath != headPath {
  49. return nil, fmt.Errorf("repoPath: want %q but got %q", headPath, repoPath)
  50. }
  51. expRev := mergeBase + "..." + headBranch
  52. if rev != expRev {
  53. return nil, fmt.Errorf("rev: want %q but got %q", expRev, rev)
  54. }
  55. return commits, nil
  56. }
  57. MockModule.RepoDiffNameOnly = func(repoPath, base, head string, opts ...git.DiffNameOnlyOptions) ([]string, error) {
  58. if repoPath != headPath {
  59. return nil, fmt.Errorf("repoPath: want %q but got %q", headPath, repoPath)
  60. } else if base == "" {
  61. return nil, errors.New("empty base")
  62. } else if head != headBranch {
  63. return nil, fmt.Errorf("head: want %q but got %q", headBranch, head)
  64. }
  65. if len(opts) == 0 {
  66. return nil, errors.New("no options")
  67. } else if !opts[0].NeedsMergeBase {
  68. return nil, fmt.Errorf("opts.NeedsMergeBase: want %v but got %v", true, opts[0].NeedsMergeBase)
  69. }
  70. return changedFiles, nil
  71. }
  72. MockModule.RepoRemoveRemote = func(repoPath, name string, opts ...git.RemoveRemoteOptions) error {
  73. if repoPath != headPath {
  74. return fmt.Errorf("repoPath: want %q but got %q", headPath, repoPath)
  75. } else if name == "" {
  76. return errors.New("empty name")
  77. }
  78. return nil
  79. }
  80. defer func() {
  81. MockModule = MockModuleStore{}
  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. }