|
| 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 | +} |
0 commit comments