aboutsummaryrefslogtreecommitdiffstats
path: root/commands/bug/bug_comment_test.go
blob: add48b218b04dbc5ab553d489446e60dea6df7de (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package bugcmd

import (
	"fmt"
	"os"
	"strings"
	"testing"
	"time"

	"github.com/stretchr/testify/require"

	"github.com/MichaelMure/git-bug/commands/bug/testenv"
	"github.com/MichaelMure/git-bug/commands/cmdtest"
	"github.com/MichaelMure/git-bug/commands/execenv"
)

func TestBugComment(t *testing.T) {
	const golden = "testdata/comment/message-only"

	env, bug := testenv.NewTestEnvAndBug(t)

	require.NoError(t, runBugComment(env, []string{bug.Human()}))

	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
}

func parseComments(t *testing.T, env *execenv.Env) []parsedComment {
	t.Helper()

	parser := &commentParser{
		t:        t,
		comments: []parsedComment{},
	}

	comment := &parsedComment{}
	parser.fn = parser.parseAuthor

	for _, line := range strings.Split(env.Out.String(), "\n") {
		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)

	var out []parsedComment

	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
}

func requireCommentsEqual(t *testing.T, golden string, env *execenv.Env) {
	t.Helper()

	const goldenFilePattern = "%s-%d-golden.txt"

	comments := parseComments(t, env)
	comments = normalizeParsedComments(t, comments)

	if *cmdtest.Update {
		for i, comment := range comments {
			fileName := fmt.Sprintf(goldenFilePattern, golden, i)
			require.NoError(t, os.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)

		fileName := fmt.Sprintf(goldenFilePattern, golden, i)
		exp, err := os.ReadFile(fileName)
		require.NoError(t, err)
		require.Equal(t, strings.ReplaceAll(string(exp), "\r", ""), strings.ReplaceAll(comment.message, "\r", ""))
	}
}