-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcall_test.go
71 lines (63 loc) · 1.83 KB
/
call_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
package muts
import (
"bytes"
"fmt"
"os/exec"
"strings"
"testing"
"time"
)
func TestWaitCall(t *testing.T) {
var args []string
var cmd string
execCommand = func(prog string, params ...string) *exec.Cmd {
args = params
cmd = prog
return new(exec.Cmd)
}
defer func() { execCommand = exec.Command }()
Exec(NewExecOptions("ls").Wait(true).Force(true))
t.Log(cmd, args)
if got, want := cmd, "sh"; got != want {
t.Errorf("got %q want %q", got, want)
}
if got, want := args[0], "-c"; got != want {
t.Errorf("got %q want %q", got, want)
}
if got, want := args[1], "ls"; got != want {
t.Errorf("got %q want %q", got, want)
}
}
func TestCaptureOutput(t *testing.T) {
out, err := CallReturn("date")
if err != nil {
t.Error("date output expected", err)
}
if !strings.Contains(out, fmt.Sprint(time.Now().Year())) {
t.Errorf("got %q", out)
}
}
func TestExecNonSilent(t *testing.T) {
outBuffer := new(bytes.Buffer)
result := Exec(NewExecOptions("echo TEST").Stdout(outBuffer).Silent(false))
if !strings.Contains(outBuffer.String(), "TEST") {
t.Errorf("got [%q] wanted at least [%q]", result, "TEST")
}
outBuffer = new(bytes.Buffer)
result = Exec(NewExecOptions("echo TEST").Stdout(outBuffer).Silent(true))
if strings.Contains(outBuffer.String(), "TEST") {
t.Errorf("got [%q] wanted at least [%q]", result, "TEST")
}
}
func TestExecSilentError(t *testing.T) {
errBuffer := new(bytes.Buffer)
result := Exec(NewExecOptions("echo TEST", " 1>&2 ").Stderr(errBuffer).Silent(false))
if !strings.Contains(errBuffer.String(), "TEST") {
t.Errorf("got [%q] wanted at least [%q]", result, "TEST")
}
errBuffer = new(bytes.Buffer)
result = Exec(NewExecOptions("echo TEST", " 1>&2 ").Stderr(errBuffer).Silent(true))
if strings.Contains(errBuffer.String(), "TEST") {
t.Errorf("got [%q] wanted at least [%q]", result, "TEST")
}
}