github.com/ianfoo/lab@v0.9.5-0.20180123060006-5ed79f2ccfc7/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 title",
    39  			ExpectedTitle: "test commit and more title",
    40  			ExpectedBody:  "",
    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  
    50  	for _, test := range tests {
    51  		t.Run(test.Name, func(t *testing.T) {
    52  			test := test
    53  			t.Parallel()
    54  			title, body, err := parseTitleBody(test.Message)
    55  			if err != nil {
    56  				t.Fatal(err)
    57  			}
    58  
    59  			assert.Equal(t, title, test.ExpectedTitle)
    60  			assert.Equal(t, body, test.ExpectedBody)
    61  		})
    62  	}
    63  }
    64  
    65  func TestEditor(t *testing.T) {
    66  	filePath := filepath.Join(os.TempDir(), "labEditorTest")
    67  	if _, err := os.Stat(filePath); err == os.ErrExist {
    68  		os.Remove(filePath)
    69  	}
    70  
    71  	var path string
    72  	t.Run("editorPath()", func(t *testing.T) {
    73  		var err error
    74  		path, err = editorPath()
    75  		if err != nil {
    76  			t.Fatal(err)
    77  		}
    78  
    79  		require.NotEmpty(t, editorPath)
    80  	})
    81  	t.Run("Open Editor", func(t *testing.T) {
    82  		cmd := editorCMD(path, filePath)
    83  		err := cmd.Start()
    84  		if err != nil {
    85  			t.Fatal(err)
    86  		}
    87  		err = cmd.Process.Kill()
    88  		if err != nil {
    89  			t.Fatal(err)
    90  		}
    91  	})
    92  }