Skip to content

Commit 13d7486

Browse files
committed
Add templating function 'joinList'
1 parent ecffcc7 commit 13d7486

3 files changed

Lines changed: 75 additions & 1 deletion

File tree

internal/templater/funcs.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func init() {
3333
"splitArgs": splitArgs,
3434
"IsSH": IsSH, // Deprecated
3535
"joinPath": filepath.Join,
36+
"joinPathList": joinPathList,
3637
"relPath": filepath.Rel,
3738
"absPath": filepath.Abs,
3839
"merge": merge,
@@ -131,3 +132,7 @@ func mustToYaml(v any) (string, error) {
131132
}
132133
return string(output), nil
133134
}
135+
136+
func joinPathList(paths ...string) string {
137+
return strings.Join(paths, string(filepath.ListSeparator))
138+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package templater
2+
3+
import (
4+
"runtime"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
9+
10+
"github.com/go-task/task/v3/taskfile/ast"
11+
)
12+
13+
func TestTemplateFuncs(t *testing.T) {
14+
t.Parallel()
15+
16+
tests := []struct {
17+
name string
18+
template string
19+
expected string
20+
}{
21+
{
22+
name: "joinPath",
23+
template: `{{ joinPath .BaseDir "dir" "file.txt" }}`,
24+
expected: func() string {
25+
switch os := runtime.GOOS; os {
26+
case "windows":
27+
return "base\\dir\\file.txt"
28+
default:
29+
return "base/dir/file.txt"
30+
}
31+
}(),
32+
},
33+
{
34+
name: "joinPath with single argument",
35+
template: `{{ joinPath "dir1" }}`,
36+
expected: "dir1",
37+
},
38+
{
39+
name: "joinPathList",
40+
template: `{{ joinPathList .BaseDir "subdir" "file.txt" }}`,
41+
expected: func() string {
42+
switch os := runtime.GOOS; os {
43+
case "windows":
44+
return "base;subdir;file.txt"
45+
default:
46+
return "base:subdir:file.txt"
47+
}
48+
}(),
49+
},
50+
}
51+
52+
for _, tc := range tests {
53+
t.Run(tc.name, func(t *testing.T) {
54+
t.Parallel()
55+
vars := ast.NewVars(
56+
&ast.VarElement{
57+
Key: "BaseDir",
58+
Value: ast.Var{Value: "base"},
59+
},
60+
)
61+
cache := &Cache{Vars: vars}
62+
cache.ResetCache()
63+
result := Replace(tc.template, cache)
64+
require.NoError(t, cache.Err())
65+
assert.Equal(t, tc.expected, result)
66+
})
67+
}
68+
}

website/src/docs/reference/templating.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,8 @@ tasks:
616616
- echo "{{.WIN_PATH | toSlash}}" # Convert to forward slashes
617617
- echo "{{.WIN_PATH | fromSlash}}" # Convert to OS-specific slashes
618618
- echo "{{joinPath .OUTPUT_DIR .BINARY_NAME}}" # Join path elements
619-
- echo "Relative {{relPath .ROOT_DIR .TASKFILE_DIR}}" # Get relative path
619+
- echo "{{joinPathList .PATH_1 .PATH_2 .PATH_3}}" # Join a list of path elements using the OS List Separator
620+
- echo "Relative {{relPath .ROOT_DIR .TASKFILE_DIR}}" # Get relative path
620621
- echo '{{absPath "../sibling"}}' # Resolve to an absolute path
621622
```
622623

0 commit comments

Comments
 (0)