From 7a91eb7e1d20bf99ce4fa9d751c02b7b4d100d45 Mon Sep 17 00:00:00 2001 From: Steve Moyer Date: Fri, 19 Aug 2022 13:19:02 -0400 Subject: tests(850): add tests for comment ls, add and edit --- commands/comment_add_test.go | 33 +++++ commands/comment_edit_test.go | 36 +++++ commands/comment_test.go | 160 +++++++++++++++++++++ commands/golden_test.go | 5 + commands/testdata/comment/add-0-golden.txt | 3 + commands/testdata/comment/add-1-golden.txt | 6 + commands/testdata/comment/edit-0-golden.txt | 3 + commands/testdata/comment/edit-1-golden.txt | 6 + .../testdata/comment/message-only-0-golden.txt | 3 + 9 files changed, 255 insertions(+) create mode 100644 commands/comment_add_test.go create mode 100644 commands/comment_edit_test.go create mode 100644 commands/comment_test.go create mode 100644 commands/golden_test.go create mode 100644 commands/testdata/comment/add-0-golden.txt create mode 100644 commands/testdata/comment/add-1-golden.txt create mode 100644 commands/testdata/comment/edit-0-golden.txt create mode 100644 commands/testdata/comment/edit-1-golden.txt create mode 100644 commands/testdata/comment/message-only-0-golden.txt (limited to 'commands') diff --git a/commands/comment_add_test.go b/commands/comment_add_test.go new file mode 100644 index 00000000..34ff3743 --- /dev/null +++ b/commands/comment_add_test.go @@ -0,0 +1,33 @@ +package commands + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func newTestEnvAndBugWithComment(t *testing.T) (*testEnv, string, string) { + t.Helper() + + env, bugID := newTestEnvAndBug(t) + + opts := commentAddOptions{ + message: "this is a bug comment", + } + require.NoError(t, runCommentAdd(env.env, opts, []string{bugID})) + require.NoError(t, runComment(env.env, []string{bugID})) + comments := parseComments(t, env) + require.Len(t, comments, 2) + + env.out.Reset() + + return env, bugID, comments[1].id +} + +func TestCommentAdd(t *testing.T) { + const golden = "testdata/comment/add" + + env, bugID, _ := newTestEnvAndBugWithComment(t) + require.NoError(t, runComment(env.env, []string{bugID})) + requireCommentsEqual(t, golden, env) +} diff --git a/commands/comment_edit_test.go b/commands/comment_edit_test.go new file mode 100644 index 00000000..191a089c --- /dev/null +++ b/commands/comment_edit_test.go @@ -0,0 +1,36 @@ +package commands + +import ( + "testing" + + "github.com/MichaelMure/git-bug/bug" + "github.com/stretchr/testify/require" +) + +func TestCommentEdit(t *testing.T) { + const golden = "testdata/comment/edit" + + env, bugID, commentID := newTestEnvAndBugWithComment(t) + + opts := commentEditOptions{ + message: "this is an altered bug comment", + } + require.NoError(t, runCommentEdit(env.env, opts, []string{commentID})) + + // TODO: remove this comment and everything between the "snip" + // comments when issue #850 is resolved. + + // ***** snip ***** + cache, err := env.env.backend.ResolveBugPrefix(bugID) + require.NoError(t, err) + bu, err := bug.Read(env.env.repo, cache.Id()) + require.NoError(t, err) + for _, op := range bu.Operations() { + t.Log("Operation: ", op) + } + t.Log("Compiled comments: ", bu.Compile().Comments) + // ***** snip ***** + + require.NoError(t, runComment(env.env, []string{bugID})) + requireCommentsEqual(t, golden, env) +} diff --git a/commands/comment_test.go b/commands/comment_test.go new file mode 100644 index 00000000..c2e7cb36 --- /dev/null +++ b/commands/comment_test.go @@ -0,0 +1,160 @@ +package commands + +import ( + "fmt" + "io/ioutil" + "strings" + "testing" + "time" + + "github.com/stretchr/testify/require" +) + +func TestComment(t *testing.T) { + const golden = "testdata/comment/message-only" + + env, bug := newTestEnvAndBug(t) + + require.NoError(t, runComment(env.env, []string{bug})) + + 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 *testEnv) []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) + + 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 *testEnv) { + t.Helper() + + const goldenFilePatter = "%s-%d-golden.txt" + + comments := parseComments(t, env) + comments = normalizeParsedComments(t, comments) + + if *update { + t.Log("Got here") + for i, comment := range comments { + fileName := fmt.Sprintf(goldenFilePatter, golden, i) + 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) + + fileName := fmt.Sprintf(goldenFilePatter, golden, i) + exp, err := ioutil.ReadFile(fileName) + require.NoError(t, err) + require.Equal(t, string(exp), comment.message) + } +} diff --git a/commands/golden_test.go b/commands/golden_test.go new file mode 100644 index 00000000..9fcee0d6 --- /dev/null +++ b/commands/golden_test.go @@ -0,0 +1,5 @@ +package commands + +import "flag" + +var update = flag.Bool("update", false, "update golden files") diff --git a/commands/testdata/comment/add-0-golden.txt b/commands/testdata/comment/add-0-golden.txt new file mode 100644 index 00000000..44ae0c1a --- /dev/null +++ b/commands/testdata/comment/add-0-golden.txt @@ -0,0 +1,3 @@ + + + this is a bug message diff --git a/commands/testdata/comment/add-1-golden.txt b/commands/testdata/comment/add-1-golden.txt new file mode 100644 index 00000000..bcf127c0 --- /dev/null +++ b/commands/testdata/comment/add-1-golden.txt @@ -0,0 +1,6 @@ + + + this is a bug message + + + this is a bug comment diff --git a/commands/testdata/comment/edit-0-golden.txt b/commands/testdata/comment/edit-0-golden.txt new file mode 100644 index 00000000..44ae0c1a --- /dev/null +++ b/commands/testdata/comment/edit-0-golden.txt @@ -0,0 +1,3 @@ + + + this is a bug message diff --git a/commands/testdata/comment/edit-1-golden.txt b/commands/testdata/comment/edit-1-golden.txt new file mode 100644 index 00000000..bcf127c0 --- /dev/null +++ b/commands/testdata/comment/edit-1-golden.txt @@ -0,0 +1,6 @@ + + + this is a bug message + + + this is a bug comment diff --git a/commands/testdata/comment/message-only-0-golden.txt b/commands/testdata/comment/message-only-0-golden.txt new file mode 100644 index 00000000..44ae0c1a --- /dev/null +++ b/commands/testdata/comment/message-only-0-golden.txt @@ -0,0 +1,3 @@ + + + this is a bug message -- cgit From 2c2c449187557384fd1e5b9333e808451be86041 Mon Sep 17 00:00:00 2001 From: Steve Moyer Date: Fri, 19 Aug 2022 19:09:42 -0400 Subject: fix(850): remove obsolete test logging --- commands/ls_test.go | 1 - 1 file changed, 1 deletion(-) (limited to 'commands') diff --git a/commands/ls_test.go b/commands/ls_test.go index 6e31ed83..5759f9d0 100644 --- a/commands/ls_test.go +++ b/commands/ls_test.go @@ -77,7 +77,6 @@ $` env, _ := newTestEnvAndBug(t) require.NoError(t, runLs(env.env, opts, []string{})) - t.Log(env.out.String()) require.Regexp(t, testcase.exp, env.out.String()) }) } -- cgit From ff1b7448c9dab4a3723cb12ada49bc73ea7dc755 Mon Sep 17 00:00:00 2001 From: Steve Moyer Date: Tue, 23 Aug 2022 10:40:56 -0400 Subject: fix(850): merge in CombinedId from 664 --- commands/comment_edit_test.go | 15 --------------- commands/testdata/comment/edit-1-golden.txt | 2 +- 2 files changed, 1 insertion(+), 16 deletions(-) (limited to 'commands') diff --git a/commands/comment_edit_test.go b/commands/comment_edit_test.go index 191a089c..50c1850b 100644 --- a/commands/comment_edit_test.go +++ b/commands/comment_edit_test.go @@ -3,7 +3,6 @@ package commands import ( "testing" - "github.com/MichaelMure/git-bug/bug" "github.com/stretchr/testify/require" ) @@ -17,20 +16,6 @@ func TestCommentEdit(t *testing.T) { } require.NoError(t, runCommentEdit(env.env, opts, []string{commentID})) - // TODO: remove this comment and everything between the "snip" - // comments when issue #850 is resolved. - - // ***** snip ***** - cache, err := env.env.backend.ResolveBugPrefix(bugID) - require.NoError(t, err) - bu, err := bug.Read(env.env.repo, cache.Id()) - require.NoError(t, err) - for _, op := range bu.Operations() { - t.Log("Operation: ", op) - } - t.Log("Compiled comments: ", bu.Compile().Comments) - // ***** snip ***** - require.NoError(t, runComment(env.env, []string{bugID})) requireCommentsEqual(t, golden, env) } diff --git a/commands/testdata/comment/edit-1-golden.txt b/commands/testdata/comment/edit-1-golden.txt index bcf127c0..3d83c02b 100644 --- a/commands/testdata/comment/edit-1-golden.txt +++ b/commands/testdata/comment/edit-1-golden.txt @@ -3,4 +3,4 @@ this is a bug message - this is a bug comment + this is an altered bug comment -- cgit From 0f885d4fb55d7133f7b4c4fe9b94c14511db673c Mon Sep 17 00:00:00 2001 From: Steve Moyer Date: Tue, 23 Aug 2022 11:02:23 -0400 Subject: fix(850): normalize Windows line endings -> *nix --- commands/comment_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'commands') diff --git a/commands/comment_test.go b/commands/comment_test.go index c2e7cb36..74cfee81 100644 --- a/commands/comment_test.go +++ b/commands/comment_test.go @@ -155,6 +155,6 @@ func requireCommentsEqual(t *testing.T, golden string, env *testEnv) { fileName := fmt.Sprintf(goldenFilePatter, golden, i) exp, err := ioutil.ReadFile(fileName) require.NoError(t, err) - require.Equal(t, string(exp), comment.message) + require.Equal(t, string(exp), strings.ReplaceAll(comment.message, "\r", "")) } } -- cgit From c4a4d457f7327083d530c2b70e2e41e04f1f13f6 Mon Sep 17 00:00:00 2001 From: Steve Moyer Date: Tue, 23 Aug 2022 11:22:42 -0400 Subject: fix(850): normalize Windows line endings -> *nix (golden files) --- commands/comment_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'commands') diff --git a/commands/comment_test.go b/commands/comment_test.go index 74cfee81..43062ed0 100644 --- a/commands/comment_test.go +++ b/commands/comment_test.go @@ -155,6 +155,6 @@ func requireCommentsEqual(t *testing.T, golden string, env *testEnv) { fileName := fmt.Sprintf(goldenFilePatter, golden, i) exp, err := ioutil.ReadFile(fileName) require.NoError(t, err) - require.Equal(t, string(exp), strings.ReplaceAll(comment.message, "\r", "")) + require.Equal(t, strings.ReplaceAll(string(exp), "\r", ""), strings.ReplaceAll(comment.message, "\r", "")) } } -- cgit