Skip to content

Commit

Permalink
Add support for overriding template dir (#50)
Browse files Browse the repository at this point in the history
Add optional '-template-dir' flag to 'gotestfmt' cli command that allows overriding of the default template lookup path. Defaults to './.gotestfmt' to maintain backwards compatability.
Simplify ciEnvironments mapping to easier support dynamic root template folder.
Update 'findTemplate' function to allow passing in a root directory path that is used for os filesystem lookups, but not for embed fs lookups.

Co-authored-by: Michael Engel <[email protected]>
  • Loading branch information
obfu5c8 and engelmi committed Jun 5, 2023
1 parent b870aff commit 4c97682
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 25 deletions.
40 changes: 20 additions & 20 deletions cmd/gotestfmt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,10 @@ import (
)

// ciEnvironments maps environment variables to directories to check for templates.
var ciEnvironments = map[string][]string{
"GITHUB_WORKFLOW": {
"./.gotestfmt/github",
"./.gotestfmt",
},
"TEAMCITY_VERSION": {
"./.gotestfmt/teamcity",
"./.gotestfmt",
},
"GITLAB_CI": {
"./.gotestfmt/gitlab",
"./.gotestfmt",
},
var ciEnvironments = map[string]string{
"GITHUB_WORKFLOW": "github",
"TEAMCITY_VERSION": "teamcity",
"GITLAB_CI": "gitlab",
}

type hide string
Expand Down Expand Up @@ -90,13 +81,12 @@ func hideDescription() string {
}

func main() {
dirs := []string{
"./.gotestfmt",
}
dirs := []string{""}
ci := ""
inputFile := "-"
formatter := ""
hide := ""
templateDir := "./.gotestfmt"
var nofail bool
var showTestStatus bool

Expand Down Expand Up @@ -130,6 +120,12 @@ func main() {
formatter,
"Absolute path to an external program to format individual test output. This program will be called for each test case with a non-empty output and receive the test case output on stdin. It must produce the final output on stdout.",
)
flag.StringVar(
&templateDir,
"template-dir",
templateDir,
"Absolute path to a folder containing templates",
)
flag.BoolVar(
&nofail,
"nofail",
Expand All @@ -140,13 +136,16 @@ func main() {

if ci != "" {
dirs = []string{
fmt.Sprintf("./.gotestfmt/%s", filepath.Clean(ci)),
"./.gotestfmt",
filepath.Clean(ci),
"",
}
} else {
for env, directories := range ciEnvironments {
for env, subDir := range ciEnvironments {
if os.Getenv(env) != "" {
dirs = directories
dirs = []string{
subDir,
"",
}
}
}
}
Expand All @@ -160,6 +159,7 @@ func main() {
cfg.Formatter = formatter

format, err := gotestfmt.New(
templateDir,
dirs,
)
if err != nil {
Expand Down
13 changes: 8 additions & 5 deletions gotestfmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,30 @@ import (
var fs embed.FS

func New(
templateRoot string,
templateDirs []string,
) (CombinedExitCode, error) {
downloadsTpl := findTemplate(templateDirs, "downloads.gotpl")
downloadsTpl := findTemplate(templateRoot, templateDirs, "downloads.gotpl")

packageTpl := findTemplate(templateDirs, "package.gotpl")
packageTpl := findTemplate(templateRoot, templateDirs, "package.gotpl")

return &goTestFmt{
downloadsTpl: downloadsTpl,
packageTpl: packageTpl,
}, nil
}

func findTemplate(dirs []string, tpl string) []byte {
func findTemplate(root string, dirs []string, tpl string) []byte {
var lastError error
for _, dir := range dirs {
templateContents, err := os.ReadFile(path.Join(dir, tpl))
templateContents, err := os.ReadFile(path.Join(root, dir, tpl))
if err == nil {
return templateContents
}
lastError = err
}
for _, dir := range dirs {
templateContents, err := fs.ReadFile(path.Join(dir, tpl))
templateContents, err := fs.ReadFile(path.Join("./.gotestfmt", dir, tpl))
if err == nil {
return templateContents
}
Expand All @@ -49,6 +50,7 @@ func findTemplate(dirs []string, tpl string) []byte {
}

// Combined is an interface that combines both the classic GoTestFmt interface and the Formatter interface.
//
//goland:noinspection GoDeprecation
type Combined interface {
GoTestFmt
Expand All @@ -64,6 +66,7 @@ type CombinedExitCode interface {
// GoTestFmt implements the classic Format instruction. This is no longer in use.
//
// Deprecated: please use the Formatter interface instead.
//
//goland:noinspection GoDeprecation
type GoTestFmt interface {
Format(input io.Reader, target io.WriteCloser)
Expand Down

0 comments on commit 4c97682

Please sign in to comment.