From 8e97e98afb586d65f8dc5e51e4a5cac20d27f362 Mon Sep 17 00:00:00 2001 From: Sergio Date: Mon, 9 Mar 2026 07:48:47 -0700 Subject: [PATCH] templater: add joinEnv helper for PATH-like vars --- internal/templater/funcs.go | 6 ++++++ internal/templater/funcs_test.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 internal/templater/funcs_test.go diff --git a/internal/templater/funcs.go b/internal/templater/funcs.go index 7bffae31a6..138a83e999 100644 --- a/internal/templater/funcs.go +++ b/internal/templater/funcs.go @@ -1,6 +1,7 @@ package templater import ( + stdos "os" "maps" "math/rand/v2" "path/filepath" @@ -33,6 +34,7 @@ func init() { "splitArgs": splitArgs, "IsSH": IsSH, // Deprecated "joinPath": filepath.Join, + "joinEnv": joinEnv, "relPath": filepath.Rel, "merge": merge, "spew": spew.Sdump, @@ -89,6 +91,10 @@ func splitArgs(s string) ([]string, error) { return shell.Fields(s, nil) } +func joinEnv(elems ...string) string { + return strings.Join(elems, string(stdos.PathListSeparator)) +} + // Deprecated: now always returns true func IsSH() bool { return true diff --git a/internal/templater/funcs_test.go b/internal/templater/funcs_test.go new file mode 100644 index 0000000000..85ea234852 --- /dev/null +++ b/internal/templater/funcs_test.go @@ -0,0 +1,31 @@ +package templater + +import ( + stdos "os" + "testing" +) + +func TestJoinEnv(t *testing.T) { + t.Parallel() + + got := joinEnv("/tmp/tools/bin", "/usr/local/bin", "/usr/bin") + want := "/tmp/tools/bin" + string(stdos.PathListSeparator) + "/usr/local/bin" + string(stdos.PathListSeparator) + "/usr/bin" + if got != want { + t.Fatalf("joinEnv() = %q, want %q", got, want) + } +} + +func TestJoinEnvTemplateFunc(t *testing.T) { + t.Parallel() + + joinEnvFunc, ok := templateFuncs["joinEnv"].(func(...string) string) + if !ok { + t.Fatalf("joinEnv template function has unexpected type: %T", templateFuncs["joinEnv"]) + } + + got := joinEnvFunc("a", "b") + want := "a" + string(stdos.PathListSeparator) + "b" + if got != want { + t.Fatalf("template joinEnv() = %q, want %q", got, want) + } +}