From 98eb8cc928e1cb7bd94acb01179ec8d7e734dc90 Mon Sep 17 00:00:00 2001 From: hau Date: Sun, 29 Nov 2020 20:02:40 +0700 Subject: [PATCH] test(changelog): unit test for changelog with markdown --- pkg/changelog/markdown.go | 2 +- pkg/changelog/markdown_test.go | 48 +++++++++++++++++++ .../empty_old_data.golden | 15 ++++++ pkg/markdown/parse.go | 2 + 4 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 pkg/changelog/markdown_test.go create mode 100644 pkg/changelog/testdata/TestMarkdownGeneratorGenerate/empty_old_data.golden diff --git a/pkg/changelog/markdown.go b/pkg/changelog/markdown.go index 50202a6..92cbd70 100644 --- a/pkg/changelog/markdown.go +++ b/pkg/changelog/markdown.go @@ -112,7 +112,7 @@ func (g *MarkdownGenerator) getOldNodes() []markdown.Node { result = append(result, markdown.Parse(lines)...) - // remove title + // remove title as we will add later if len(result) > 0 && markdown.Equal(result[0], markdown.NewHeader(firstLevel, title)) { result = result[1:] } diff --git a/pkg/changelog/markdown_test.go b/pkg/changelog/markdown_test.go new file mode 100644 index 0000000..f55da50 --- /dev/null +++ b/pkg/changelog/markdown_test.go @@ -0,0 +1,48 @@ +package changelog + +import ( + "testing" + "time" + + "github.com/haunt98/changeloguru/pkg/convention" + "github.com/sebdah/goldie/v2" +) + +func TestMarkdownGeneratorGenerate(t *testing.T) { + tests := []struct { + name string + oldData string + version string + t time.Time + commits []convention.Commit + }{ + { + name: "empty old data", + version: "v1.0.0", + t: time.Date(2020, 1, 18, 0, 0, 0, 0, time.Local), + commits: []convention.Commit{ + { + RawHeader: "feat: new feature", + Type: convention.FeatType, + }, + { + RawHeader: "fix: new fix", + Type: convention.FixType, + }, + { + RawHeader: "chore: new build", + Type: convention.ChoreType, + }, + }, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + g := goldie.New(t) + markdownGenerator := NewMarkdownGenerator(tc.oldData, tc.version, tc.t) + got := markdownGenerator.Generate(tc.commits) + g.Assert(t, t.Name(), []byte(got)) + }) + } +} diff --git a/pkg/changelog/testdata/TestMarkdownGeneratorGenerate/empty_old_data.golden b/pkg/changelog/testdata/TestMarkdownGeneratorGenerate/empty_old_data.golden new file mode 100644 index 0000000..d02c02c --- /dev/null +++ b/pkg/changelog/testdata/TestMarkdownGeneratorGenerate/empty_old_data.golden @@ -0,0 +1,15 @@ +# CHANGELOG + +## v1.0.0 (2020-1-18) + +### Added + +- feat: new feature + +### Fixed + +- feat: new feature + +### Others + +- chore: new build \ No newline at end of file diff --git a/pkg/markdown/parse.go b/pkg/markdown/parse.go index 45e7e2c..36b5d46 100644 --- a/pkg/markdown/parse.go +++ b/pkg/markdown/parse.go @@ -10,6 +10,8 @@ func Parse(lines []string) []Node { bases := make([]Node, 0, defaultBaseLen) for _, line := range lines { + line = strings.TrimSpace(line) + if line == "" { continue }