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

     1  package cmd
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os/exec"
     6  	"path/filepath"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  // #3952 is not special, it's just a place to dump discussions as mr #1 filled up, long term should update the tests clean up what they create
    15  const mrCommentSlashDiscussionDumpsterID = "3953"
    16  
    17  func Test_mrCreateNote(t *testing.T) {
    18  	tests := []struct {
    19  		Name         string
    20  		Args         []string
    21  		ExpectedBody string
    22  	}{
    23  		{
    24  			Name:         "Normal text",
    25  			Args:         []string{"lab-testing", mrCommentSlashDiscussionDumpsterID, "-m", "note text"},
    26  			ExpectedBody: "https://gitlab.com/lab-testing/test/merge_requests/" + mrCommentSlashDiscussionDumpsterID + "#note_",
    27  		},
    28  		{
    29  			// Escaped sequence text direct in the argument list as the
    30  			// following one was already a problem:
    31  			// https://github.com/zaquestion/lab/issues/376
    32  			Name:         "Escape char",
    33  			Args:         []string{"lab-testing", mrCommentSlashDiscussionDumpsterID, "-m", "{\"key\": \"value\"}"},
    34  			ExpectedBody: "https://gitlab.com/lab-testing/test/merge_requests/" + mrCommentSlashDiscussionDumpsterID + "#note_",
    35  		},
    36  	}
    37  	noteCmd := []string{"mr", "note"}
    38  	repo := copyTestRepo(t)
    39  
    40  	for _, test := range tests {
    41  		t.Run(test.Name, func(t *testing.T) {
    42  			cmd := exec.Command(labBinaryPath, append(noteCmd, test.Args...)...)
    43  			cmd.Dir = repo
    44  
    45  			b, err := cmd.CombinedOutput()
    46  			if err != nil {
    47  				t.Log(string(b))
    48  				t.Fatal(err)
    49  			}
    50  
    51  			require.Contains(t, string(b), test.ExpectedBody)
    52  		})
    53  	}
    54  }
    55  
    56  func Test_mrCreateNote_file(t *testing.T) {
    57  	repo := copyTestRepo(t)
    58  
    59  	err := ioutil.WriteFile(filepath.Join(repo, "hellolab.txt"), []byte("hello\nlab\n"), 0644)
    60  	if err != nil {
    61  		t.Fatal(err)
    62  	}
    63  
    64  	cmd := exec.Command(labBinaryPath, "mr", "note", "lab-testing", mrCommentSlashDiscussionDumpsterID,
    65  		"-F", "hellolab.txt")
    66  	cmd.Dir = repo
    67  
    68  	b, err := cmd.CombinedOutput()
    69  	if err != nil {
    70  		t.Log(string(b))
    71  		t.Fatal(err)
    72  	}
    73  
    74  	require.Contains(t, string(b), "https://gitlab.com/lab-testing/test/merge_requests/"+mrCommentSlashDiscussionDumpsterID+"#note_")
    75  }
    76  
    77  func Test_mrReplyAndResolve(t *testing.T) {
    78  	repo := copyTestRepo(t)
    79  
    80  	cmd := exec.Command(labBinaryPath, "mr", "note", "lab-testing", mrCommentSlashDiscussionDumpsterID, "-m", "merge request text")
    81  	cmd.Dir = repo
    82  
    83  	a, err := cmd.CombinedOutput()
    84  	if err != nil {
    85  		t.Log(string(a))
    86  		t.Fatal(err)
    87  	}
    88  	_noteIDs := strings.Split(string(a), "\n")
    89  	noteIDs := strings.Split(_noteIDs[0], "#note_")
    90  	noteID := noteIDs[1]
    91  
    92  	// add reply to the noteID
    93  	reply := exec.Command(labBinaryPath, "mr", "reply", "lab-testing", mrCommentSlashDiscussionDumpsterID+":"+noteID, "-m", "reply to note")
    94  	reply.Dir = repo
    95  	c, err := reply.CombinedOutput()
    96  	if err != nil {
    97  		t.Log(string(c))
    98  		t.Fatal(err)
    99  	}
   100  	_replyIDs := strings.Split(string(c), "\n")
   101  	replyIDs := strings.Split(_replyIDs[0], "#note_")
   102  	replyID := replyIDs[1]
   103  
   104  	show := exec.Command(labBinaryPath, "mr", "show", "lab-testing", mrCommentSlashDiscussionDumpsterID, "--comments")
   105  	show.Dir = repo
   106  	d, err := show.CombinedOutput()
   107  	if err != nil {
   108  		t.Log(string(d))
   109  		t.Fatal(err)
   110  	}
   111  
   112  	resolve := exec.Command(labBinaryPath, "mr", "resolve", "lab-testing", mrCommentSlashDiscussionDumpsterID+":"+noteID)
   113  	resolve.Dir = repo
   114  	e, err := resolve.CombinedOutput()
   115  	if err != nil {
   116  		t.Log(string(e))
   117  		t.Fatal(err)
   118  	}
   119  
   120  	require.Contains(t, string(d), "#"+noteID+": "+"lab-testing started a discussion")
   121  	require.Contains(t, string(d), "#"+replyID+": "+"lab-testing commented at")
   122  	require.Contains(t, string(e), "Resolved")
   123  }
   124  
   125  func Test_mrNoteMsg(t *testing.T) {
   126  	tests := []struct {
   127  		Name         string
   128  		Msgs         []string
   129  		ExpectedBody string
   130  	}{
   131  		{
   132  			Name:         "Using messages",
   133  			Msgs:         []string{"note paragraph 1", "note paragraph 2"},
   134  			ExpectedBody: "note paragraph 1\n\nnote paragraph 2",
   135  		},
   136  		{
   137  			Name:         "From Editor",
   138  			Msgs:         nil,
   139  			ExpectedBody: "", // this is not a great test
   140  		},
   141  	}
   142  	for _, test := range tests {
   143  		t.Run(test.Name, func(t *testing.T) {
   144  			test := test
   145  			t.Parallel()
   146  			body, err := noteMsg(test.Msgs, true, 1, "OPEN", "", "\n")
   147  			if err != nil {
   148  				t.Fatal(err)
   149  			}
   150  			assert.Equal(t, test.ExpectedBody, body)
   151  		})
   152  	}
   153  }