зеркало из https://github.com/mislav/hub.git
Refactor to github.Editor to make it testable
This commit is contained in:
Родитель
d8d04fdccc
Коммит
aabfb73803
|
@ -92,5 +92,10 @@ func writeIssueTitleAndBody(project *github.Project) (string, string, error) {
|
|||
`
|
||||
message = fmt.Sprintf(message, project.Name)
|
||||
|
||||
return github.GetTitleAndBodyFromEditor("ISSUE", message)
|
||||
editor, err := github.NewEditor("ISSUE", message)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
return editor.EditTitleAndBody()
|
||||
}
|
||||
|
|
|
@ -183,9 +183,16 @@ func pullRequest(cmd *Command, args *Args) {
|
|||
|
||||
func writePullRequestTitleAndBody(base, head, fullBase, fullHead string, commits []string) (title, body string, err error) {
|
||||
message, err := pullRequestChangesMessage(base, head, fullBase, fullHead, commits)
|
||||
utils.Check(err)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return github.GetTitleAndBodyFromEditor("PULLREQ", message)
|
||||
editor, err := github.NewEditor("PULLREQ", message)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return editor.EditTitleAndBody()
|
||||
}
|
||||
|
||||
func pullRequestChangesMessage(base, head, fullBase, fullHead string, commits []string) (string, error) {
|
||||
|
|
|
@ -121,7 +121,12 @@ func writeReleaseTitleAndBody(project *github.Project, tag, currentBranch string
|
|||
`
|
||||
message = fmt.Sprintf(message, tag, project.Name, currentBranch)
|
||||
|
||||
return github.GetTitleAndBodyFromEditor("RELEASE", message)
|
||||
editor, err := github.NewEditor("RELEASE", message)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
return editor.EditTitleAndBody()
|
||||
}
|
||||
|
||||
func getAssetsDirectory(assetsDir, tag string) (string, error) {
|
||||
|
|
|
@ -2,6 +2,7 @@ package github
|
|||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"github.com/jingweno/gh/cmd"
|
||||
"github.com/jingweno/gh/git"
|
||||
|
@ -35,47 +36,74 @@ func GetTitleAndBodyFromFlags(messageFlag, fileFlag string) (title, body string,
|
|||
return
|
||||
}
|
||||
|
||||
func GetTitleAndBodyFromEditor(about, message string) (title, body string, err error) {
|
||||
messageFile, err := getMessageFile(about)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer os.Remove(messageFile)
|
||||
|
||||
if message != "" {
|
||||
err = ioutil.WriteFile(messageFile, []byte(message), 0644)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
editor, err := git.Editor()
|
||||
func NewEditor(topic, message string) (editor *Editor, err error) {
|
||||
messageFile, err := getMessageFile(topic)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = editTitleAndBody(editor, messageFile)
|
||||
program, err := git.Editor()
|
||||
if err != nil {
|
||||
err = fmt.Errorf("error using text editor for title/body message")
|
||||
return
|
||||
}
|
||||
|
||||
title, body, err = readTitleAndBody(messageFile)
|
||||
if err != nil {
|
||||
return
|
||||
editor = &Editor{
|
||||
Program: program,
|
||||
File: messageFile,
|
||||
Message: message,
|
||||
doEdit: doTextEditorEdit,
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func editTitleAndBody(editor, messageFile string) error {
|
||||
editCmd := cmd.New(editor)
|
||||
type Editor struct {
|
||||
Program string
|
||||
File string
|
||||
Message string
|
||||
doEdit func(program, file string) error
|
||||
}
|
||||
|
||||
func (e *Editor) Edit() (content []byte, err error) {
|
||||
if e.Message != "" {
|
||||
err = ioutil.WriteFile(e.File, []byte(e.Message), 0644)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
defer os.Remove(e.File)
|
||||
|
||||
err = e.doEdit(e.Program, e.File)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("error using text editor for editing message")
|
||||
return
|
||||
}
|
||||
|
||||
content, err = ioutil.ReadFile(e.File)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (e *Editor) EditTitleAndBody() (title, body string, err error) {
|
||||
content, err := e.Edit()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
reader := bufio.NewReader(bytes.NewReader(content))
|
||||
title, body, err = readTitleAndBodyFrom(reader)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func doTextEditorEdit(program, file string) error {
|
||||
editCmd := cmd.New(program)
|
||||
r := regexp.MustCompile("[mg]?vi[m]$")
|
||||
if r.MatchString(editor) {
|
||||
if r.MatchString(program) {
|
||||
editCmd.WithArg("-c")
|
||||
editCmd.WithArg("set ft=gitcommit tw=0 wrap lbr")
|
||||
}
|
||||
editCmd.WithArg(messageFile)
|
||||
editCmd.WithArg(file)
|
||||
|
||||
return editCmd.Exec()
|
||||
}
|
||||
|
|
|
@ -3,10 +3,64 @@ package github
|
|||
import (
|
||||
"bufio"
|
||||
"github.com/bmizerany/assert"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestEditor_Edit(t *testing.T) {
|
||||
tempFile, _ := ioutil.TempFile("", "editor-test")
|
||||
editor := Editor{
|
||||
Program: "memory",
|
||||
File: tempFile.Name(),
|
||||
doEdit: func(program string, file string) error {
|
||||
assert.Equal(t, "memory", program)
|
||||
assert.Equal(t, tempFile.Name(), file)
|
||||
|
||||
return ioutil.WriteFile(file, []byte("hello"), 0644)
|
||||
},
|
||||
}
|
||||
|
||||
content, err := editor.Edit()
|
||||
assert.Equal(t, nil, err)
|
||||
assert.Equal(t, "hello", string(content))
|
||||
|
||||
// file is removed after edit
|
||||
_, err = os.Stat(tempFile.Name())
|
||||
assert.T(t, os.IsNotExist(err))
|
||||
}
|
||||
|
||||
func TestEditor_EditTitleAndBody(t *testing.T) {
|
||||
tempFile, _ := ioutil.TempFile("", "editor-test")
|
||||
editor := Editor{
|
||||
Program: "memory",
|
||||
File: tempFile.Name(),
|
||||
doEdit: func(program string, file string) error {
|
||||
assert.Equal(t, "memory", program)
|
||||
assert.Equal(t, tempFile.Name(), file)
|
||||
|
||||
message := `A title
|
||||
A title continues
|
||||
|
||||
A body
|
||||
A body continues
|
||||
# comment
|
||||
`
|
||||
return ioutil.WriteFile(file, []byte(message), 0644)
|
||||
},
|
||||
}
|
||||
|
||||
title, body, err := editor.EditTitleAndBody()
|
||||
assert.Equal(t, nil, err)
|
||||
assert.Equal(t, "A title A title continues", title)
|
||||
assert.Equal(t, "A body\nA body continues", body)
|
||||
|
||||
// file is removed after edit
|
||||
_, err = os.Stat(tempFile.Name())
|
||||
assert.T(t, os.IsNotExist(err))
|
||||
}
|
||||
|
||||
func TestReadTitleAndBody(t *testing.T) {
|
||||
message := `A title
|
||||
A title continues
|
||||
|
|
Загрузка…
Ссылка в новой задаче