From 4c97682ab858d6bbd26fc020e255cb339c9c8119 Mon Sep 17 00:00:00 2001 From: obfu5c8 <32260700+obfu5c8@users.noreply.github.com> Date: Mon, 5 Jun 2023 08:49:53 +0100 Subject: [PATCH] Add support for overriding template dir (#50) 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 --- cmd/gotestfmt/main.go | 40 ++++++++++++++++++++-------------------- gotestfmt.go | 13 ++++++++----- 2 files changed, 28 insertions(+), 25 deletions(-) diff --git a/cmd/gotestfmt/main.go b/cmd/gotestfmt/main.go index 1273870..2ed48f6 100644 --- a/cmd/gotestfmt/main.go +++ b/cmd/gotestfmt/main.go @@ -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 @@ -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 @@ -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", @@ -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, + "", + } } } } @@ -160,6 +159,7 @@ func main() { cfg.Formatter = formatter format, err := gotestfmt.New( + templateDir, dirs, ) if err != nil { diff --git a/gotestfmt.go b/gotestfmt.go index 7c3912f..bcb70db 100644 --- a/gotestfmt.go +++ b/gotestfmt.go @@ -17,11 +17,12 @@ 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, @@ -29,17 +30,17 @@ func New( }, 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 } @@ -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 @@ -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)