diff --git a/sv/formatter.go b/sv/formatter.go index 74860db..8e83d76 100644 --- a/sv/formatter.go +++ b/sv/formatter.go @@ -40,7 +40,7 @@ const ( {{- end}} {{- end}}` - rnTemplate = `## v{{.Version}} ({{.Date}}) + rnTemplate = `## v{{.Version}}{{if .Date}} ({{.Date}}){{end}} {{- template "rnSection" .Sections.feat}} {{- template "rnSection" .Sections.fix}} {{- template "rnSectionBreakingChanges" .BreakingChanges}} @@ -89,9 +89,13 @@ func (p OutputFormatterImpl) FormatChangelog(releasenotes []ReleaseNote) string } func releaseNoteVariables(releasenote ReleaseNote) releaseNoteTemplateVariables { + var date = "" + if !releasenote.Date.IsZero() { + date = releasenote.Date.Format("2006-01-02") + } return releaseNoteTemplateVariables{ Version: fmt.Sprintf("%d.%d.%d", releasenote.Version.Major(), releasenote.Version.Minor(), releasenote.Version.Patch()), - Date: releasenote.Date.Format("2006-01-02"), + Date: date, Sections: releasenote.Sections, BreakingChanges: releasenote.BreakingChanges, } diff --git a/sv/formatter_test.go b/sv/formatter_test.go new file mode 100644 index 0000000..d60c7f9 --- /dev/null +++ b/sv/formatter_test.go @@ -0,0 +1,40 @@ +package sv + +import ( + "testing" + "time" + + "github.com/Masterminds/semver" +) + +var dateChangelog = `## v1.0.0 (2020-05-01) +` +var emptyDateChangelog = `## v1.0.0 +` + +func TestOutputFormatterImpl_FormatReleaseNote(t *testing.T) { + date, _ := time.Parse("2006-01-02", "2020-05-01") + + tests := []struct { + name string + input ReleaseNote + want string + }{ + {"", emptyReleaseNote("1.0.0", date.Truncate(time.Minute)), dateChangelog}, + {"", emptyReleaseNote("1.0.0", time.Time{}.Truncate(time.Minute)), emptyDateChangelog}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := NewOutputFormatter().FormatReleaseNote(tt.input); got != tt.want { + t.Errorf("OutputFormatterImpl.FormatReleaseNote() = %v, want %v", got, tt.want) + } + }) + } +} + +func emptyReleaseNote(version string, date time.Time) ReleaseNote { + return ReleaseNote{ + Version: *semver.MustParse(version), + Date: date, + } +} diff --git a/sv/git.go b/sv/git.go index b126b31..fd5193a 100644 --- a/sv/git.go +++ b/sv/git.go @@ -121,10 +121,7 @@ func parseTagsOutput(input string) ([]GitTag, error) { for scanner.Scan() { if line := strings.TrimSpace(scanner.Text()); line != "" { values := strings.Split(line, "#") - date, err := time.Parse("2006-01-02 15:04:05 -0700", values[0]) - if err != nil { - return nil, fmt.Errorf("failed to parse tag data, message: %v", err) - } + date, _ := time.Parse("2006-01-02 15:04:05 -0700", values[0]) // ignore invalid dates result = append(result, GitTag{Name: values[1], Date: date}) } } diff --git a/sv/git_test.go b/sv/git_test.go new file mode 100644 index 0000000..7b96394 --- /dev/null +++ b/sv/git_test.go @@ -0,0 +1,39 @@ +package sv + +import ( + "reflect" + "testing" + "time" +) + +func Test_parseTagsOutput(t *testing.T) { + tests := []struct { + name string + input string + want []GitTag + wantErr bool + }{ + {"with date", "2020-05-01 18:00:00 -0300#1.0.0", []GitTag{GitTag{Name: "1.0.0", Date: date("2020-05-01 18:00:00 -0300")}}, false}, + {"without date", "#1.0.0", []GitTag{GitTag{Name: "1.0.0", Date: time.Time{}}}, false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := parseTagsOutput(tt.input) + if (err != nil) != tt.wantErr { + t.Errorf("parseTagsOutput() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("parseTagsOutput() = %v, want %v", got, tt.want) + } + }) + } +} + +func date(input string) time.Time { + t, err := time.Parse("2006-01-02 15:04:05 -0700", input) + if err != nil { + panic(err) + } + return t +}