提交 295577d3 编写于 作者: J Jingwen Owen Ou

Remove editor file is there’s no title

See https://github.com/github/hub/issues/700
上级 2ce43736
......@@ -63,6 +63,10 @@ func (e *Editor) EditTitleAndBody() (title, body string, err error) {
reader := bytes.NewReader(content)
title, body, err = readTitleAndBody(reader, e.CS)
if err != nil || title == "" {
e.DeleteFile()
}
return
}
......
......@@ -5,7 +5,6 @@ import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
......@@ -59,14 +58,13 @@ func TestEditor_openAndEdit_readFileIfExist(t *testing.T) {
}
func TestEditor_openAndEdit_writeFileIfNotExist(t *testing.T) {
tempDir, _ := ioutil.TempDir("", "editor-test")
tempFile := filepath.Join(tempDir, "PULLREQ")
tempFile, _ := ioutil.TempFile("", "PULLREQ")
editor := Editor{
Program: "memory",
File: tempFile,
File: tempFile.Name(),
openEditor: func(program string, file string) error {
assert.Equal(t, "memory", program)
assert.Equal(t, tempFile, file)
assert.Equal(t, tempFile.Name(), file)
return ioutil.WriteFile(file, []byte("hello"), 0644)
},
......@@ -77,16 +75,37 @@ func TestEditor_openAndEdit_writeFileIfNotExist(t *testing.T) {
assert.Equal(t, "hello", string(content))
}
func TestEditor_EditTitleAndBodyEmptyTitle(t *testing.T) {
tempFile, _ := ioutil.TempFile("", "PULLREQ")
editor := Editor{
Program: "memory",
File: tempFile.Name(),
CS: "#",
openEditor: func(program string, file string) error {
assert.Equal(t, "memory", program)
assert.Equal(t, tempFile.Name(), file)
return ioutil.WriteFile(file, []byte(""), 0644)
},
}
title, body, err := editor.EditTitleAndBody()
assert.Equal(t, nil, err)
assert.Equal(t, "", title)
assert.Equal(t, "", body)
_, err = os.Stat(tempFile.Name())
assert.T(t, os.IsNotExist(err))
}
func TestEditor_EditTitleAndBody(t *testing.T) {
tempDir, _ := ioutil.TempDir("", "editor-test")
tempFile := filepath.Join(tempDir, "PULLREQ")
tempFile, _ := ioutil.TempFile("", "PULLREQ")
editor := Editor{
Program: "memory",
File: tempFile,
File: tempFile.Name(),
CS: "#",
openEditor: func(program string, file string) error {
assert.Equal(t, "memory", program)
assert.Equal(t, tempFile, file)
assert.Equal(t, tempFile.Name(), file)
message := `A title
A title continues
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册