-
Notifications
You must be signed in to change notification settings - Fork 190
/
Copy pathcmd_test.go
47 lines (39 loc) · 984 Bytes
/
cmd_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
package cmdr_test
import (
"fmt"
"testing"
"github.com/gookit/goutil/sysutil/cmdr"
"github.com/gookit/goutil/testutil/assert"
)
func TestNewCmd(t *testing.T) {
c := cmdr.NewCmd("ls").
WithArg("-l").
WithArgs([]string{"-h"}).
AddArg("-a").
AddArgf("%s", "./")
assert.Eq(t, "ls", c.BinName())
assert.Eq(t, "ls", c.IDString())
assert.StrContains(t, "ls", c.BinOrPath())
assert.NotContains(t, c.OnlyArgs(), "ls")
c.OnBefore(func(c *cmdr.Cmd) {
assert.Eq(t, "ls -l -h -a ./", c.Cmdline())
})
// SafeOutput
out := c.SafeOutput()
fmt.Println(out)
assert.NotEmpty(t, out)
assert.NotEmpty(t, cmdr.OutputLines(out))
assert.NotEmpty(t, cmdr.FirstLine(out))
c.ResetArgs()
assert.Len(t, c.Args, 1)
assert.Empty(t, c.OnlyArgs())
}
func TestCmdRun(t *testing.T) {
t.Run("AllOutput", func(t *testing.T) {
c := cmdr.NewCmd("echo", "OK")
assert.Eq(t, "OK", c.Args[1])
output, err := c.AllOutput()
assert.NoErr(t, err)
assert.Eq(t, "OK\n", output)
})
}