github.com/zaquestion/lab@v0.25.1/internal/git/edit_test.go (about)

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