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

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"os/exec"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/acarl005/stripansi"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func Test_issueCmd(t *testing.T) {
    14  	var issueID string
    15  	t.Run("create", func(t *testing.T) {
    16  		repo := copyTestRepo(t)
    17  		cmd := exec.Command(labBinaryPath, "issue", "create", "lab-testing",
    18  			"-m", "issue title",
    19  			"-m", "issue description",
    20  			"-l", "bug",
    21  			"-l", "critical",
    22  			"-a", "lab-testing")
    23  		cmd.Dir = repo
    24  
    25  		b, err := cmd.CombinedOutput()
    26  		if err != nil {
    27  			t.Log(string(b))
    28  			t.Fatal(err)
    29  		}
    30  
    31  		out := string(b)
    32  		require.Contains(t, out, "https://gitlab.com/lab-testing/test/-/issues/")
    33  
    34  		i := strings.Index(out, "\n")
    35  		issueID = strings.TrimPrefix(out[:i], "https://gitlab.com/lab-testing/test/-/issues/")
    36  		t.Log(issueID)
    37  	})
    38  	t.Run("show", func(t *testing.T) {
    39  		if issueID == "" {
    40  			t.Skip("issueID is empty, create likely failed")
    41  		}
    42  		repo := copyTestRepo(t)
    43  		cmd := exec.Command(labBinaryPath, "issue", "show", "lab-testing", issueID)
    44  		cmd.Dir = repo
    45  
    46  		b, err := cmd.CombinedOutput()
    47  		if err != nil {
    48  			t.Log(string(b))
    49  			t.Fatal(err)
    50  		}
    51  		out := string(b)
    52  		outStripped := stripansi.Strip(out) // This is required because glamour adds a lot of ansi chars
    53  		require.Contains(t, out, "Project: lab-testing/test\n")
    54  		require.Contains(t, out, "Status: Open\n")
    55  		require.Contains(t, out, "Assignees: lab-testing\n")
    56  		require.Contains(t, out, fmt.Sprintf("#%s issue title", issueID))
    57  		require.Contains(t, out, "===================================\n")
    58  		require.Contains(t, outStripped, "issue description")
    59  		require.Contains(t, out, "Labels: bug, critical\n")
    60  		require.Contains(t, out, fmt.Sprintf("WebURL: https://gitlab.com/lab-testing/test/-/issues/%s", issueID))
    61  	})
    62  	t.Run("close", func(t *testing.T) {
    63  		if issueID == "" {
    64  			t.Skip("issueID is empty, create likely failed")
    65  		}
    66  		repo := copyTestRepo(t)
    67  		cmd := exec.Command(labBinaryPath, "issue", "lab-testing", "-d", issueID)
    68  		cmd.Dir = repo
    69  
    70  		b, err := cmd.CombinedOutput()
    71  		if err != nil {
    72  			t.Log(string(b))
    73  			t.Fatal(err)
    74  		}
    75  		require.Contains(t, string(b), fmt.Sprintf("Issue #%s closed", issueID))
    76  	})
    77  }
    78  
    79  func Test_issueCmdDuplicate(t *testing.T) {
    80  	var issueID string
    81  	t.Run("create", func(t *testing.T) {
    82  		repo := copyTestRepo(t)
    83  		cmd := exec.Command(labBinaryPath, "issue", "create", "lab-testing",
    84  			"-m", "issue title",
    85  			"-m", "issue description",
    86  			"-l", "bug",
    87  			"-l", "critical",
    88  			"-a", "lab-testing")
    89  		cmd.Dir = repo
    90  
    91  		b, err := cmd.CombinedOutput()
    92  		if err != nil {
    93  			t.Log(string(b))
    94  			t.Fatal(err)
    95  		}
    96  
    97  		out := string(b)
    98  		require.Contains(t, out, "https://gitlab.com/lab-testing/test/-/issues/")
    99  
   100  		i := strings.Index(out, "\n")
   101  		issueID = strings.TrimPrefix(out[:i], "https://gitlab.com/lab-testing/test/-/issues/")
   102  		t.Log(issueID)
   103  	})
   104  	t.Run("close", func(t *testing.T) {
   105  		if issueID == "" {
   106  			t.Skip("issueID is empty, create likely failed")
   107  		}
   108  		repo := copyTestRepo(t)
   109  		cmd := exec.Command(labBinaryPath, "issue", "close", "lab-testing", "--duplicate", "1", issueID)
   110  		cmd.Dir = repo
   111  
   112  		b, err := cmd.CombinedOutput()
   113  		if err != nil {
   114  			t.Log(string(b))
   115  			t.Fatal(err)
   116  		}
   117  		require.Contains(t, string(b), fmt.Sprintf("Issue #%s closed as duplicate", issueID))
   118  	})
   119  }
   120  
   121  func Test_issueCmd_noArgs(t *testing.T) {
   122  	repo := copyTestRepo(t)
   123  	cmd := exec.Command(labBinaryPath, "issue")
   124  	cmd.Dir = repo
   125  
   126  	b, err := cmd.CombinedOutput()
   127  	if err != nil {
   128  		t.Log(string(b))
   129  		t.Fatal(err)
   130  	}
   131  	require.Contains(t, string(b), `Usage:
   132    lab issue [flags]
   133    lab issue [command]`)
   134  }