github.com/jingweno/gh@v2.1.1-0.20221007190738-04a7985fa9a1+incompatible/github/editor_test.go (about)

     1  package github
     2  
     3  import (
     4  	"bufio"
     5  	"github.com/bmizerany/assert"
     6  	"io/ioutil"
     7  	"os"
     8  	"strings"
     9  	"testing"
    10  )
    11  
    12  func TestEditor_Edit(t *testing.T) {
    13  	tempFile, _ := ioutil.TempFile("", "editor-test")
    14  	editor := Editor{
    15  		Program: "memory",
    16  		File:    tempFile.Name(),
    17  		doEdit: func(program string, file string) error {
    18  			assert.Equal(t, "memory", program)
    19  			assert.Equal(t, tempFile.Name(), file)
    20  
    21  			return ioutil.WriteFile(file, []byte("hello"), 0644)
    22  		},
    23  	}
    24  
    25  	content, err := editor.Edit()
    26  	assert.Equal(t, nil, err)
    27  	assert.Equal(t, "hello", string(content))
    28  
    29  	// file is removed after edit
    30  	_, err = os.Stat(tempFile.Name())
    31  	assert.T(t, os.IsNotExist(err))
    32  }
    33  
    34  func TestEditor_EditTitleAndBody(t *testing.T) {
    35  	tempFile, _ := ioutil.TempFile("", "editor-test")
    36  	editor := Editor{
    37  		Program: "memory",
    38  		File:    tempFile.Name(),
    39  		doEdit: func(program string, file string) error {
    40  			assert.Equal(t, "memory", program)
    41  			assert.Equal(t, tempFile.Name(), file)
    42  
    43  			message := `A title
    44  A title continues
    45  
    46  A body
    47  A body continues
    48  # comment
    49  `
    50  			return ioutil.WriteFile(file, []byte(message), 0644)
    51  		},
    52  	}
    53  
    54  	title, body, err := editor.EditTitleAndBody()
    55  	assert.Equal(t, nil, err)
    56  	assert.Equal(t, "A title A title continues", title)
    57  	assert.Equal(t, "A body\nA body continues", body)
    58  
    59  	// file is removed after edit
    60  	_, err = os.Stat(tempFile.Name())
    61  	assert.T(t, os.IsNotExist(err))
    62  }
    63  
    64  func TestReadTitleAndBody(t *testing.T) {
    65  	message := `A title
    66  A title continues
    67  
    68  A body
    69  A body continues
    70  # comment
    71  `
    72  	r := strings.NewReader(message)
    73  	reader := bufio.NewReader(r)
    74  	title, body, err := readTitleAndBody(reader)
    75  	assert.Equal(t, nil, err)
    76  	assert.Equal(t, "A title A title continues", title)
    77  	assert.Equal(t, "A body\nA body continues", body)
    78  }
    79  
    80  func TestGetMessageFile(t *testing.T) {
    81  	gitPullReqMsgFile, _ := getMessageFile("PULLREQ")
    82  	assert.T(t, strings.Contains(gitPullReqMsgFile, "PULLREQ_EDITMSG"))
    83  }