2022-09-10 12:09:19 +03:00
|
|
|
package bugcmd
|
2022-08-19 20:19:02 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2022-09-10 12:09:19 +03:00
|
|
|
|
|
|
|
"github.com/MichaelMure/git-bug/commands/bug/testenv"
|
|
|
|
"github.com/MichaelMure/git-bug/commands/cmdtest"
|
|
|
|
"github.com/MichaelMure/git-bug/commands/execenv"
|
2022-08-19 20:19:02 +03:00
|
|
|
)
|
|
|
|
|
2022-09-10 12:09:19 +03:00
|
|
|
func TestBugComment(t *testing.T) {
|
2022-08-19 20:19:02 +03:00
|
|
|
const golden = "testdata/comment/message-only"
|
|
|
|
|
2022-09-10 12:09:19 +03:00
|
|
|
env, bug := testenv.NewTestEnvAndBug(t)
|
2022-08-19 20:19:02 +03:00
|
|
|
|
2022-09-10 12:09:19 +03:00
|
|
|
require.NoError(t, runBugComment(env, []string{bug.Human()}))
|
2022-08-19 20:19:02 +03:00
|
|
|
|
|
|
|
requireCommentsEqual(t, golden, env)
|
|
|
|
}
|
|
|
|
|
|
|
|
const gitDateFormat = "Mon Jan 2 15:04:05 2006 -0700"
|
|
|
|
|
|
|
|
type parsedComment struct {
|
|
|
|
author string
|
|
|
|
id string
|
|
|
|
date time.Time
|
|
|
|
message string
|
|
|
|
}
|
|
|
|
|
|
|
|
type parseFunc func(*parsedComment, string)
|
|
|
|
|
|
|
|
type commentParser struct {
|
|
|
|
t *testing.T
|
|
|
|
fn parseFunc
|
|
|
|
comments []parsedComment
|
|
|
|
}
|
|
|
|
|
2022-09-10 12:09:19 +03:00
|
|
|
func parseComments(t *testing.T, env *execenv.Env) []parsedComment {
|
2022-08-19 20:19:02 +03:00
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
parser := &commentParser{
|
|
|
|
t: t,
|
|
|
|
comments: []parsedComment{},
|
|
|
|
}
|
|
|
|
|
|
|
|
comment := &parsedComment{}
|
|
|
|
parser.fn = parser.parseAuthor
|
|
|
|
|
2022-09-10 12:09:19 +03:00
|
|
|
for _, line := range strings.Split(env.Out.String(), "\n") {
|
2022-08-19 20:19:02 +03:00
|
|
|
parser.fn(comment, line)
|
|
|
|
}
|
|
|
|
|
|
|
|
parser.comments = append(parser.comments, *comment)
|
|
|
|
|
|
|
|
return parser.comments
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *commentParser) parseAuthor(comment *parsedComment, line string) {
|
|
|
|
p.t.Helper()
|
|
|
|
|
|
|
|
tkns := strings.Split(line, ": ")
|
|
|
|
require.Len(p.t, tkns, 2)
|
|
|
|
require.Equal(p.t, "Author", tkns[0])
|
|
|
|
|
|
|
|
comment.author = tkns[1]
|
|
|
|
p.fn = p.parseID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *commentParser) parseID(comment *parsedComment, line string) {
|
|
|
|
p.t.Helper()
|
|
|
|
|
|
|
|
tkns := strings.Split(line, ": ")
|
|
|
|
require.Len(p.t, tkns, 2)
|
|
|
|
require.Equal(p.t, "Id", tkns[0])
|
|
|
|
|
|
|
|
comment.id = tkns[1]
|
|
|
|
p.fn = p.parseDate
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *commentParser) parseDate(comment *parsedComment, line string) {
|
|
|
|
p.t.Helper()
|
|
|
|
|
|
|
|
tkns := strings.Split(line, ": ")
|
|
|
|
require.Len(p.t, tkns, 2)
|
|
|
|
require.Equal(p.t, "Date", tkns[0])
|
|
|
|
|
|
|
|
date, err := time.Parse(gitDateFormat, tkns[1])
|
|
|
|
require.NoError(p.t, err)
|
|
|
|
|
|
|
|
comment.date = date
|
|
|
|
p.fn = p.parseMessage
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *commentParser) parseMessage(comment *parsedComment, line string) {
|
|
|
|
p.t.Helper()
|
|
|
|
|
|
|
|
if strings.HasPrefix(line, "Author: ") {
|
|
|
|
p.comments = append(p.comments, *comment)
|
|
|
|
comment = &parsedComment{}
|
|
|
|
p.parseAuthor(comment, line)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
require.True(p.t, line == "" || strings.HasPrefix(line, " "))
|
|
|
|
|
|
|
|
comment.message = strings.Join([]string{comment.message, line}, "\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
func normalizeParsedComments(t *testing.T, comments []parsedComment) []parsedComment {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
prefix := 0x1234567
|
|
|
|
date, err := time.Parse(gitDateFormat, "Fri Aug 19 07:00:00 2022 +1900")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-09-10 12:09:19 +03:00
|
|
|
var out []parsedComment
|
2022-08-19 20:19:02 +03:00
|
|
|
|
|
|
|
for i, comment := range comments {
|
|
|
|
comment.id = fmt.Sprintf("%7x", prefix+i)
|
|
|
|
comment.date = date.Add(time.Duration(i) * time.Minute)
|
|
|
|
out = append(out, comment)
|
|
|
|
}
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2022-09-10 12:09:19 +03:00
|
|
|
func requireCommentsEqual(t *testing.T, golden string, env *execenv.Env) {
|
2022-08-19 20:19:02 +03:00
|
|
|
t.Helper()
|
|
|
|
|
2022-09-10 12:09:19 +03:00
|
|
|
const goldenFilePattern = "%s-%d-golden.txt"
|
2022-08-19 20:19:02 +03:00
|
|
|
|
|
|
|
comments := parseComments(t, env)
|
|
|
|
comments = normalizeParsedComments(t, comments)
|
|
|
|
|
2022-09-10 12:09:19 +03:00
|
|
|
if *cmdtest.Update {
|
2022-08-19 20:19:02 +03:00
|
|
|
t.Log("Got here")
|
|
|
|
for i, comment := range comments {
|
2022-09-10 12:09:19 +03:00
|
|
|
fileName := fmt.Sprintf(goldenFilePattern, golden, i)
|
2022-08-19 20:19:02 +03:00
|
|
|
require.NoError(t, ioutil.WriteFile(fileName, []byte(comment.message), 0644))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
prefix := 0x1234567
|
|
|
|
date, err := time.Parse(gitDateFormat, "Fri Aug 19 07:00:00 2022 +1900")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
for i, comment := range comments {
|
|
|
|
require.Equal(t, "John Doe", comment.author)
|
|
|
|
require.Equal(t, fmt.Sprintf("%7x", prefix+i), comment.id)
|
|
|
|
require.Equal(t, date.Add(time.Duration(i)*time.Minute), comment.date)
|
|
|
|
|
2022-09-10 12:09:19 +03:00
|
|
|
fileName := fmt.Sprintf(goldenFilePattern, golden, i)
|
2022-08-19 20:19:02 +03:00
|
|
|
exp, err := ioutil.ReadFile(fileName)
|
|
|
|
require.NoError(t, err)
|
2022-08-23 18:22:42 +03:00
|
|
|
require.Equal(t, strings.ReplaceAll(string(exp), "\r", ""), strings.ReplaceAll(comment.message, "\r", ""))
|
2022-08-19 20:19:02 +03:00
|
|
|
}
|
|
|
|
}
|