From 755cc9f8d895647d2373a7eca784f45f72f26201 Mon Sep 17 00:00:00 2001 From: Denis Palashevskii Date: Wed, 21 Apr 2021 16:43:10 +0400 Subject: [PATCH] Add tests --- pkg/commands/pull_request_test.go | 111 ++++++++++++++++++++++++++++-- 1 file changed, 104 insertions(+), 7 deletions(-) diff --git a/pkg/commands/pull_request_test.go b/pkg/commands/pull_request_test.go index eb9b173ae..0413022d6 100644 --- a/pkg/commands/pull_request_test.go +++ b/pkg/commands/pull_request_test.go @@ -48,7 +48,8 @@ func TestGetRepoInfoFromURL(t *testing.T) { func TestCreatePullRequest(t *testing.T) { type scenario struct { testName string - branch *models.Branch + from *models.Branch + to *models.Branch remoteUrl string command func(string, ...string) *exec.Cmd test func(url string, err error) @@ -57,7 +58,7 @@ func TestCreatePullRequest(t *testing.T) { scenarios := []scenario{ { testName: "Opens a link to new pull request on bitbucket", - branch: &models.Branch{ + from: &models.Branch{ Name: "feature/profile-page", }, remoteUrl: "git@bitbucket.org:johndoe/social_network.git", @@ -78,7 +79,7 @@ func TestCreatePullRequest(t *testing.T) { }, { testName: "Opens a link to new pull request on bitbucket with http remote url", - branch: &models.Branch{ + from: &models.Branch{ Name: "feature/events", }, remoteUrl: "https://my_username@bitbucket.org/johndoe/social_network.git", @@ -99,7 +100,7 @@ func TestCreatePullRequest(t *testing.T) { }, { testName: "Opens a link to new pull request on github", - branch: &models.Branch{ + from: &models.Branch{ Name: "feature/sum-operation", }, remoteUrl: "git@github.com:peter/calculator.git", @@ -118,9 +119,81 @@ func TestCreatePullRequest(t *testing.T) { assert.Equal(t, "https://github.com/peter/calculator/compare/feature/sum-operation?expand=1", url) }, }, + { + testName: "Opens a link to new pull request on bitbucket with specific target branch", + from: &models.Branch{ + Name: "feature/profile-page/avatar", + }, + to: &models.Branch{ + Name: "feature/profile-page", + }, + remoteUrl: "git@bitbucket.org:johndoe/social_network.git", + command: func(cmd string, args ...string) *exec.Cmd { + // Handle git remote url call + if strings.HasPrefix(cmd, "git") { + return secureexec.Command("echo", "git@bitbucket.org:johndoe/social_network.git") + } + + assert.Equal(t, cmd, "open") + assert.Equal(t, args, []string{"https://bitbucket.org/johndoe/social_network/pull-requests/new?source=feature/profile-page/avatar&dest=feature/profile-page&t=1"}) + return secureexec.Command("echo") + }, + test: func(url string, err error) { + assert.NoError(t, err) + assert.Equal(t, "https://bitbucket.org/johndoe/social_network/pull-requests/new?source=feature/profile-page/avatar&dest=feature/profile-page&t=1", url) + }, + }, + { + testName: "Opens a link to new pull request on bitbucket with http remote url with specified target branch", + from: &models.Branch{ + Name: "feature/remote-events", + }, + to: &models.Branch{ + Name: "feature/events", + }, + remoteUrl: "https://my_username@bitbucket.org/johndoe/social_network.git", + command: func(cmd string, args ...string) *exec.Cmd { + // Handle git remote url call + if strings.HasPrefix(cmd, "git") { + return secureexec.Command("echo", "https://my_username@bitbucket.org/johndoe/social_network.git") + } + + assert.Equal(t, cmd, "open") + assert.Equal(t, args, []string{"https://bitbucket.org/johndoe/social_network/pull-requests/new?source=feature/remote-events&dest=feature/events&t=1"}) + return secureexec.Command("echo") + }, + test: func(url string, err error) { + assert.NoError(t, err) + assert.Equal(t, "https://bitbucket.org/johndoe/social_network/pull-requests/new?source=feature/remote-events&dest=feature/events&t=1", url) + }, + }, + { + testName: "Opens a link to new pull request on github with specific target branch", + from: &models.Branch{ + Name: "feature/sum-operation", + }, + to: &models.Branch{ + Name: "feature/operations", + }, + remoteUrl: "git@github.com:peter/calculator.git", + command: func(cmd string, args ...string) *exec.Cmd { + // Handle git remote url call + if strings.HasPrefix(cmd, "git") { + return secureexec.Command("echo", "git@github.com:peter/calculator.git") + } + + assert.Equal(t, cmd, "open") + assert.Equal(t, args, []string{"https://github.com/peter/calculator/compare/feature/operations...feature/sum-operation?expand=1"}) + return secureexec.Command("echo") + }, + test: func(url string, err error) { + assert.NoError(t, err) + assert.Equal(t, "https://github.com/peter/calculator/compare/feature/operations...feature/sum-operation?expand=1", url) + }, + }, { testName: "Opens a link to new pull request on gitlab", - branch: &models.Branch{ + from: &models.Branch{ Name: "feature/ui", }, remoteUrl: "git@gitlab.com:peter/calculator.git", @@ -139,9 +212,33 @@ func TestCreatePullRequest(t *testing.T) { assert.Equal(t, "https://gitlab.com/peter/calculator/merge_requests/new?merge_request[source_branch]=feature/ui", url) }, }, + { + testName: "Opens a link to new pull request on gitlab with specific target branch", + from: &models.Branch{ + Name: "feature/commit-ui", + }, + to: &models.Branch{ + Name: "epic/ui", + }, + remoteUrl: "git@gitlab.com:peter/calculator.git", + command: func(cmd string, args ...string) *exec.Cmd { + // Handle git remote url call + if strings.HasPrefix(cmd, "git") { + return secureexec.Command("echo", "git@gitlab.com:peter/calculator.git") + } + + assert.Equal(t, cmd, "open") + assert.Equal(t, args, []string{"https://gitlab.com/peter/calculator/merge_requests/new?merge_request[source_branch]=feature/commit-ui&merge_request[target_branch]=epic/ui"}) + return secureexec.Command("echo") + }, + test: func(url string, err error) { + assert.NoError(t, err) + assert.Equal(t, "https://gitlab.com/peter/calculator/merge_requests/new?merge_request[source_branch]=feature/commit-ui&merge_request[target_branch]=epic/ui", url) + }, + }, { testName: "Throws an error if git service is unsupported", - branch: &models.Branch{ + from: &models.Branch{ Name: "feature/divide-operation", }, remoteUrl: "git@something.com:peter/calculator.git", @@ -171,7 +268,7 @@ func TestCreatePullRequest(t *testing.T) { return s.remoteUrl, nil } dummyPullRequest := NewPullRequest(gitCommand) - s.test(dummyPullRequest.Create(s.branch)) + s.test(dummyPullRequest.Create(s.from, s.to)) }) } }