-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathscript_test.go
75 lines (68 loc) · 1.58 KB
/
script_test.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package hive_test
import (
"bufio"
"bytes"
"context"
"strings"
"testing"
"github.com/cilium/hive"
"github.com/cilium/hive/cell"
"github.com/cilium/hive/hivetest"
"github.com/cilium/hive/script"
"github.com/stretchr/testify/require"
)
func exampleCmd() hive.ScriptCmdOut {
return hive.NewScriptCmd(
"example1",
script.Command(
script.CmdUsage{
Summary: "Example command",
},
func(s *script.State, args ...string) (script.WaitFunc, error) {
s.Logf("hello1")
return nil, nil
},
),
)
}
func example2Cmd() hive.ScriptCmdsOut {
return hive.NewScriptCmds(
map[string]script.Cmd{
"example2": script.Command(
script.CmdUsage{
Summary: "Second example command",
},
func(s *script.State, args ...string) (script.WaitFunc, error) {
s.Logf("hello2")
return nil, nil
},
),
},
)
}
func TestScriptCommands(t *testing.T) {
h := hive.New(
cell.Provide(exampleCmd, example2Cmd),
)
cmds, err := h.ScriptCommands(hivetest.Logger(t))
require.NoError(t, err, "ScriptCommands")
e := script.Engine{
Cmds: cmds,
}
s, err := script.NewState(context.TODO(), "/tmp", nil)
require.NoError(t, err, "NewState")
script := `
hive start
example1
example2
hive stop
`
bio := bufio.NewReader(bytes.NewBufferString(script))
var stdout bytes.Buffer
err = e.Execute(s, "", bio, &stdout)
require.NoError(t, err, "Execute")
expected := `> hive start.*> example1.*hello1.*> example2.*hello2.*> hive stop`
require.Regexp(t, expected, strings.ReplaceAll(stdout.String(), "\n", " "))
}