0
0
mirror of https://github.com/thegeeklab/git-sv.git synced 2024-11-28 06:30:35 +00:00
git-sv/sv/formatter_test.go

139 lines
3.9 KiB
Go
Raw Normal View History

2020-05-01 03:45:08 +00:00
package sv
import (
"bytes"
"os"
2020-05-01 03:45:08 +00:00
"testing"
"time"
2021-02-13 18:40:09 +00:00
"github.com/Masterminds/semver/v3"
2020-05-01 03:45:08 +00:00
)
var templatesFS = os.DirFS("../cmd/git-sv/resources/templates")
2020-05-01 03:45:08 +00:00
var dateChangelog = `## v1.0.0 (2020-05-01)
`
2021-07-31 19:03:58 +00:00
var nonVersioningChangelog = `## abc (2020-05-01)
`
2020-05-01 03:45:08 +00:00
var emptyDateChangelog = `## v1.0.0
`
2021-07-31 19:03:58 +00:00
var emptyVersionChangelog = `## 2020-05-01
`
2021-07-31 19:03:58 +00:00
var fullChangeLog = `## v1.0.0 (2020-05-01)
### Features
- subject text ()
### Bug Fixes
- subject text ()
### Build
- subject text ()
### Breaking Changes
- break change message
`
2020-05-01 03:45:08 +00:00
func TestOutputFormatterImpl_FormatReleaseNote(t *testing.T) {
date, _ := time.Parse("2006-01-02", "2020-05-01")
tests := []struct {
2021-07-31 20:34:40 +00:00
name string
input ReleaseNote
want string
wantErr bool
2020-05-01 03:45:08 +00:00
}{
2021-07-31 20:34:40 +00:00
{"with date", emptyReleaseNote("1.0.0", date.Truncate(time.Minute)), dateChangelog, false},
{"without date", emptyReleaseNote("1.0.0", time.Time{}.Truncate(time.Minute)), emptyDateChangelog, false},
{"without version", emptyReleaseNote("", date.Truncate(time.Minute)), emptyVersionChangelog, false},
{"non versioning tag", emptyReleaseNote("abc", date.Truncate(time.Minute)), nonVersioningChangelog, false},
2021-07-31 20:34:40 +00:00
{"full changelog", fullReleaseNote("1.0.0", date.Truncate(time.Minute)), fullChangeLog, false},
2020-05-01 03:45:08 +00:00
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := NewOutputFormatter(templatesFS).FormatReleaseNote(tt.input)
2021-07-31 20:34:40 +00:00
if got != tt.want {
2020-05-01 03:45:08 +00:00
t.Errorf("OutputFormatterImpl.FormatReleaseNote() = %v, want %v", got, tt.want)
}
2021-07-31 20:34:40 +00:00
if (err != nil) != tt.wantErr {
t.Errorf("OutputFormatterImpl.FormatReleaseNote() error = %v, wantErr %v", err, tt.wantErr)
}
2020-05-01 03:45:08 +00:00
})
}
}
func emptyReleaseNote(tag string, date time.Time) ReleaseNote {
v, _ := semver.NewVersion(tag)
2020-05-01 03:45:08 +00:00
return ReleaseNote{
Version: v,
Tag: tag,
2020-05-01 03:45:08 +00:00
Date: date,
}
}
func fullReleaseNote(tag string, date time.Time) ReleaseNote {
v, _ := semver.NewVersion(tag)
sections := []ReleaseNoteSection{
newReleaseNoteCommitsSection("Features", []string{"feat"}, []GitCommitLog{commitlog("feat", map[string]string{}, "a")}),
newReleaseNoteCommitsSection("Bug Fixes", []string{"fix"}, []GitCommitLog{commitlog("fix", map[string]string{}, "a")}),
newReleaseNoteCommitsSection("Build", []string{"build"}, []GitCommitLog{commitlog("build", map[string]string{}, "a")}),
ReleaseNoteBreakingChangeSection{"Breaking Changes", []string{"break change message"}},
}
return releaseNote(v, tag, date, sections, map[string]struct{}{"a": {}})
}
func Test_checkTemplatesExecution(t *testing.T) {
tpls := NewOutputFormatter(templatesFS).templates
tests := []struct {
template string
variables interface{}
}{
{"changelog-md.tpl", changelogVariables("v1.0.0", "v1.0.1")},
{"releasenotes-md.tpl", releaseNotesVariables("v1.0.0")},
}
for _, tt := range tests {
t.Run(tt.template, func(t *testing.T) {
var b bytes.Buffer
err := tpls.ExecuteTemplate(&b, tt.template, tt.variables)
if err != nil {
t.Errorf("invalid template err = %v", err)
return
}
if len(b.Bytes()) <= 0 {
t.Errorf("empty template")
}
})
}
}
func releaseNotesVariables(release string) releaseNoteTemplateVariables {
return releaseNoteTemplateVariables{
Release: release,
Date: time.Date(2006, 1, 02, 0, 0, 0, 0, time.UTC),
Sections: []ReleaseNoteSection{
newReleaseNoteCommitsSection("Features", []string{"feat"}, []GitCommitLog{commitlog("feat", map[string]string{}, "a")}),
newReleaseNoteCommitsSection("Bug Fixes", []string{"fix"}, []GitCommitLog{commitlog("fix", map[string]string{}, "a")}),
newReleaseNoteCommitsSection("Build", []string{"build"}, []GitCommitLog{commitlog("build", map[string]string{}, "a")}),
ReleaseNoteBreakingChangeSection{"Breaking Changes", []string{"break change message"}},
},
}
}
func changelogVariables(releases ...string) []releaseNoteTemplateVariables {
var variables []releaseNoteTemplateVariables
for _, r := range releases {
variables = append(variables, releaseNotesVariables(r))
}
return variables
}