-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommand_test.go
141 lines (121 loc) · 2.92 KB
/
command_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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package egg
import (
"bytes"
"io"
"log"
"os"
"sync"
"testing"
)
func TestNewCommand(t *testing.T) {
var command *Command
var err error
// NewCommand with no args
command, err = NewCommand([]string{})
if err == nil {
t.Errorf("NewCommand with no args should of returned an error")
}
if command != nil {
t.Errorf("NewCommand with no args return %v, it should have been nil", command)
}
// NewCommand with one arg
command, err = NewCommand([]string{"ls"})
if err != nil {
t.Errorf("NewCommand with one arg shouldn't of returned an error: %v", err)
}
if command == nil {
t.Errorf("NewCommand with one args should have returns a command but it did not")
}
// NewCommand with multiple args
command, err = NewCommand([]string{"ls", "-al"})
if err != nil {
t.Errorf("NewCommand with one arg shouldn't of returned an error: %v", err)
}
if command == nil {
t.Errorf("NewCommand with one args should have returns a command but it did not")
}
}
func TestRun(t *testing.T) {
var command *Command
var err error
var output string
var expectedOutput string
// Run with echo
output = captureOutput(func() {
command, err = NewCommand([]string{"echo", "whatup"})
if err != nil {
t.Errorf("Error creating command: %v", err)
}
err = command.Run()
if err != nil {
t.Errorf("Error running echo %v", err)
}
})
expectedOutput = "whatup\n"
if output != expectedOutput {
t.Errorf("Command output should have been %#v, but was %#v", expectedOutput, output)
}
// Run with command that doesn't exist
output = captureOutput(func() {
command, err = NewCommand([]string{"idontexist"})
if err != nil {
t.Errorf("Error creating command: %v", err)
}
err = command.Run()
if err == nil {
t.Errorf("Non existing command should have raise error")
}
})
expectedOutput = ""
if output != expectedOutput {
t.Errorf("Command output should have been %#v, but was %#v", expectedOutput, output)
}
// Run called twice
output = captureOutput(func() {
command, err = NewCommand([]string{"echo"})
if err != nil {
t.Errorf("Error creating command: %v", err)
}
err = command.Run()
if err != nil {
t.Errorf("Error running command: %v", err)
}
err = command.Run()
if err == nil {
t.Errorf("No error was raised when Run was called twice")
}
})
expectedOutput = "\n"
if output != expectedOutput {
t.Errorf("Command output should have been %#v, but was %#v", expectedOutput, output)
}
}
func captureOutput(f func()) string {
reader, writer, err := os.Pipe()
if err != nil {
panic(err)
}
stdout := os.Stdout
stderr := os.Stderr
defer func() {
os.Stdout = stdout
os.Stderr = stderr
log.SetOutput(os.Stderr)
}()
os.Stdout = writer
os.Stderr = writer
log.SetOutput(writer)
out := make(chan string)
wg := new(sync.WaitGroup)
wg.Add(1)
go func() {
var buf bytes.Buffer
wg.Done()
io.Copy(&buf, reader)
out <- buf.String()
}()
wg.Wait()
f()
writer.Close()
return <-out
}