Skip to content

Commit

Permalink
Merge pull request #4 from bvieira/ignore-missing-date
Browse files Browse the repository at this point in the history
Feature: tags without date
  • Loading branch information
bvieira authored May 1, 2020
2 parents 67da4c9 + 37c630d commit c3a35e4
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 6 deletions.
8 changes: 6 additions & 2 deletions sv/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down Expand Up @@ -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,
}
Expand Down
40 changes: 40 additions & 0 deletions sv/formatter_test.go
Original file line number Diff line number Diff line change
@@ -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,
}
}
5 changes: 1 addition & 4 deletions sv/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
}
}
Expand Down
39 changes: 39 additions & 0 deletions sv/git_test.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit c3a35e4

Please sign in to comment.