2023-10-15 19:29:29 +00:00
|
|
|
package formatter
|
2020-05-01 03:45:08 +00:00
|
|
|
|
|
|
|
import (
|
2022-01-30 21:45:16 +00:00
|
|
|
"bytes"
|
2020-05-01 03:45:08 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-02-13 18:40:09 +00:00
|
|
|
"github.com/Masterminds/semver/v3"
|
2023-10-15 19:29:29 +00:00
|
|
|
"github.com/thegeeklab/git-sv/v2/sv"
|
|
|
|
"github.com/thegeeklab/git-sv/v2/templates"
|
2020-05-01 03:45:08 +00:00
|
|
|
)
|
|
|
|
|
2023-10-15 19:29:29 +00:00
|
|
|
var tmpls = templates.New("")
|
2022-02-06 21:15:36 +00:00
|
|
|
|
2023-10-13 21:32:14 +00:00
|
|
|
var dateChangelog = `## v1.0.0 (2020-05-01)`
|
2021-07-31 19:03:58 +00:00
|
|
|
|
2023-10-13 21:32:14 +00:00
|
|
|
var nonVersioningChangelog = `## abc (2020-05-01)`
|
2021-12-29 21:02:50 +00:00
|
|
|
|
2023-10-13 21:32:14 +00:00
|
|
|
var emptyDateChangelog = `## v1.0.0`
|
2021-07-31 19:03:58 +00:00
|
|
|
|
2023-10-13 21:32:14 +00:00
|
|
|
var emptyVersionChangelog = `## 2020-05-01`
|
2021-07-31 19:03:58 +00:00
|
|
|
|
2021-07-31 00:03:34 +00:00
|
|
|
var fullChangeLog = `## v1.0.0 (2020-05-01)
|
|
|
|
|
|
|
|
### Features
|
|
|
|
|
|
|
|
- subject text ()
|
|
|
|
|
|
|
|
### Bug Fixes
|
|
|
|
|
|
|
|
- subject text ()
|
|
|
|
|
|
|
|
### Build
|
|
|
|
|
|
|
|
- subject text ()
|
|
|
|
|
|
|
|
### Breaking Changes
|
|
|
|
|
2023-10-13 21:32:14 +00:00
|
|
|
- break change message`
|
2020-05-01 03:45:08 +00:00
|
|
|
|
2023-10-15 19:29:29 +00:00
|
|
|
func TestBaseOutputFormatter_FormatReleaseNote(t *testing.T) {
|
2020-05-01 03:45:08 +00:00
|
|
|
date, _ := time.Parse("2006-01-02", "2020-05-01")
|
|
|
|
|
|
|
|
tests := []struct {
|
2021-07-31 20:34:40 +00:00
|
|
|
name string
|
2023-10-15 19:29:29 +00:00
|
|
|
input sv.ReleaseNote
|
2021-07-31 20:34:40 +00:00
|
|
|
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},
|
2021-12-29 21:02:50 +00:00
|
|
|
{"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) {
|
2023-10-15 19:29:29 +00:00
|
|
|
got, err := NewOutputFormatter(tmpls).FormatReleaseNote(tt.input)
|
2021-07-31 20:34:40 +00:00
|
|
|
if got != tt.want {
|
2023-10-15 19:29:29 +00:00
|
|
|
t.Errorf("BaseOutputFormatter.FormatReleaseNote() = %v, want %v", got, tt.want)
|
2020-05-01 03:45:08 +00:00
|
|
|
}
|
2021-07-31 20:34:40 +00:00
|
|
|
|
|
|
|
if (err != nil) != tt.wantErr {
|
2023-10-15 19:29:29 +00:00
|
|
|
t.Errorf("BaseOutputFormatter.FormatReleaseNote() error = %v, wantErr %v", err, tt.wantErr)
|
2021-07-31 20:34:40 +00:00
|
|
|
}
|
2020-05-01 03:45:08 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-15 19:29:29 +00:00
|
|
|
func emptyReleaseNote(tag string, date time.Time) sv.ReleaseNote {
|
2021-12-29 21:02:50 +00:00
|
|
|
v, _ := semver.NewVersion(tag)
|
2023-10-12 14:18:25 +00:00
|
|
|
|
2023-10-15 19:29:29 +00:00
|
|
|
return sv.ReleaseNote{
|
2021-01-25 20:16:56 +00:00
|
|
|
Version: v,
|
2021-12-29 21:02:50 +00:00
|
|
|
Tag: tag,
|
2020-05-01 03:45:08 +00:00
|
|
|
Date: date,
|
|
|
|
}
|
|
|
|
}
|
2021-07-31 00:03:34 +00:00
|
|
|
|
2023-10-15 19:29:29 +00:00
|
|
|
func fullReleaseNote(tag string, date time.Time) sv.ReleaseNote {
|
2021-12-29 21:02:50 +00:00
|
|
|
v, _ := semver.NewVersion(tag)
|
2023-10-15 19:29:29 +00:00
|
|
|
sections := []sv.ReleaseNoteSection{
|
|
|
|
sv.TestNewReleaseNoteCommitsSection(
|
2023-10-12 14:18:25 +00:00
|
|
|
"Features",
|
|
|
|
[]string{"feat"},
|
2023-10-15 19:29:29 +00:00
|
|
|
[]sv.CommitLog{sv.TestCommitlog("feat", map[string]string{}, "a")},
|
2023-10-12 14:18:25 +00:00
|
|
|
),
|
2023-10-15 19:29:29 +00:00
|
|
|
sv.TestNewReleaseNoteCommitsSection(
|
2023-10-12 14:18:25 +00:00
|
|
|
"Bug Fixes",
|
|
|
|
[]string{"fix"},
|
2023-10-15 19:29:29 +00:00
|
|
|
[]sv.CommitLog{sv.TestCommitlog("fix", map[string]string{}, "a")},
|
2023-10-12 14:18:25 +00:00
|
|
|
),
|
2023-10-15 19:29:29 +00:00
|
|
|
sv.TestNewReleaseNoteCommitsSection(
|
2023-10-12 14:18:25 +00:00
|
|
|
"Build",
|
|
|
|
[]string{"build"},
|
2023-10-15 19:29:29 +00:00
|
|
|
[]sv.CommitLog{sv.TestCommitlog("build", map[string]string{}, "a")},
|
2023-10-12 14:18:25 +00:00
|
|
|
),
|
2023-10-15 19:29:29 +00:00
|
|
|
sv.ReleaseNoteBreakingChangeSection{Name: "Breaking Changes", Messages: []string{"break change message"}},
|
2021-07-31 00:03:34 +00:00
|
|
|
}
|
2023-10-12 14:18:25 +00:00
|
|
|
|
2023-10-15 19:29:29 +00:00
|
|
|
return sv.TestReleaseNote(v, tag, date, sections, map[string]struct{}{"a": {}})
|
2021-07-31 00:03:34 +00:00
|
|
|
}
|
2022-01-30 21:45:16 +00:00
|
|
|
|
|
|
|
func Test_checkTemplatesExecution(t *testing.T) {
|
2023-10-15 19:29:29 +00:00
|
|
|
tpls := NewOutputFormatter(tmpls).templates
|
2022-01-30 21:45:16 +00:00
|
|
|
tests := []struct {
|
|
|
|
template string
|
|
|
|
variables interface{}
|
|
|
|
}{
|
|
|
|
{"changelog-md.tpl", changelogVariables("v1.0.0", "v1.0.1")},
|
|
|
|
{"releasenotes-md.tpl", releaseNotesVariables("v1.0.0")},
|
|
|
|
}
|
2023-10-12 14:18:25 +00:00
|
|
|
|
2022-01-30 21:45:16 +00:00
|
|
|
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)
|
2023-10-12 14:18:25 +00:00
|
|
|
|
2022-01-30 21:45:16 +00:00
|
|
|
return
|
|
|
|
}
|
2023-10-12 14:18:25 +00:00
|
|
|
|
|
|
|
if len(b.Bytes()) == 0 {
|
2022-01-30 21:45:16 +00:00
|
|
|
t.Errorf("empty template")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-02-06 21:15:36 +00:00
|
|
|
|
|
|
|
func releaseNotesVariables(release string) releaseNoteTemplateVariables {
|
|
|
|
return releaseNoteTemplateVariables{
|
|
|
|
Release: release,
|
2023-10-12 14:18:25 +00:00
|
|
|
Date: time.Date(2006, 1, 0o2, 0, 0, 0, 0, time.UTC),
|
2023-10-15 19:29:29 +00:00
|
|
|
Sections: []sv.ReleaseNoteSection{
|
|
|
|
sv.TestNewReleaseNoteCommitsSection("Features",
|
2023-10-12 14:18:25 +00:00
|
|
|
[]string{"feat"},
|
2023-10-15 19:29:29 +00:00
|
|
|
[]sv.CommitLog{sv.TestCommitlog("feat", map[string]string{}, "a")},
|
2023-10-12 14:18:25 +00:00
|
|
|
),
|
2023-10-15 19:29:29 +00:00
|
|
|
sv.TestNewReleaseNoteCommitsSection("Bug Fixes",
|
2023-10-12 14:18:25 +00:00
|
|
|
[]string{"fix"},
|
2023-10-15 19:29:29 +00:00
|
|
|
[]sv.CommitLog{sv.TestCommitlog("fix", map[string]string{}, "a")},
|
2023-10-12 14:18:25 +00:00
|
|
|
),
|
2023-10-15 19:29:29 +00:00
|
|
|
sv.TestNewReleaseNoteCommitsSection("Build",
|
2023-10-12 14:18:25 +00:00
|
|
|
[]string{"build"},
|
2023-10-15 19:29:29 +00:00
|
|
|
[]sv.CommitLog{sv.TestCommitlog("build", map[string]string{}, "a")},
|
2023-10-12 14:18:25 +00:00
|
|
|
),
|
2023-10-15 19:29:29 +00:00
|
|
|
sv.ReleaseNoteBreakingChangeSection{Name: "Breaking Changes", Messages: []string{"break change message"}},
|
2022-02-06 21:15:36 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func changelogVariables(releases ...string) []releaseNoteTemplateVariables {
|
|
|
|
var variables []releaseNoteTemplateVariables
|
2023-10-12 14:18:25 +00:00
|
|
|
|
2022-02-06 21:15:36 +00:00
|
|
|
for _, r := range releases {
|
|
|
|
variables = append(variables, releaseNotesVariables(r))
|
|
|
|
}
|
|
|
|
|
2023-10-12 14:18:25 +00:00
|
|
|
return variables
|
2022-02-06 21:15:36 +00:00
|
|
|
}
|