/pkg/commands/pull_request_test.go

https://github.com/jesseduffield/lazygit · Go · 161 lines · 141 code · 12 blank · 8 comment · 6 complexity · fb8c97c1cfdb25e8872923a8ea9be9e8 MD5 · raw file

  1. package commands
  2. import (
  3. "os/exec"
  4. "strings"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. // TestGetRepoInfoFromURL is a function.
  9. func TestGetRepoInfoFromURL(t *testing.T) {
  10. type scenario struct {
  11. testName string
  12. repoURL string
  13. test func(*RepoInformation)
  14. }
  15. scenarios := []scenario{
  16. {
  17. "Returns repository information for git remote url",
  18. "git@github.com:petersmith/super_calculator",
  19. func(repoInfo *RepoInformation) {
  20. assert.EqualValues(t, repoInfo.Owner, "petersmith")
  21. assert.EqualValues(t, repoInfo.Repository, "super_calculator")
  22. },
  23. },
  24. {
  25. "Returns repository information for http remote url",
  26. "https://my_username@bitbucket.org/johndoe/social_network.git",
  27. func(repoInfo *RepoInformation) {
  28. assert.EqualValues(t, repoInfo.Owner, "johndoe")
  29. assert.EqualValues(t, repoInfo.Repository, "social_network")
  30. },
  31. },
  32. }
  33. for _, s := range scenarios {
  34. t.Run(s.testName, func(t *testing.T) {
  35. s.test(getRepoInfoFromURL(s.repoURL))
  36. })
  37. }
  38. }
  39. // TestCreatePullRequest is a function.
  40. func TestCreatePullRequest(t *testing.T) {
  41. type scenario struct {
  42. testName string
  43. branch *Branch
  44. command func(string, ...string) *exec.Cmd
  45. test func(err error)
  46. }
  47. scenarios := []scenario{
  48. {
  49. "Opens a link to new pull request on bitbucket",
  50. &Branch{
  51. Name: "feature/profile-page",
  52. },
  53. func(cmd string, args ...string) *exec.Cmd {
  54. // Handle git remote url call
  55. if strings.HasPrefix(cmd, "git") {
  56. return exec.Command("echo", "git@bitbucket.org:johndoe/social_network.git")
  57. }
  58. assert.Equal(t, cmd, "open")
  59. assert.Equal(t, args, []string{"https://bitbucket.org/johndoe/social_network/pull-requests/new?source=feature/profile-page&t=1"})
  60. return exec.Command("echo")
  61. },
  62. func(err error) {
  63. assert.NoError(t, err)
  64. },
  65. },
  66. {
  67. "Opens a link to new pull request on bitbucket with http remote url",
  68. &Branch{
  69. Name: "feature/events",
  70. },
  71. func(cmd string, args ...string) *exec.Cmd {
  72. // Handle git remote url call
  73. if strings.HasPrefix(cmd, "git") {
  74. return exec.Command("echo", "https://my_username@bitbucket.org/johndoe/social_network.git")
  75. }
  76. assert.Equal(t, cmd, "open")
  77. assert.Equal(t, args, []string{"https://bitbucket.org/johndoe/social_network/pull-requests/new?source=feature/events&t=1"})
  78. return exec.Command("echo")
  79. },
  80. func(err error) {
  81. assert.NoError(t, err)
  82. },
  83. },
  84. {
  85. "Opens a link to new pull request on github",
  86. &Branch{
  87. Name: "feature/sum-operation",
  88. },
  89. func(cmd string, args ...string) *exec.Cmd {
  90. // Handle git remote url call
  91. if strings.HasPrefix(cmd, "git") {
  92. return exec.Command("echo", "git@github.com:peter/calculator.git")
  93. }
  94. assert.Equal(t, cmd, "open")
  95. assert.Equal(t, args, []string{"https://github.com/peter/calculator/compare/feature/sum-operation?expand=1"})
  96. return exec.Command("echo")
  97. },
  98. func(err error) {
  99. assert.NoError(t, err)
  100. },
  101. },
  102. {
  103. "Opens a link to new pull request on gitlab",
  104. &Branch{
  105. Name: "feature/ui",
  106. },
  107. func(cmd string, args ...string) *exec.Cmd {
  108. // Handle git remote url call
  109. if strings.HasPrefix(cmd, "git") {
  110. return exec.Command("echo", "git@gitlab.com:peter/calculator.git")
  111. }
  112. assert.Equal(t, cmd, "open")
  113. assert.Equal(t, args, []string{"https://gitlab.com/peter/calculator/merge_requests/new?merge_request[source_branch]=feature/ui"})
  114. return exec.Command("echo")
  115. },
  116. func(err error) {
  117. assert.NoError(t, err)
  118. },
  119. },
  120. {
  121. "Throws an error if git service is unsupported",
  122. &Branch{
  123. Name: "feature/divide-operation",
  124. },
  125. func(cmd string, args ...string) *exec.Cmd {
  126. return exec.Command("echo", "git@something.com:peter/calculator.git")
  127. },
  128. func(err error) {
  129. assert.Error(t, err)
  130. },
  131. },
  132. }
  133. for _, s := range scenarios {
  134. t.Run(s.testName, func(t *testing.T) {
  135. gitCommand := NewDummyGitCommand()
  136. gitCommand.OSCommand.command = s.command
  137. gitCommand.OSCommand.Config.GetUserConfig().Set("os.openLinkCommand", "open {{link}}")
  138. gitCommand.Config.GetUserConfig().Set("services", map[string]string{
  139. // valid configuration for a custom service URL
  140. "git.work.com": "gitlab:code.work.com",
  141. // invalid configurations for a custom service URL
  142. "invalid.work.com": "noservice:invalid.work.com",
  143. "noservice.work.com": "noservice.work.com",
  144. })
  145. dummyPullRequest := NewPullRequest(gitCommand)
  146. s.test(dummyPullRequest.Create(s.branch))
  147. })
  148. }
  149. }