-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathexample_test.go
More file actions
58 lines (50 loc) · 1.19 KB
/
example_test.go
File metadata and controls
58 lines (50 loc) · 1.19 KB
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
package spec
import (
"bytes"
_ "embed"
"testing"
"github.com/carapace-sh/carapace/pkg/assert"
"github.com/carapace-sh/carapace/pkg/sandbox"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
)
func sandboxSpec(t *testing.T, spec string) (f func(func(s *sandbox.Sandbox))) {
var command Command
if err := yaml.Unmarshal([]byte(spec), &command); err != nil {
panic(err.Error())
}
return sandbox.Command(t, func() *cobra.Command {
return command.ToCobra()
})
}
func runnableSpec(t *testing.T, spec string) func(func(r runnable)) {
return func(f func(r runnable)) {
var command Command
if err := yaml.Unmarshal([]byte(spec), &command); err != nil {
t.Error(err)
}
f(runnable{t, command})
}
}
type runnable struct {
t *testing.T
command Command
}
type runnableResult struct {
t *testing.T
actual string
}
func (r runnable) Run(args ...string) runnableResult {
cmd := r.command.ToCobra()
var stdout, stderr bytes.Buffer
cmd.SetArgs(args)
cmd.SetOut(&stdout)
cmd.SetErr(&stderr)
if err := cmd.Execute(); err != nil {
r.t.Error(err)
}
return runnableResult{r.t, stdout.String()}
}
func (r runnableResult) Expect(expected string) {
assert.Equal(r.t, expected, r.actual)
}