summaryrefslogtreecommitdiff
path: root/plugins_test.go
blob: 1e00a908fd63c19d1183bf13989aaf8b046c103c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package gomplate

import (
	"bytes"
	"context"
	"strings"
	"testing"
	"text/template"
	"time"

	"gotest.tools/v3/assert"
	"gotest.tools/v3/assert/cmp"

	"github.com/hairyhenderson/gomplate/v3/internal/config"
)

func TestBindPlugins(t *testing.T) {
	ctx := context.Background()
	fm := template.FuncMap{}
	cfg := &config.Config{
		Plugins: map[string]config.PluginConfig{},
	}
	err := bindPlugins(ctx, cfg, fm)
	assert.NilError(t, err)
	assert.DeepEqual(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"))

	err = bindPlugins(ctx, cfg, fm)
	assert.ErrorContains(t, err, "already bound")
}

func TestBuildCommand(t *testing.T) {
	ctx := context.Background()
	data := []struct {
		name, path string
		args       []string
		expected   []string
	}{
		{"foo", "foo", nil, []string{"foo"}},
		{"foo", "foo", []string{"bar"}, []string{"foo", "bar"}},
		{"foo", "foo.bat", nil, []string{"cmd.exe", "/c", "foo.bat"}},
		{"foo", "foo.cmd", []string{"bar"}, []string{"cmd.exe", "/c", "foo.cmd", "bar"}},
		{"foo", "foo.ps1", []string{"bar", "baz"}, []string{"pwsh", "-File", "foo.ps1", "bar", "baz"}},
	}
	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)
	}
}

func TestRun(t *testing.T) {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	stderr := &bytes.Buffer{}
	p := &plugin{
		ctx:     ctx,
		timeout: 500 * time.Millisecond,
		stderr:  stderr,
		path:    "echo",
	}
	out, err := p.run("foo")
	assert.NilError(t, err)
	assert.Equal(t, "", stderr.String())
	assert.Equal(t, "foo", strings.TrimSpace(out.(string)))
}