github.com/andrewrech/lazygit@v0.8.1/pkg/commands/pull_request_test.go (about)

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