forked from kreuzwerker/tacks
/
command_test.go
91 lines (57 loc) · 1.4 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
package tacks
import (
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
var (
bad = `false`
missing = `no_such_binary_exists`
good1 = `ls -1 | sort -fr | head -n 1`
good2 = `ls -1 | sort -f | head -n 1`
)
func TestCommand(t *testing.T) {
t.Parallel()
assert := assert.New(t)
c := Command(good1)
res, err := c.Value()
assert.Equal("values_test.go", res)
assert.NoError(err)
c = Command(bad)
res, err = c.Value()
assert.Empty(res)
assert.Equal(`failed to execute "false": "" (exit status 1)`, err.Error())
c = Command(missing)
res, err = c.Value()
assert.Empty(res)
assert.Equal(`failed to execute "no_such_binary_exists": /bin/bash: no_such_binary_exists: command not found (exit status 127)`, err.Error())
}
func TestCommandEnv(t *testing.T) {
t.Parallel()
assert := assert.New(t)
c := Command(`env`)
res, err := c.Value()
assert.NoError(err)
var (
golang = os.Environ()
shell = strings.Split(res.(string), "\n")
)
for _, e := range golang {
if strings.HasPrefix(e, "PATH=") {
for _, ee := range shell {
if strings.HasPrefix(ee, "PATH=") {
assert.Equal(e, ee)
return
}
}
}
}
assert.Fail("PATH not inherited")
}
func TestCommandPipefail(t *testing.T) {
assert := assert.New(t)
c := Command(`false | echo`)
_, err := c.Value()
assert.Equal(`failed to execute "false | echo": "" (exit status 1)`, err.Error())
}