-
Notifications
You must be signed in to change notification settings - Fork 31
/
exec_test.go
120 lines (101 loc) · 2.17 KB
/
exec_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
package godo
import (
"fmt"
"io/ioutil"
"runtime"
"strings"
"testing"
"gopkg.in/stretchr/testify.v1/assert"
"github.com/mgutz/str"
)
var cat = "cat"
func init() {
if runtime.GOOS == "windows" {
cat = "type"
}
}
func TestRunMultiline(t *testing.T) {
output, _ := RunOutput(`
{{.cat}} test/foo.txt
{{.cat}} test/bar.txt
`, M{"cat": cat})
assert.Equal(t, "foo\nbar\n", output)
}
func TestRunError(t *testing.T) {
output, err := RunOutput(`
{{.cat}} test/doesnotexist.txt
{{.cat}} test/bar.txt
`, M{"cat": cat})
assert.Error(t, err)
assert.Contains(t, err.Error(), "line=1")
assert.Contains(t, output, "doesnotexist")
}
func TestInside(t *testing.T) {
Inside("test", func() {
var out string
if isWindows {
out, _ = RunOutput("foo.cmd")
} else {
out, _ = RunOutput("bash foo.sh")
}
if str.Clean(out) != "FOOBAR" {
t.Error("Inside failed. Got", fmt.Sprintf("%q", out))
}
})
version, _ := ioutil.ReadFile("./VERSION.go")
if !strings.Contains(string(version), "var Version") {
t.Error("Inside failed to reset work directory")
}
}
func TestBash(t *testing.T) {
if isWindows {
return
}
out, _ := BashOutput(`echo -n foobar`)
if out != "foobar" {
t.Error("Simple bash failed. Got", out)
}
out, _ = BashOutput(`
echo -n foobar
echo -n bahbaz
`)
if out != "foobarbahbaz" {
t.Error("Multiline bash failed. Got", out)
}
out, _ = BashOutput(`
echo -n \
foobar
`)
if out != "foobar" {
t.Error("Bash line continuation failed. Got", out)
}
out, _ = BashOutput(`
echo -n "foobar"
`)
if out != "foobar" {
t.Error("Bash quotes failed. Got", out)
}
out, _ = BashOutput(`
echo -n "fo\"obar"
`)
if out != "fo\"obar" {
t.Error("Bash quoted string failed. Got", out)
}
}
func TestTemplatedCommands(t *testing.T) {
echo := "echo"
if isWindows {
echo = "cmd /c echo"
}
// in V2 BashOutput accepts an options map
out, err := RunOutput(echo+" {{.name}}", M{"name": "oy"})
assert.NoError(t, err)
assert.Equal(t, "oy", str.Clean(out))
if isWindows {
return
}
// in V2 BashOutput accepts an options map
out, err = BashOutput("echo {{.name}}", M{"name": "oy"})
assert.NoError(t, err)
assert.Equal(t, "oy", str.Clean(out))
}