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

     1  package cmd
     2  
     3  import (
     4  	"bufio"
     5  	"io/ioutil"
     6  	"os/exec"
     7  	"path/filepath"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func Test_mrCreateDiscussion(t *testing.T) {
    16  	repo := copyTestRepo(t)
    17  	cmd := exec.Command(labBinaryPath, "mr", "discussion", "lab-testing", mrCommentSlashDiscussionDumpsterID,
    18  		"-m", "discussion text")
    19  	cmd.Dir = repo
    20  
    21  	b, err := cmd.CombinedOutput()
    22  	if err != nil {
    23  		t.Log(string(b))
    24  		t.Fatal(err)
    25  	}
    26  
    27  	require.Contains(t, string(b), "https://gitlab.com/lab-testing/test/merge_requests/"+mrCommentSlashDiscussionDumpsterID+"#note_")
    28  }
    29  
    30  func Test_mrCreateDiscussion_file(t *testing.T) {
    31  	repo := copyTestRepo(t)
    32  
    33  	err := ioutil.WriteFile(filepath.Join(repo, "hellolab.txt"), []byte("hello\nlab\n"), 0644)
    34  	if err != nil {
    35  		t.Fatal(err)
    36  	}
    37  
    38  	cmd := exec.Command(labBinaryPath, "mr", "discussion", "lab-testing", mrCommentSlashDiscussionDumpsterID,
    39  		"-F", "hellolab.txt")
    40  	cmd.Dir = repo
    41  
    42  	b, err := cmd.CombinedOutput()
    43  	if err != nil {
    44  		t.Log(string(b))
    45  		t.Fatal(err)
    46  	}
    47  
    48  	require.Contains(t, string(b), "https://gitlab.com/lab-testing/test/merge_requests/"+mrCommentSlashDiscussionDumpsterID+"#note_")
    49  }
    50  
    51  func Test_mrDiscussionMsg(t *testing.T) {
    52  	tests := []struct {
    53  		Name         string
    54  		Msgs         []string
    55  		ExpectedBody string
    56  	}{
    57  		{
    58  			Name:         "Using messages",
    59  			Msgs:         []string{"discussion paragraph 1", "discussion paragraph 2"},
    60  			ExpectedBody: "discussion paragraph 1\n\ndiscussion paragraph 2",
    61  		},
    62  		{
    63  			Name:         "From Editor",
    64  			Msgs:         nil,
    65  			ExpectedBody: "", // this is not a great test
    66  		},
    67  	}
    68  	for _, test := range tests {
    69  		t.Run(test.Name, func(t *testing.T) {
    70  			test := test
    71  			t.Parallel()
    72  			body, err := mrDiscussionMsg(1, "OPEN", "", test.Msgs, "")
    73  			if err != nil {
    74  				t.Fatal(err)
    75  			}
    76  			assert.Equal(t, test.ExpectedBody, body)
    77  		})
    78  	}
    79  }
    80  
    81  func Test_mrDiscussionText(t *testing.T) {
    82  	t.Parallel()
    83  	tmpl := mrDiscussionGetTemplate("")
    84  	text, err := noteText(1701, "OPEN", "", "\n", tmpl)
    85  	if err != nil {
    86  		t.Fatal(err)
    87  	}
    88  	require.Equal(t, `
    89  
    90  # This thread is being started on OPEN Merge Request 1701.
    91  # Comment lines beginning with '#' are discarded.`, text)
    92  }
    93  
    94  // test !1159 is reserved for testing commit comments
    95  var mrDiscussionCommitMRid = "1159"
    96  
    97  func mrDiscussionShow(t *testing.T, repo string) string {
    98  	cmd := exec.Command(labBinaryPath, "mr", "show", mrDiscussionCommitMRid, "--comments", "--no-pager")
    99  	cmd.Dir = repo
   100  
   101  	a, err := cmd.CombinedOutput()
   102  	if err != nil {
   103  		t.Log(string(a))
   104  		t.Fatal(err)
   105  	}
   106  
   107  	return string(a)
   108  }
   109  
   110  func mrDiscussionCommitCommentCleanup(t *testing.T, repo string) {
   111  
   112  	showText := mrDiscussionShow(t, repo)
   113  
   114  	scanner := bufio.NewScanner(strings.NewReader(showText))
   115  	for scanner.Scan() {
   116  		line := scanner.Text()
   117  		if !strings.HasPrefix(line, "#") {
   118  			continue
   119  		}
   120  		// lab mr edit 1159:<id> --delete-note
   121  		commentid := strings.Split(line, ": ")[0][1:]
   122  		cmd := exec.Command(labBinaryPath, "mr", "edit", mrDiscussionCommitMRid+":"+commentid, "--delete-note")
   123  		cmd.Dir = repo
   124  
   125  		a, err := cmd.CombinedOutput()
   126  		if err != nil {
   127  			t.Log(string(a))
   128  			t.Fatal(err)
   129  		}
   130  	}
   131  }
   132  
   133  func Test_mrDiscussionCommitComments(t *testing.T) {
   134  	var testCommitID = "538c74715661e04fd4ffffcb72a04b6da969f8dd"
   135  
   136  	repo := copyTestRepo(t)
   137  
   138  	mrDiscussionCommitCommentCleanup(t, repo)
   139  
   140  	showText := mrDiscussionShow(t, repo)
   141  	require.NotContains(t, showText, "old line 3")
   142  	require.NotContains(t, showText, "context line 5")
   143  	require.NotContains(t, showText, "new line 8")
   144  
   145  	cmd := exec.Command(labBinaryPath, "mr", "checkout", mrDiscussionCommitMRid)
   146  	cmd.Dir = repo
   147  
   148  	a, err := cmd.CombinedOutput()
   149  	if err != nil {
   150  		t.Log(string(a))
   151  		t.Fatal(err)
   152  	}
   153  
   154  	//lab mr discussion 1159 --commit 538c74715661e04fd4ffffcb72a04b6da969f8dd --position=test:-3,3 -m "old line 3"
   155  	cmd = exec.Command(labBinaryPath, "mr", "discussion", mrDiscussionCommitMRid, "--commit", testCommitID, "--position=test:-3,3", "-m", "old line 3")
   156  	cmd.Dir = repo
   157  
   158  	a, err = cmd.CombinedOutput()
   159  	if err != nil {
   160  		t.Log(string(a))
   161  		t.Fatal(err)
   162  	}
   163  
   164  	showText = mrDiscussionShow(t, repo)
   165  	require.Contains(t, showText, "old line 3")
   166  	require.Contains(t, showText, `
   167  commit:538c74715661e04fd4ffffcb72a04b6da969f8dd
   168  File:test
   169  |        @@ -1,9 +1,11 @@
   170  |  1   1  line 1
   171  |  2   2  line 2
   172  |  3     -line 3`)
   173  	require.NotContains(t, showText, "context line 5")
   174  	require.NotContains(t, showText, "new line 8")
   175  
   176  	//lab mr discussion 1159 --commit 538c74715661e04fd4ffffcb72a04b6da969f8dd --position=test:\ 5,4 -m "context line 5"
   177  	cmd = exec.Command(labBinaryPath, "mr", "discussion", mrDiscussionCommitMRid, "--commit", testCommitID, "--position=test: 5,4", "-m", "context line 5")
   178  	cmd.Dir = repo
   179  
   180  	a, err = cmd.CombinedOutput()
   181  	if err != nil {
   182  		t.Log(string(a))
   183  		t.Fatal(err)
   184  	}
   185  
   186  	showText = mrDiscussionShow(t, repo)
   187  	require.Contains(t, showText, "context line 5")
   188  	require.Contains(t, showText, `
   189  commit:538c74715661e04fd4ffffcb72a04b6da969f8dd
   190  File:test
   191  |        @@ -1,9 +1,11 @@
   192  |  1   1  line 1
   193  |  2   2  line 2
   194  |  3     -line 3
   195  |  4   3  line 4
   196  |  5   4  line 5`)
   197  	require.NotContains(t, showText, "new line 8")
   198  
   199  	//lab mr discussion 1159 --commit 538c74715661e04fd4ffffcb72a04b6da969f8dd --position=test:+8,8 -m "new line 8"
   200  	cmd = exec.Command(labBinaryPath, "mr", "discussion", mrDiscussionCommitMRid, "--commit", testCommitID, "--position=test:+8,8", "-m", "new line 8")
   201  	cmd.Dir = repo
   202  
   203  	a, err = cmd.CombinedOutput()
   204  	if err != nil {
   205  		t.Log(string(a))
   206  		t.Fatal(err)
   207  	}
   208  
   209  	showText = mrDiscussionShow(t, repo)
   210  	require.Contains(t, showText, "new line 8")
   211  	require.Contains(t, showText, `
   212  commit:538c74715661e04fd4ffffcb72a04b6da969f8dd
   213  File:test
   214  |  5   4  line 5
   215  |  6   5  line 6
   216  |  7   6  line 7
   217  |  8     -line 8
   218  |  9   7  line 9
   219  |      8 +line 10`)
   220  	mrDiscussionCommitCommentCleanup(t, repo)
   221  }