forked from gookit/goutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec_test.go
59 lines (48 loc) · 1.37 KB
/
exec_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
package sysutil_test
import (
"strings"
"testing"
"github.com/gookit/goutil/sysutil"
"github.com/gookit/goutil/testutil/assert"
)
func TestNewCmd(t *testing.T) {
c := sysutil.NewCmd("echo", "OK")
assert.Eq(t, "OK", c.Args[1])
}
func TestFlushExec(t *testing.T) {
err := sysutil.FlushExec("echo", "OK")
assert.NoErr(t, err)
}
func TestExecCmd(t *testing.T) {
ret, err := sysutil.ExecCmd("echo", []string{"OK"})
assert.NoErr(t, err)
// *nix: "OK\n" win: "OK\r\n"
assert.Eq(t, "OK", strings.TrimSpace(ret))
ret, err = sysutil.QuickExec("echo OK")
assert.NoErr(t, err)
assert.Eq(t, "OK", strings.TrimSpace(ret))
ret, err = sysutil.ExecLine("echo OK1")
assert.NoErr(t, err)
assert.Eq(t, "OK1", strings.TrimSpace(ret))
}
func TestShellExec(t *testing.T) {
ret, err := sysutil.ShellExec("echo OK")
assert.NoErr(t, err)
// *nix: "OK\n" win: "OK\r\n"
assert.Eq(t, "OK", strings.TrimSpace(ret))
ret, err = sysutil.ShellExec("echo OK", "bash")
assert.NoErr(t, err)
assert.Eq(t, "OK", strings.TrimSpace(ret))
if sysutil.IsWindows() {
t.Run("cmd", func(t *testing.T) {
ret, err := sysutil.ShellExec("echo OK", "cmd")
assert.NoErr(t, err)
assert.Eq(t, "OK", strings.TrimSpace(ret))
})
t.Run("powershell", func(t *testing.T) {
ret, err := sysutil.ShellExec("echo OK", "powershell")
assert.NoErr(t, err)
assert.Eq(t, "OK", strings.TrimSpace(ret))
})
}
}