forked from 1Password/shell-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
provisioner_test_helper.go
61 lines (47 loc) · 1.58 KB
/
provisioner_test_helper.go
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
package plugintest
import (
"context"
"fmt"
"testing"
"github.com/1Password/shell-plugins/sdk"
"github.com/stretchr/testify/assert"
)
// TestProvisioner will invoke the specified provisioner with the item fields specified in each test case, comparing
// the provisioner output with the specified expected output.
func TestProvisioner(t *testing.T, provisioner sdk.Provisioner, cases map[string]ProvisionCase) {
t.Helper()
for name, c := range cases {
t.Run(name, func(t *testing.T) {
t.Helper()
if c.ExpectedOutput.Environment == nil {
c.ExpectedOutput.Environment = make(map[string]string)
}
if c.ExpectedOutput.Files == nil {
c.ExpectedOutput.Files = make(map[string]sdk.OutputFile)
}
ctx := context.Background()
in := sdk.ProvisionInput{
ItemFields: c.ItemFields,
HomeDir: "~",
TempDir: "/tmp",
}
out := sdk.ProvisionOutput{
Environment: make(map[string]string),
Files: make(map[string]sdk.OutputFile),
CommandLine: c.CommandLine,
}
provisioner.Provision(ctx, in, &out)
description := fmt.Sprintf("Provision: %s", name)
assert.Equal(t, c.ExpectedOutput, out, description)
})
}
}
type ProvisionCase struct {
// ItemFields can be used to populate the item fields to pass to the provisioner.
ItemFields map[sdk.FieldName]string
// CommandLine can be used to populate the command line to pass to the provisioner.
CommandLine []string
// ExpectedOutput can be used to set the exact expected provision output, which contains the
// environment, files, and command line.
ExpectedOutput sdk.ProvisionOutput
}