gitlab.com/lab-cli/lab@v0.14.0/internal/git/edit_test.go (about)

     1  package git
     2  
     3  import (
     4  	"github.com/stretchr/testify/assert"
     5  	"github.com/stretchr/testify/require"
     6  	"os"
     7  	"path/filepath"
     8  	"testing"
     9  )
    10  
    11  func Test_parseTitleBody(t *testing.T) {
    12  	tests := []struct {
    13  		Name          string
    14  		Message       string
    15  		ExpectedTitle string
    16  		ExpectedBody  string
    17  	}{
    18  		{
    19  			Name:          "Title Only",
    20  			Message:       "test commit",
    21  			ExpectedTitle: "test commit",
    22  			ExpectedBody:  "",
    23  		},
    24  		{
    25  			Name:          "Title and Body",
    26  			Message:       "test commit\n\ntest body",
    27  			ExpectedTitle: "test commit",
    28  			ExpectedBody:  "test body",
    29  		},
    30  		{
    31  			Name:          "Title and Body mixed comments",
    32  			Message:       "test commit\n\ntest body\n# comments\nmore of body",
    33  			ExpectedTitle: "test commit",
    34  			ExpectedBody:  "test body\nmore of body",
    35  		},
    36  		{
    37  			Name:          "Multiline Title",
    38  			Message:       "test commit\nand more body",
    39  			ExpectedTitle: "\n",
    40  			ExpectedBody:  "test commit\nand more body",
    41  		},
    42  		{
    43  			Name:          "Title includes issue number",
    44  			Message:       "test commit #100", // # is the commentChar
    45  			ExpectedTitle: "test commit #100",
    46  			ExpectedBody:  "",
    47  		},
    48  		{
    49  			Name:          "commented lines",
    50  			Message:       "# this is a comment\nThe title\n\nThe Body\n# another coment", // # is the commentChar
    51  			ExpectedTitle: "The title",
    52  			ExpectedBody:  "The Body",
    53  		},
    54  		{
    55  			Name:          "escaped commented lines",
    56  			Message:       "# this is a comment\nThe title\n\nThe Body\n\\# markdown title", // # is the commentChar
    57  			ExpectedTitle: "The title",
    58  			ExpectedBody:  "The Body\n# markdown title",
    59  		},
    60  	}
    61  
    62  	for _, test := range tests {
    63  		t.Run(test.Name, func(t *testing.T) {
    64  			test := test
    65  			t.Parallel()
    66  			title, body, err := parseTitleBody(test.Message)
    67  			if err != nil {
    68  				t.Fatal(err)
    69  			}
    70  
    71  			assert.Equal(t, title, test.ExpectedTitle)
    72  			assert.Equal(t, body, test.ExpectedBody)
    73  		})
    74  	}
    75  }
    76  
    77  func TestEditor(t *testing.T) {
    78  	filePath := filepath.Join(os.TempDir(), "labEditorTest")
    79  	if _, err := os.Stat(filePath); err == os.ErrExist {
    80  		os.Remove(filePath)
    81  	}
    82  
    83  	var path string
    84  	t.Run("editorPath()", func(t *testing.T) {
    85  		var err error
    86  		path, err = editorPath()
    87  		if err != nil {
    88  			t.Fatal(err)
    89  		}
    90  
    91  		require.NotEmpty(t, editorPath)
    92  	})
    93  	t.Run("Open Editor", func(t *testing.T) {
    94  		cmd := editorCMD(path, filePath)
    95  		err := cmd.Start()
    96  		if err != nil {
    97  			t.Fatal(err)
    98  		}
    99  		err = cmd.Process.Kill()
   100  		if err != nil {
   101  			t.Fatal(err)
   102  		}
   103  	})
   104  }