0
0
mirror of https://github.com/thegeeklab/git-sv.git synced 2024-11-14 07:40:40 +00:00

refactor: move templates from sv to cmd/git-sv

issue: #40
This commit is contained in:
Beatriz Vieira 2022-02-06 18:15:36 -03:00
parent b5d9f9c535
commit 0230b1e00f
9 changed files with 70 additions and 56 deletions

View File

@ -1,6 +1,8 @@
package main package main
import ( import (
"embed"
"io/fs"
"log" "log"
"os" "os"
"path/filepath" "path/filepath"
@ -17,6 +19,16 @@ const (
repoConfigFilename = ".sv4git.yml" repoConfigFilename = ".sv4git.yml"
) )
var (
//go:embed resources/templates/*.tpl
defaultTemplatesFS embed.FS
)
func templateFS() fs.FS {
defaultTemplatesFS, _ := fs.Sub(defaultTemplatesFS, "resources/templates")
return defaultTemplatesFS
}
func main() { func main() {
log.SetFlags(0) log.SetFlags(0)
@ -25,7 +37,7 @@ func main() {
git := sv.NewGit(messageProcessor, cfg.Tag) git := sv.NewGit(messageProcessor, cfg.Tag)
semverProcessor := sv.NewSemVerCommitsProcessor(cfg.Versioning, cfg.CommitMessage) semverProcessor := sv.NewSemVerCommitsProcessor(cfg.Versioning, cfg.CommitMessage)
releasenotesProcessor := sv.NewReleaseNoteProcessor(cfg.ReleaseNotes) releasenotesProcessor := sv.NewReleaseNoteProcessor(cfg.ReleaseNotes)
outputFormatter := sv.NewOutputFormatter() outputFormatter := sv.NewOutputFormatter(templateFS())
app := cli.NewApp() app := cli.NewApp()
app.Name = "sv" app.Name = "sv"
@ -146,10 +158,9 @@ func main() {
} }
func loadCfg() Config { func loadCfg() Config {
envCfg := loadEnvConfig()
cfg := defaultConfig() cfg := defaultConfig()
envCfg := loadEnvConfig()
if envCfg.Home != "" { if envCfg.Home != "" {
if homeCfg, err := readConfig(filepath.Join(envCfg.Home, configFilename)); err == nil { if homeCfg, err := readConfig(filepath.Join(envCfg.Home, configFilename)); err == nil {
if merr := merge(&cfg, homeCfg); merr != nil { if merr := merge(&cfg, homeCfg); merr != nil {
@ -160,7 +171,7 @@ func loadCfg() Config {
repoPath, rerr := getRepoPath() repoPath, rerr := getRepoPath()
if rerr != nil { if rerr != nil {
log.Fatal("failed to get repository path, error: ", rerr) log.Fatal("failed to discovery repository top level, error: ", rerr)
} }
if repoCfg, err := readConfig(filepath.Join(repoPath, repoConfigFilename)); err == nil { if repoCfg, err := readConfig(filepath.Join(repoPath, repoConfigFilename)); err == nil {

View File

@ -0,0 +1,24 @@
package main
import (
"testing"
)
func Test_checkTemplatesFiles(t *testing.T) {
tests := []string{
"resources/templates/changelog-md.tpl",
"resources/templates/releasenotes-md.tpl",
}
for _, tt := range tests {
t.Run(tt, func(t *testing.T) {
got, err := defaultTemplatesFS.ReadFile(tt)
if err != nil {
t.Errorf("missing template error = %v", err)
return
}
if len(got) <= 0 {
t.Errorf("empty template")
}
})
}
}

View File

@ -2,7 +2,7 @@ package sv
import ( import (
"bytes" "bytes"
"embed" "io/fs"
"text/template" "text/template"
) )
@ -14,11 +14,6 @@ type releaseNoteTemplateVariables struct {
BreakingChanges BreakingChangeSection BreakingChanges BreakingChangeSection
} }
var (
//go:embed resources/templates/*
defaultTemplatesFS embed.FS
)
// OutputFormatter output formatter interface. // OutputFormatter output formatter interface.
type OutputFormatter interface { type OutputFormatter interface {
FormatReleaseNote(releasenote ReleaseNote) (string, error) FormatReleaseNote(releasenote ReleaseNote) (string, error)
@ -31,8 +26,8 @@ type OutputFormatterImpl struct {
} }
// NewOutputFormatter TemplateProcessor constructor. // NewOutputFormatter TemplateProcessor constructor.
func NewOutputFormatter() *OutputFormatterImpl { func NewOutputFormatter(templatesFS fs.FS) *OutputFormatterImpl {
tpls := template.Must(template.New("templates").ParseFS(defaultTemplatesFS, "resources/templates/*")) tpls := template.Must(template.New("templates").ParseFS(templatesFS, "*"))
return &OutputFormatterImpl{templates: tpls} return &OutputFormatterImpl{templates: tpls}
} }

View File

@ -2,6 +2,7 @@ package sv
import ( import (
"bytes" "bytes"
"os"
"testing" "testing"
"text/template" "text/template"
"time" "time"
@ -9,6 +10,8 @@ import (
"github.com/Masterminds/semver/v3" "github.com/Masterminds/semver/v3"
) )
var templatesFS = os.DirFS("../cmd/git-sv/resources/templates")
var dateChangelog = `## v1.0.0 (2020-05-01) var dateChangelog = `## v1.0.0 (2020-05-01)
` `
@ -57,7 +60,7 @@ func TestOutputFormatterImpl_FormatReleaseNote(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
got, err := NewOutputFormatter().FormatReleaseNote(tt.input) got, err := NewOutputFormatter(templatesFS).FormatReleaseNote(tt.input)
if got != tt.want { if got != tt.want {
t.Errorf("OutputFormatterImpl.FormatReleaseNote() = %v, want %v", got, tt.want) t.Errorf("OutputFormatterImpl.FormatReleaseNote() = %v, want %v", got, tt.want)
} }
@ -88,6 +91,30 @@ func fullReleaseNote(tag string, date time.Time) ReleaseNote {
return releaseNote(v, tag, date, sections, []string{"break change message"}) return releaseNote(v, tag, date, sections, []string{"break change message"})
} }
func Test_checkTemplatesExecution(t *testing.T) {
tpls := template.Must(template.New("templates").ParseFS(templatesFS, "*"))
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 { func releaseNotesVariables(release string) releaseNoteTemplateVariables {
return releaseNoteTemplateVariables{ return releaseNoteTemplateVariables{
Release: release, Release: release,
@ -110,46 +137,3 @@ func changelogVariables(releases ...string) []releaseNoteTemplateVariables {
return variables return variables
} }
func Test_checkTemplatesFiles(t *testing.T) {
tests := []string{
"resources/templates/changelog-md.tpl",
"resources/templates/releasenotes-md.tpl",
}
for _, tt := range tests {
t.Run(tt, func(t *testing.T) {
got, err := defaultTemplatesFS.ReadFile(tt)
if err != nil {
t.Errorf("missing template error = %v", err)
return
}
if len(got) <= 0 {
t.Errorf("empty template")
}
})
}
}
func Test_checkTemplatesExecution(t *testing.T) {
tpls := template.Must(template.New("templates").ParseFS(defaultTemplatesFS, "resources/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")
}
})
}
}