Skip to content

Commit 4c97682

Browse files
obfu5c8engelmi
andauthored
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 <[email protected]>
1 parent b870aff commit 4c97682

File tree

2 files changed

+28
-25
lines changed

2 files changed

+28
-25
lines changed

cmd/gotestfmt/main.go

+20-20
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,10 @@ import (
1212
)
1313

1414
// ciEnvironments maps environment variables to directories to check for templates.
15-
var ciEnvironments = map[string][]string{
16-
"GITHUB_WORKFLOW": {
17-
"./.gotestfmt/github",
18-
"./.gotestfmt",
19-
},
20-
"TEAMCITY_VERSION": {
21-
"./.gotestfmt/teamcity",
22-
"./.gotestfmt",
23-
},
24-
"GITLAB_CI": {
25-
"./.gotestfmt/gitlab",
26-
"./.gotestfmt",
27-
},
15+
var ciEnvironments = map[string]string{
16+
"GITHUB_WORKFLOW": "github",
17+
"TEAMCITY_VERSION": "teamcity",
18+
"GITLAB_CI": "gitlab",
2819
}
2920

3021
type hide string
@@ -90,13 +81,12 @@ func hideDescription() string {
9081
}
9182

9283
func main() {
93-
dirs := []string{
94-
"./.gotestfmt",
95-
}
84+
dirs := []string{""}
9685
ci := ""
9786
inputFile := "-"
9887
formatter := ""
9988
hide := ""
89+
templateDir := "./.gotestfmt"
10090
var nofail bool
10191
var showTestStatus bool
10292

@@ -130,6 +120,12 @@ func main() {
130120
formatter,
131121
"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.",
132122
)
123+
flag.StringVar(
124+
&templateDir,
125+
"template-dir",
126+
templateDir,
127+
"Absolute path to a folder containing templates",
128+
)
133129
flag.BoolVar(
134130
&nofail,
135131
"nofail",
@@ -140,13 +136,16 @@ func main() {
140136

141137
if ci != "" {
142138
dirs = []string{
143-
fmt.Sprintf("./.gotestfmt/%s", filepath.Clean(ci)),
144-
"./.gotestfmt",
139+
filepath.Clean(ci),
140+
"",
145141
}
146142
} else {
147-
for env, directories := range ciEnvironments {
143+
for env, subDir := range ciEnvironments {
148144
if os.Getenv(env) != "" {
149-
dirs = directories
145+
dirs = []string{
146+
subDir,
147+
"",
148+
}
150149
}
151150
}
152151
}
@@ -160,6 +159,7 @@ func main() {
160159
cfg.Formatter = formatter
161160

162161
format, err := gotestfmt.New(
162+
templateDir,
163163
dirs,
164164
)
165165
if err != nil {

gotestfmt.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,30 @@ import (
1717
var fs embed.FS
1818

1919
func New(
20+
templateRoot string,
2021
templateDirs []string,
2122
) (CombinedExitCode, error) {
22-
downloadsTpl := findTemplate(templateDirs, "downloads.gotpl")
23+
downloadsTpl := findTemplate(templateRoot, templateDirs, "downloads.gotpl")
2324

24-
packageTpl := findTemplate(templateDirs, "package.gotpl")
25+
packageTpl := findTemplate(templateRoot, templateDirs, "package.gotpl")
2526

2627
return &goTestFmt{
2728
downloadsTpl: downloadsTpl,
2829
packageTpl: packageTpl,
2930
}, nil
3031
}
3132

32-
func findTemplate(dirs []string, tpl string) []byte {
33+
func findTemplate(root string, dirs []string, tpl string) []byte {
3334
var lastError error
3435
for _, dir := range dirs {
35-
templateContents, err := os.ReadFile(path.Join(dir, tpl))
36+
templateContents, err := os.ReadFile(path.Join(root, dir, tpl))
3637
if err == nil {
3738
return templateContents
3839
}
3940
lastError = err
4041
}
4142
for _, dir := range dirs {
42-
templateContents, err := fs.ReadFile(path.Join(dir, tpl))
43+
templateContents, err := fs.ReadFile(path.Join("./.gotestfmt", dir, tpl))
4344
if err == nil {
4445
return templateContents
4546
}
@@ -49,6 +50,7 @@ func findTemplate(dirs []string, tpl string) []byte {
4950
}
5051

5152
// Combined is an interface that combines both the classic GoTestFmt interface and the Formatter interface.
53+
//
5254
//goland:noinspection GoDeprecation
5355
type Combined interface {
5456
GoTestFmt
@@ -64,6 +66,7 @@ type CombinedExitCode interface {
6466
// GoTestFmt implements the classic Format instruction. This is no longer in use.
6567
//
6668
// Deprecated: please use the Formatter interface instead.
69+
//
6770
//goland:noinspection GoDeprecation
6871
type GoTestFmt interface {
6972
Format(input io.Reader, target io.WriteCloser)

0 commit comments

Comments
 (0)