summaryrefslogtreecommitdiff
path: root/plugins_test.go
diff options
context:
space:
mode:
authorDave Henderson <dhenderson@gmail.com>2022-05-28 12:51:53 -0400
committerDave Henderson <dhenderson@gmail.com>2022-05-28 13:15:01 -0400
commit4a5d32b4fbf89388e687bdf8758fbeae81267679 (patch)
treedb931a656ce0435a5ac2d8661d7a7aef78066104 /plugins_test.go
parentaac83f2f276e113ce46d6c0ff66d6d71ef2d70f8 (diff)
PluginFunc to create custom template functions
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
Diffstat (limited to 'plugins_test.go')
-rw-r--r--plugins_test.go60
1 files changed, 50 insertions, 10 deletions
diff --git a/plugins_test.go b/plugins_test.go
index 1e00a908..d8442ecf 100644
--- a/plugins_test.go
+++ b/plugins_test.go
@@ -3,15 +3,15 @@ package gomplate
import (
"bytes"
"context"
+ "fmt"
+ "os"
"strings"
"testing"
"text/template"
"time"
- "gotest.tools/v3/assert"
- "gotest.tools/v3/assert/cmp"
-
"github.com/hairyhenderson/gomplate/v3/internal/config"
+ "github.com/stretchr/testify/assert"
)
func TestBindPlugins(t *testing.T) {
@@ -21,13 +21,13 @@ func TestBindPlugins(t *testing.T) {
Plugins: map[string]config.PluginConfig{},
}
err := bindPlugins(ctx, cfg, fm)
- assert.NilError(t, err)
- assert.DeepEqual(t, template.FuncMap{}, fm)
+ assert.NoError(t, err)
+ assert.EqualValues(t, template.FuncMap{}, fm)
cfg.Plugins = map[string]config.PluginConfig{"foo": {Cmd: "bar"}}
err = bindPlugins(ctx, cfg, fm)
- assert.NilError(t, err)
- assert.Check(t, cmp.Contains(fm, "foo"))
+ assert.NoError(t, err)
+ assert.Contains(t, fm, "foo")
err = bindPlugins(ctx, cfg, fm)
assert.ErrorContains(t, err, "already bound")
@@ -49,12 +49,11 @@ func TestBuildCommand(t *testing.T) {
for _, d := range data {
p := &plugin{
ctx: ctx,
- name: d.name,
path: d.path,
}
name, args := p.buildCommand(d.args)
actual := append([]string{name}, args...)
- assert.DeepEqual(t, d.expected, actual)
+ assert.EqualValues(t, d.expected, actual)
}
}
@@ -70,7 +69,48 @@ func TestRun(t *testing.T) {
path: "echo",
}
out, err := p.run("foo")
- assert.NilError(t, err)
+ assert.NoError(t, err)
assert.Equal(t, "", stderr.String())
assert.Equal(t, "foo", strings.TrimSpace(out.(string)))
}
+
+func ExamplePluginFunc() {
+ ctx := context.Background()
+
+ // PluginFunc creates a template function that runs an arbitrary command.
+ f := PluginFunc(ctx, "echo", PluginOpts{})
+
+ // The function can be used in a template, but here we'll just run it
+ // directly. This is equivalent to running 'echo foo bar'
+ out, err := f("foo", "bar")
+ if err != nil {
+ panic(err)
+ }
+ fmt.Println(out)
+
+ // Output:
+ // foo bar
+}
+
+func ExamplePluginFunc_with_template() {
+ ctx := context.Background()
+
+ f := PluginFunc(ctx, "echo", PluginOpts{})
+
+ // PluginFunc is intended for use with gomplate, but can be used in any
+ // text/template by adding it to the FuncMap.
+ tmpl := template.New("new").Funcs(template.FuncMap{"echo": f})
+
+ tmpl, err := tmpl.Parse(`{{ echo "baz" "qux" }}`)
+ if err != nil {
+ panic(err)
+ }
+
+ err = tmpl.Execute(os.Stdout, nil)
+ if err != nil {
+ panic(err)
+ }
+
+ // Output:
+ // baz qux
+}