...
1 package sh
2
3 import (
4 "fmt"
5 "log"
6 "runtime"
7 "strings"
8 "testing"
9 )
10
11 func TestAlias(t *testing.T) {
12 s := NewSession()
13 s.Alias("gr", "echo", "hi")
14 out, err := s.Command("gr", "sky").Output()
15 if err != nil {
16 t.Error(err)
17 }
18 if string(out) != "hi sky\n" {
19 t.Errorf("expect 'hi sky' but got:%s", string(out))
20 }
21 }
22
23 func ExampleSession_Command() {
24 s := NewSession()
25 out, err := s.Command("echo", "hello").Output()
26 if err != nil {
27 log.Fatal(err)
28 }
29 fmt.Println(string(out))
30
31 }
32
33 func ExampleSession_Command_pipe() {
34 s := NewSession()
35 out, err := s.Command("echo", "hello", "world").Command("awk", "{print $2}").Output()
36 if err != nil {
37 log.Fatal(err)
38 }
39 fmt.Println(string(out))
40
41 }
42
43 func ExampleSession_Alias() {
44 s := NewSession()
45 s.Alias("alias_echo_hello", "echo", "hello")
46 out, err := s.Command("alias_echo_hello", "world").Output()
47 if err != nil {
48 log.Fatal(err)
49 }
50 fmt.Println(string(out))
51
52 }
53
54 func TestEcho(t *testing.T) {
55 out, err := Echo("one two three").Command("wc", "-w").Output()
56 if err != nil {
57 t.Error(err)
58 }
59 if strings.TrimSpace(string(out)) != "3" {
60 t.Errorf("expect '3' but got:%s", string(out))
61 }
62 }
63
64 func TestSession(t *testing.T) {
65 if runtime.GOOS == "windows" {
66 t.Log("ignore test on windows")
67 return
68 }
69 session := NewSession()
70 session.ShowCMD = true
71 err := session.Call("pwd")
72 if err != nil {
73 t.Error(err)
74 }
75 out, err := session.SetDir("/").Command("pwd").Output()
76 if err != nil {
77 t.Error(err)
78 }
79 if string(out) != "/\n" {
80 t.Errorf("expect /, but got %s", string(out))
81 }
82 }
83
84
95 func Example(t *testing.T) {
96 s := NewSession()
97
98 s.Env["PATH"] = "/usr/bin:/bin"
99 s.SetDir("/bin")
100 s.Alias("ll", "ls", "-l")
101
102 if s.Test("d", "local") {
103
104 s.Command("ll", "local").Command("awk", "{print $1, $NF}").Command("grep", "bin").Run()
105 }
106 }
107
View as plain text