github.com/zaquestion/lab@v0.25.1/cmd/mr_checkout_test.go (about)

     1  package cmd
     2  
     3  import (
     4  	"os/exec"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func Test_mrCheckoutCmdRun(t *testing.T) {
    11  	repo := copyTestRepo(t)
    12  
    13  	// make sure the branch does not exist
    14  	cmd := exec.Command("git", "branch", "-D", "mrtest")
    15  	cmd.Dir = repo
    16  	cmd.CombinedOutput()
    17  
    18  	cmd = exec.Command(labBinaryPath, "mr", "checkout", "1")
    19  	cmd.Dir = repo
    20  	b, err := cmd.CombinedOutput()
    21  	if err != nil {
    22  		t.Log(string(b))
    23  		t.Fatal(err)
    24  	}
    25  	t.Log(string(b))
    26  
    27  	cmd = exec.Command("git", "branch")
    28  	cmd.Dir = repo
    29  
    30  	branch, err := cmd.CombinedOutput()
    31  	if err != nil {
    32  		t.Fatal(err)
    33  	}
    34  	require.Contains(t, string(branch), "mrtest")
    35  
    36  	cmd = exec.Command("git", "log", "-n1")
    37  	cmd.Dir = repo
    38  	log, err := cmd.CombinedOutput()
    39  	if err != nil {
    40  		t.Fatal(err)
    41  	}
    42  	eLog := string(log)
    43  	require.Contains(t, eLog, "Test file for MR test")
    44  	require.Contains(t, eLog, "54fd49a2ac60aeeef5ddc75efecd49f85f7ba9b0")
    45  }
    46  
    47  func Test_mrCheckoutCmd_track(t *testing.T) {
    48  	repo := copyTestRepo(t)
    49  
    50  	// make sure the branch does not exist
    51  	cmd := exec.Command("git", "branch", "-D", "mrtest")
    52  	cmd.Dir = repo
    53  	cmd.CombinedOutput()
    54  
    55  	cmd = exec.Command(labBinaryPath, "mr", "checkout", "1", "-f", "-t", "-b", "mrtest_track")
    56  	cmd.Dir = repo
    57  	b, err := cmd.CombinedOutput()
    58  	if err != nil {
    59  		t.Log(string(b))
    60  		t.Fatal(err)
    61  	}
    62  	t.Log(string(b))
    63  
    64  	cmd = exec.Command("git", "branch")
    65  	cmd.Dir = repo
    66  
    67  	branch, err := cmd.CombinedOutput()
    68  	if err != nil {
    69  		t.Fatal(err)
    70  	}
    71  	require.Contains(t, string(branch), "mrtest")
    72  
    73  	cmd = exec.Command("git", "log", "-n1")
    74  	cmd.Dir = repo
    75  	log, err := cmd.CombinedOutput()
    76  	if err != nil {
    77  		t.Fatal(err)
    78  	}
    79  	eLog := string(log)
    80  	require.Contains(t, eLog, "Test file for MR test")
    81  	require.Contains(t, eLog, "54fd49a2ac60aeeef5ddc75efecd49f85f7ba9b0")
    82  
    83  	cmd = exec.Command("git", "remote", "-v")
    84  	cmd.Dir = repo
    85  	gitOut, err := cmd.CombinedOutput()
    86  	if err != nil {
    87  		t.Fatal(err)
    88  	}
    89  	remotes := string(gitOut)
    90  	require.Contains(t, remotes, "origin	git@gitlab.com:zaquestion/test.git")
    91  }
    92  
    93  func Test_mrCheckoutCmdRunWithDifferentName(t *testing.T) {
    94  	repo := copyTestRepo(t)
    95  
    96  	// make sure the branch does not exist
    97  	cmd := exec.Command("git", "branch", "-D", "mrtest")
    98  	cmd.Dir = repo
    99  	cmd.CombinedOutput()
   100  
   101  	cmd = exec.Command(labBinaryPath, "mr", "checkout", "1", "-b", "mrtest_custom_name")
   102  	cmd.Dir = repo
   103  	b, err := cmd.CombinedOutput()
   104  	if err != nil {
   105  		t.Log(string(b))
   106  		t.Fatal(err)
   107  	}
   108  	t.Log(string(b))
   109  
   110  	cmd = exec.Command("git", "branch")
   111  	cmd.Dir = repo
   112  
   113  	branch, err := cmd.CombinedOutput()
   114  	if err != nil {
   115  		t.Fatal(err)
   116  	}
   117  	require.Contains(t, string(branch), "mrtest_custom_name")
   118  
   119  	cmd = exec.Command("git", "log", "-n1")
   120  	cmd.Dir = repo
   121  	log, err := cmd.CombinedOutput()
   122  	if err != nil {
   123  		t.Fatal(err)
   124  	}
   125  	eLog := string(log)
   126  	require.Contains(t, eLog, "Test file for MR test")
   127  	require.Contains(t, eLog, "54fd49a2ac60aeeef5ddc75efecd49f85f7ba9b0")
   128  }
   129  
   130  func Test_mrDoubleCheckoutFailCmdRun(t *testing.T) {
   131  	repo := copyTestRepo(t)
   132  
   133  	// make sure the branch does not exist
   134  	cmd := exec.Command("git", "branch", "-D", "mrtest")
   135  	cmd.Dir = repo
   136  	cmd.CombinedOutput()
   137  
   138  	first := exec.Command(labBinaryPath, "mr", "checkout", "1")
   139  	first.Dir = repo
   140  	b, err := first.CombinedOutput()
   141  	if err != nil {
   142  		t.Log(string(b))
   143  		t.Fatal(err)
   144  	}
   145  
   146  	second := exec.Command(labBinaryPath, "mr", "checkout", "1")
   147  	second.Dir = repo
   148  	log, err := second.CombinedOutput()
   149  	eLog := string(log)
   150  	if err == nil {
   151  		t.Log(eLog)
   152  		t.Fatal(err)
   153  	}
   154  	require.Contains(t, eLog, "branch mrtest already exists")
   155  }
   156  
   157  func Test_mrDoubleCheckoutForceRun(t *testing.T) {
   158  	repo := copyTestRepo(t)
   159  
   160  	// make sure the branch does not exist
   161  	cmd := exec.Command("git", "branch", "-D", "mrtest")
   162  	cmd.Dir = repo
   163  	cmd.CombinedOutput()
   164  
   165  	first := exec.Command(labBinaryPath, "mr", "checkout", "1")
   166  	first.Dir = repo
   167  	b, err := first.CombinedOutput()
   168  	if err != nil {
   169  		t.Log(string(b))
   170  		t.Fatal(err)
   171  	}
   172  
   173  	changeBranch := exec.Command("git", "checkout", "master")
   174  	changeBranch.Dir = repo
   175  	c, err := changeBranch.CombinedOutput()
   176  	if err != nil {
   177  		t.Log(string(c))
   178  		t.Fatal(err)
   179  	}
   180  
   181  	second := exec.Command(labBinaryPath, "mr", "checkout", "1", "--force")
   182  	second.Dir = repo
   183  	log, err := second.CombinedOutput()
   184  	eLog := string(log)
   185  	if err != nil {
   186  		t.Log(eLog)
   187  		t.Fatal(err)
   188  	}
   189  	require.Contains(t, eLog, "Deleted branch mrtest")
   190  }