forked from codeskyblue/go-sh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sh_test.go
83 lines (77 loc) · 1.56 KB
/
sh_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
76
77
78
79
80
81
82
83
package sh
import (
"runtime"
"testing"
)
func TestAlias(t *testing.T) {
s := NewSession()
s.Alias("gr", "echo", "hi")
out, err := s.Command("gr", "sky").Output()
if err != nil {
t.Error(err)
}
if string(out) != "hi sky\n" {
t.Errorf("expect 'hi sky' but got:%s", string(out))
}
}
func TestCommand1(t *testing.T) {
var err error
err = Command("echo", "hello123").Run()
if err != nil {
t.Error(err)
}
}
/*
func TestCapture(t *testing.T) {
r, err := Capture("echo", []string{"hello"})
if err != nil {
t.Error(err)
}
_ = r
if r.Trim() != "hello" {
t.Errorf("expect hello, but got %s", r.Trim())
}
}
*/
func TestSession(t *testing.T) {
if runtime.GOOS == "windows" {
t.Log("ignore test on windows")
return
}
session := NewSession()
session.ShowCMD = true
err := session.Call("pwd")
if err != nil {
t.Error(err)
}
out, err := session.SetDir("/").Command("pwd").Output()
if err != nil {
t.Error(err)
}
if string(out) != "/\n" {
t.Errorf("expect /, but got %s", string(out))
}
}
/*
#!/bin/bash -
#
export PATH=/usr/bin:/bin
alias ll='ls -l'
cd /usr
if test -d "local"
then
ll local | awk '{print $1, $NF}' | grep bin
fi
*/
func TestExample(t *testing.T) {
s := NewSession()
s.ShowCMD = true
s.Env["PATH"] = "/usr/bin:/bin"
s.SetDir("/usr")
s.Alias("ll", "ls", "-l")
//s.Stdout = nil
if s.Test("d", "local") {
//s.Command("ll", []string{"local"}).Command("awk", []string{"{print $1, $NF}"}).Command("grep", []string{"bin"}).Run()
s.Command("ll", "local").Command("awk", "{print $1, $NF}").Command("grep", "bin").Run()
}
}