forked from rendon/testcli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
179 lines (156 loc) · 3.65 KB
/
main_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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package testcli
import (
"bytes"
"os"
"strings"
"testing"
)
func TestSetEnv(t *testing.T) {
c := Command("/bin/sh", "-c", "echo -n $FOO")
c.SetEnv([]string{"FOO=bar"})
c.Run()
if c.Failure() {
t.Fatalf("Expected to succeed, but failed")
}
if c.stdout != "bar" {
t.Log(c.stdout)
t.Fatal("stdout failed to include input")
}
}
func TestSetStdin(t *testing.T) {
buf := bytes.NewBufferString("foo\n")
c := Command("cat")
c.SetStdin(buf)
c.Run()
if c.Failure() {
t.Fatalf("Expected to succeed, but failed")
}
if c.stdout != "foo\n" {
t.Fatal("stdout failed to include input")
}
}
func TestFailedRun(t *testing.T) {
c := Command("myunknowncommand")
c.Run()
if !c.Failure() {
t.Fatalf("Expected to fail, but succeeded")
}
}
func TestPackageFailedRun(t *testing.T) {
Run("myunknowncommand")
if !Failure() {
t.Fatalf("Expected to fail, but succeeded")
}
}
func TestSuccessfulRun(t *testing.T) {
c := Command("whoami")
c.Run()
if !c.Success() {
t.Fatalf("Expected to succeed, but failed with error: %s", c.Error())
}
}
func TestPackageSuccessfulRun(t *testing.T) {
Run("whoami")
if !Success() {
t.Fatalf("Expected to succeed, but failed with error: %s", Error())
}
}
func TestStdout(t *testing.T) {
user := os.Getenv("USER")
c := Command("whoami")
c.Run()
if !c.StdoutContains(user) {
t.Fatalf("Expected %q to contains %q", c.Stdout(), user)
}
// testing case insensitiveness
user = strings.ToUpper(user)
if !c.StdoutContains(user) {
t.Fatalf("Expected %q to contains %q", c.Stdout(), user)
}
}
func TestPackageStdout(t *testing.T) {
user := os.Getenv("USER")
Run("whoami")
if !StdoutContains(user) {
t.Fatalf("Expected %q to contains %q", Stdout(), user)
}
// testing case insensitiveness
user = strings.ToUpper(user)
if !StdoutContains(user) {
t.Fatalf("Expected %q to contains %q", Stdout(), user)
}
}
func TestStderr(t *testing.T) {
c := Command("cp")
c.Run()
if !c.Failure() {
t.Fatalf("Expected to fail, but succeeded")
}
if c.Stderr() == "" {
t.Fatalf("Expected %q NOT to be empty", c.Stderr())
}
// testing case insensitiveness
if !c.StderrContains("MISSING") {
t.Fatalf("Expected %q to contains %q", c.Stderr(), "MISSING")
}
}
func TestPackageStderr(t *testing.T) {
Run("cp")
if !Failure() {
t.Fatalf("Expected to fail, but succeeded")
}
if !StderrContains("missing") {
t.Fatalf("Expected %q to contains %q", Stderr(), "missing")
}
// testing case insensitiveness
if !StderrContains("MISSING") {
t.Fatalf("Expected %q to contains %q", Stderr(), "MISSING")
}
}
func TestStdoutMatches(t *testing.T) {
regex := "(/[^/]*)+"
c := Command("whoami")
c.Run()
if c.StdoutMatches(regex) {
t.Fatalf("Expected %q NOT to match %q", c.Stdout(), regex)
}
c = Command("pwd")
c.Run()
if !c.StdoutMatches(regex) {
t.Fatalf("Expected %q to match %q", c.Stdout(), regex)
}
}
func TestPackageStdoutMatches(t *testing.T) {
regex := "(/[^/]*)+"
Run("whoami")
if StdoutMatches(regex) {
t.Fatalf("Expected %q NOT to match %q", Stdout(), regex)
}
Run("pwd")
if !StdoutMatches(regex) {
t.Fatalf("Expected %q to match %q", Stdout(), regex)
}
}
func TestStderrMatches(t *testing.T) {
regex := "(/[^/]*)+"
c := Command("cp")
c.Run()
if c.StderrMatches(regex) {
t.Fatalf("Expected %q NOT to match %q", c.Stderr(), regex)
}
regex = "cp.*operand"
if !c.StderrMatches(regex) {
t.Fatalf("Expected %q to match %q", c.Stderr(), regex)
}
}
func TestPackageStderrMatches(t *testing.T) {
regex := "(/[^/]*)+"
Run("cp")
if StderrMatches(regex) {
t.Fatalf("Expected %q NOT to match %q", Stderr(), regex)
}
regex = ".*cp.*"
if !StderrMatches(regex) {
t.Fatalf("Expected %q to match %q", Stderr(), regex)
}
}