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

     1  package cmd
     2  
     3  import (
     4  	"os/exec"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  // issueEditCmdTestCreateIssue creates an issue and returns the issue number
    12  func issueEditCmdTestCreateIssue(t *testing.T, dir string) string {
    13  	cmd := exec.Command(labBinaryPath, "issue", "create", "lab-testing",
    14  		"-m", "issue title", "-l", "bug")
    15  	cmd.Dir = dir
    16  
    17  	b, err := cmd.CombinedOutput()
    18  	if err != nil {
    19  		t.Fatal(err)
    20  	}
    21  
    22  	s := strings.Split(string(b), "\n")
    23  	s = strings.Split(s[0], "/")
    24  	return s[len(s)-1]
    25  }
    26  
    27  // issueEditCmdTestShowIssue returns the `lab issue show` output for the given issue
    28  func issueEditCmdTestShowIssue(t *testing.T, dir string, issueNum string) string {
    29  	cmd := exec.Command(labBinaryPath, "issue", "show", "lab-testing", issueNum)
    30  	cmd.Dir = dir
    31  
    32  	b, err := cmd.CombinedOutput()
    33  	if err != nil {
    34  		t.Fatal(err)
    35  	}
    36  
    37  	return string(b)
    38  }
    39  
    40  func Test_issueEditCmd(t *testing.T) {
    41  	repo := copyTestRepo(t)
    42  
    43  	issueNum := issueEditCmdTestCreateIssue(t, repo)
    44  
    45  	// update the issue
    46  	cmd := exec.Command(labBinaryPath, "issue", "edit", "lab-testing", issueNum,
    47  		"-m", "new title")
    48  	cmd.Dir = repo
    49  
    50  	b, err := cmd.CombinedOutput()
    51  	if err != nil {
    52  		t.Log(string(b))
    53  		t.Fatal(err)
    54  	}
    55  
    56  	// show the updated issue
    57  	issueShowOuput := issueEditCmdTestShowIssue(t, repo, issueNum)
    58  
    59  	// the output should show the updated title, not the old title
    60  	require.Contains(t, issueShowOuput, "new title")
    61  	require.NotContains(t, issueShowOuput, "issue title")
    62  }
    63  
    64  func Test_issueEditLabels(t *testing.T) {
    65  	repo := copyTestRepo(t)
    66  
    67  	issueNum := issueEditCmdTestCreateIssue(t, repo)
    68  
    69  	// update the issue
    70  	cmd := exec.Command(labBinaryPath, "issue", "edit", "lab-testing", issueNum,
    71  		"-l", "crit", "--unlabel", "bug")
    72  	cmd.Dir = repo
    73  
    74  	b, err := cmd.CombinedOutput()
    75  	if err != nil {
    76  		t.Log(string(b))
    77  		t.Fatal(err)
    78  	}
    79  
    80  	// show the updated issue
    81  	issueShowOuput := issueEditCmdTestShowIssue(t, repo, issueNum)
    82  
    83  	// the output should show the updated labels
    84  	require.Contains(t, issueShowOuput, "critical")
    85  	require.NotContains(t, issueShowOuput, "bug")
    86  }
    87  
    88  func Test_issueEditAssignees(t *testing.T) {
    89  	repo := copyTestRepo(t)
    90  
    91  	issueNum := issueEditCmdTestCreateIssue(t, repo)
    92  
    93  	// add an assignee
    94  	cmd := exec.Command(labBinaryPath, "issue", "edit", "lab-testing", issueNum,
    95  		"-a", "lab-testing")
    96  	cmd.Dir = repo
    97  
    98  	b, err := cmd.CombinedOutput()
    99  	if err != nil {
   100  		t.Log(string(b))
   101  		t.Fatal(err)
   102  	}
   103  
   104  	// get the updated issue
   105  	issueShowOuput := issueEditCmdTestShowIssue(t, repo, issueNum)
   106  
   107  	// the output should show the new assignee
   108  	require.Contains(t, issueShowOuput, "Assignees: lab-testing")
   109  
   110  	// now remove the assignee
   111  	cmd = exec.Command(labBinaryPath, "issue", "edit", "lab-testing", issueNum,
   112  		"--unassign", "lab-testing")
   113  	cmd.Dir = repo
   114  
   115  	b, err = cmd.CombinedOutput()
   116  	if err != nil {
   117  		t.Log(string(b))
   118  		t.Fatal(err)
   119  	}
   120  
   121  	// get the updated issue again
   122  	issueShowOuput = issueEditCmdTestShowIssue(t, repo, issueNum)
   123  
   124  	// the output should NOT show the assignee
   125  	require.NotContains(t, issueShowOuput, "Assignees: lab-testing")
   126  }
   127  
   128  func Test_issueNoteDelete(t *testing.T) {
   129  	repo := copyTestRepo(t)
   130  
   131  	issueNum := issueEditCmdTestCreateIssue(t, repo)
   132  
   133  	// add just a note "DELETED 1"
   134  	cmd := exec.Command(labBinaryPath, "issue", "note", "lab-testing", issueNum, "-m", "DELETED 1")
   135  	cmd.Dir = repo
   136  
   137  	weburl, err := cmd.CombinedOutput()
   138  	if err != nil {
   139  		t.Log(string(weburl))
   140  		t.Fatal(err)
   141  	}
   142  	noteIDs := strings.Split(string(weburl), "\n")
   143  	noteID := strings.Split(noteIDs[0], "#note_")[1]
   144  	deletedNote := issueNum + ":" + noteID
   145  
   146  	// add another note "REPLY 2"
   147  	cmd = exec.Command(labBinaryPath, "issue", "note", "lab-testing", issueNum, "-m", "REPLY 2")
   148  	cmd.Dir = repo
   149  
   150  	weburl, err = cmd.CombinedOutput()
   151  	if err != nil {
   152  		t.Log(string(weburl))
   153  		t.Fatal(err)
   154  	}
   155  	noteIDs = strings.Split(string(weburl), "\n")
   156  	noteID = strings.Split(noteIDs[0], "#note_")[1]
   157  	replyNote := issueNum + ":" + noteID
   158  
   159  	// reply to the "REPLY 2" comment with a note to create a discussion "DELETED 2"
   160  	cmd = exec.Command(labBinaryPath, "issue", "reply", "lab-testing", replyNote, "-m", "DELETED 2")
   161  	cmd.Dir = repo
   162  
   163  	weburl, err = cmd.CombinedOutput()
   164  	if err != nil {
   165  		t.Log(string(weburl))
   166  		t.Fatal(err)
   167  	}
   168  	noteIDs = strings.Split(string(weburl), "\n")
   169  	noteID = strings.Split(noteIDs[0], "#note_")[1]
   170  	deletedDiscussion := issueNum + ":" + noteID
   171  
   172  	// reply to the comment with a second comment "DISCUSSION 1"
   173  	cmd = exec.Command(labBinaryPath, "issue", "reply", "lab-testing", replyNote, "-m", "DISCUSSION 1")
   174  	cmd.Dir = repo
   175  
   176  	weburl, err = cmd.CombinedOutput()
   177  	if err != nil {
   178  		t.Log(string(weburl))
   179  		t.Fatal(err)
   180  	}
   181  
   182  	// delete the first note
   183  	cmd = exec.Command(labBinaryPath, "issue", "edit", "lab-testing", deletedNote, "--delete-note")
   184  	cmd.Dir = repo
   185  
   186  	weburl, err = cmd.CombinedOutput()
   187  	if err != nil {
   188  		t.Log(string(weburl))
   189  		t.Fatal(err)
   190  	}
   191  
   192  	// delete the first discussion reply
   193  	cmd = exec.Command(labBinaryPath, "issue", "edit", "lab-testing", deletedDiscussion, "--delete-note")
   194  	cmd.Dir = repo
   195  
   196  	weburl, err = cmd.CombinedOutput()
   197  	if err != nil {
   198  		t.Log(string(weburl))
   199  		t.Fatal(err)
   200  	}
   201  
   202  	// show the updated issue and comments
   203  	cmd = exec.Command(labBinaryPath, "issue", "show", "lab-testing", issueNum, "--comments")
   204  	cmd.Dir = repo
   205  
   206  	b, err := cmd.CombinedOutput()
   207  	if err != nil {
   208  		t.Fatal(err)
   209  	}
   210  	issueShowOutput := string(b)
   211  
   212  	// lab show should not contain "DELETED" notes
   213  	require.NotContains(t, issueShowOutput, "DELETED 1")
   214  	require.NotContains(t, issueShowOutput, "DELETED 2")
   215  	// lab show should contain the other notes and disucssion
   216  	require.Contains(t, issueShowOutput, "REPLY 2")
   217  	require.Contains(t, issueShowOutput, "DISCUSSION 1")
   218  }