github.com/matthewdale/lab@v0.14.0/cmd/fork_test.go (about)

     1  package cmd
     2  
     3  import (
     4  	"os"
     5  	"os/exec"
     6  	"path"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/pkg/errors"
    11  	"github.com/stretchr/testify/require"
    12  	lab "github.com/zaquestion/lab/internal/gitlab"
    13  )
    14  
    15  func Test_fork(t *testing.T) {
    16  	t.Parallel()
    17  
    18  	repo := copyTestRepo(t)
    19  
    20  	// remove the .git/config so no remotes exist
    21  	os.Remove(path.Join(repo, ".git/config"))
    22  
    23  	cmd := exec.Command("git", "remote", "add", "origin",
    24  		"git@gitlab.com:zaquestion/fork_test")
    25  	cmd.Dir = repo
    26  	err := cmd.Run()
    27  	if err != nil {
    28  		t.Fatal(err)
    29  	}
    30  
    31  	t.Run("do_fork", func(t *testing.T) {
    32  		cmd = exec.Command("../lab_bin", "fork")
    33  		cmd.Dir = repo
    34  		b, err := cmd.CombinedOutput()
    35  		if err != nil {
    36  			t.Fatal(err)
    37  		}
    38  
    39  		out := string(b)
    40  
    41  		require.Contains(t, out, "From gitlab.com:lab-testing/fork_test")
    42  		require.Contains(t, out, "new remote: lab-testing")
    43  
    44  		cmd = exec.Command("git", "remote", "-v")
    45  		cmd.Dir = repo
    46  
    47  		b, err = cmd.CombinedOutput()
    48  		if err != nil {
    49  			t.Fatal(err)
    50  		}
    51  		require.Contains(t, string(b), "lab-testing")
    52  	})
    53  	time.Sleep(2 * time.Second)
    54  
    55  	// Failing to find the project will fail the test and is a legit
    56  	// failure case since its the only thing asserting the project exists
    57  	// (was forked)
    58  	p, err := lab.FindProject("fork_test")
    59  	if err != nil {
    60  		t.Fatal(errors.Wrap(err, "failed to find project for cleanup"))
    61  	}
    62  	err = lab.ProjectDelete(p.ID)
    63  	if err != nil {
    64  		t.Fatal(errors.Wrap(err, "failed to delete project during cleanup"))
    65  	}
    66  }
    67  
    68  func Test_determineForkRemote(t *testing.T) {
    69  	tests := []struct {
    70  		desc     string
    71  		project  string
    72  		expected string
    73  	}{
    74  		{"project is forked from repo", "zaquestion", "lab-testing"},
    75  		{"project is user", "lab-testing", "upstream"},
    76  	}
    77  
    78  	for _, test := range tests {
    79  		test := test
    80  		t.Run(test.desc, func(t *testing.T) {
    81  			require.Equal(t, test.expected, determineForkRemote(test.project))
    82  		})
    83  	}
    84  }