forked from codeskyblue/go-sh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sh_test.go
106 lines (97 loc) · 2.1 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package sh
import (
"fmt"
"log"
"runtime"
"strings"
"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 ExampleSession_Command() {
s := NewSession()
out, err := s.Command("echo", "hello").Output()
if err != nil {
log.Fatal(err)
}
fmt.Println(string(out))
// Output: hello
}
func ExampleSession_Command_pipe() {
s := NewSession()
out, err := s.Command("echo", "hello", "world").Command("awk", "{print $2}").Output()
if err != nil {
log.Fatal(err)
}
fmt.Println(string(out))
// Output: world
}
func ExampleSession_Alias() {
s := NewSession()
s.Alias("alias_echo_hello", "echo", "hello")
out, err := s.Command("alias_echo_hello", "world").Output()
if err != nil {
log.Fatal(err)
}
fmt.Println(string(out))
// Output: hello world
}
func TestEcho(t *testing.T) {
out, err := Echo("one two three").Command("wc", "-w").Output()
if err != nil {
t.Error(err)
}
if strings.TrimSpace(string(out)) != "3" {
t.Errorf("expect '3' but got:%s", string(out))
}
}
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 Example(t *testing.T) {
s := NewSession()
//s.ShowCMD = true
s.Env["PATH"] = "/usr/bin:/bin"
s.SetDir("/bin")
s.Alias("ll", "ls", "-l")
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()
}
}