-
Notifications
You must be signed in to change notification settings - Fork 257
/
Copy pathcmd_test.go
72 lines (65 loc) · 1.5 KB
/
cmd_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
package sh
import (
"bytes"
"os"
"testing"
)
func TestOutCmd(t *testing.T) {
cmd := OutCmd(os.Args[0], "-printArgs", "foo", "bar")
out, err := cmd("baz", "bat")
if err != nil {
t.Fatal(err)
}
expected := "[foo bar baz bat]"
if out != expected {
t.Fatalf("expected %q but got %q", expected, out)
}
}
func TestExitCode(t *testing.T) {
ran, err := Exec(nil, nil, nil, os.Args[0], "-helper", "-exit", "99")
if err == nil {
t.Fatal("unexpected nil error from run")
}
if !ran {
t.Errorf("ran returned as false, but should have been true")
}
code := ExitStatus(err)
if code != 99 {
t.Fatalf("expected exit status 99, but got %v", code)
}
}
func TestEnv(t *testing.T) {
env := "SOME_REALLY_LONG_MAGEFILE_SPECIFIC_THING"
out := &bytes.Buffer{}
ran, err := Exec(map[string]string{env: "foobar"}, out, nil, os.Args[0], "-printVar", env)
if err != nil {
t.Fatalf("unexpected error from runner: %#v", err)
}
if !ran {
t.Errorf("expected ran to be true but was false.")
}
if out.String() != "foobar\n" {
t.Errorf("expected foobar, got %q", out)
}
}
func TestNotRun(t *testing.T) {
ran, err := Exec(nil, nil, nil, "thiswontwork")
if err == nil {
t.Fatal("unexpected nil error")
}
if ran {
t.Fatal("expected ran to be false but was true")
}
}
func TestAutoExpand(t *testing.T) {
if err := os.Setenv("MAGE_FOOBAR", "baz"); err != nil {
t.Fatal(err)
}
s, err := Output("echo", "$MAGE_FOOBAR")
if err != nil {
t.Fatal(err)
}
if s != "baz" {
t.Fatalf(`Expected "baz" but got %q`, s)
}
}