0
0
mirror of https://github.com/thegeeklab/git-sv.git synced 2024-11-24 21:20:40 +00:00

refactor: fix errcheck lint

This commit is contained in:
Beatriz Vieira 2021-07-31 17:34:40 -03:00
parent 7b3df4e245
commit fec91a1947
3 changed files with 41 additions and 20 deletions

View File

@ -148,8 +148,11 @@ func commitNotesHandler(git sv.Git, rnProcessor sv.ReleaseNoteProcessor, outputF
date, _ = time.Parse("2006-01-02", commits[0].Date)
}
releasenote := rnProcessor.Create(nil, date, commits)
fmt.Println(outputFormatter.FormatReleaseNote(releasenote))
output, err := outputFormatter.FormatReleaseNote(rnProcessor.Create(nil, date, commits))
if err != nil {
return fmt.Errorf("could not format release notes, message: %v", err)
}
fmt.Println(output)
return nil
}
}
@ -173,7 +176,11 @@ func releaseNotesHandler(git sv.Git, semverProcessor sv.SemVerCommitsProcessor,
}
releasenote := rnProcessor.Create(&rnVersion, date, commits)
fmt.Println(outputFormatter.FormatReleaseNote(releasenote))
output, err := outputFormatter.FormatReleaseNote(releasenote)
if err != nil {
return fmt.Errorf("could not format release notes, message: %v", err)
}
fmt.Println(output)
return nil
}
}
@ -442,7 +449,11 @@ func changelogHandler(git sv.Git, semverProcessor sv.SemVerCommitsProcessor, rnP
releaseNotes = append(releaseNotes, rnProcessor.Create(&currentVer, tag.Date, commits))
}
fmt.Println(formatter.FormatChangelog(releaseNotes))
output, err := formatter.FormatChangelog(releaseNotes)
if err != nil {
return fmt.Errorf("could not format changelog, message: %v", err)
}
fmt.Println(output)
return nil
}

View File

@ -51,8 +51,8 @@ const (
// OutputFormatter output formatter interface.
type OutputFormatter interface {
FormatReleaseNote(releasenote ReleaseNote) string
FormatChangelog(releasenotes []ReleaseNote) string
FormatReleaseNote(releasenote ReleaseNote) (string, error)
FormatChangelog(releasenotes []ReleaseNote) (string, error)
}
// OutputFormatterImpl formater for release note and changelog.
@ -72,22 +72,26 @@ func NewOutputFormatter() *OutputFormatterImpl {
}
// FormatReleaseNote format a release note.
func (p OutputFormatterImpl) FormatReleaseNote(releasenote ReleaseNote) string {
func (p OutputFormatterImpl) FormatReleaseNote(releasenote ReleaseNote) (string, error) {
var b bytes.Buffer
p.releasenoteTemplate.Execute(&b, releaseNoteVariables(releasenote))
return b.String()
if err := p.releasenoteTemplate.Execute(&b, releaseNoteVariables(releasenote)); err != nil {
return "", err
}
return b.String(), nil
}
// FormatChangelog format a changelog.
func (p OutputFormatterImpl) FormatChangelog(releasenotes []ReleaseNote) string {
func (p OutputFormatterImpl) FormatChangelog(releasenotes []ReleaseNote) (string, error) {
templateVars := make([]releaseNoteTemplateVariables, len(releasenotes))
for i, v := range releasenotes {
templateVars[i] = releaseNoteVariables(v)
}
var b bytes.Buffer
p.changelogTemplate.Execute(&b, templateVars)
return b.String()
if err := p.changelogTemplate.Execute(&b, templateVars); err != nil {
return "", err
}
return b.String(), nil
}
func releaseNoteVariables(releasenote ReleaseNote) releaseNoteTemplateVariables {

View File

@ -42,17 +42,23 @@ func TestOutputFormatterImpl_FormatReleaseNote(t *testing.T) {
name string
input ReleaseNote
want string
wantErr bool
}{
{"with date", emptyReleaseNote("1.0.0", date.Truncate(time.Minute)), dateChangelog},
{"without date", emptyReleaseNote("1.0.0", time.Time{}.Truncate(time.Minute)), emptyDateChangelog},
{"without version", emptyReleaseNote("", date.Truncate(time.Minute)), emptyVersionChangelog},
{"full changelog", fullReleaseNote("1.0.0", date.Truncate(time.Minute)), fullChangeLog},
{"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},
{"full changelog", fullReleaseNote("1.0.0", date.Truncate(time.Minute)), fullChangeLog, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewOutputFormatter().FormatReleaseNote(tt.input); got != tt.want {
got, err := NewOutputFormatter().FormatReleaseNote(tt.input)
if got != tt.want {
t.Errorf("OutputFormatterImpl.FormatReleaseNote() = %v, want %v", got, tt.want)
}
if (err != nil) != tt.wantErr {
t.Errorf("OutputFormatterImpl.FormatReleaseNote() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}