-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmain_test.go
More file actions
56 lines (46 loc) · 1.38 KB
/
Copy pathmain_test.go
File metadata and controls
56 lines (46 loc) · 1.38 KB
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
package main
import (
"os/exec"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestShellQuote(t *testing.T) {
assert.Equal(t, "''", shellQuote(""))
assert.Equal(t, "'hello'", shellQuote("hello"))
assert.Equal(t, "'hello world'", shellQuote("hello world"))
assert.Equal(t, `'it'\''s'`, shellQuote("it's"))
}
func TestShellJoinPreservesBoundaries(t *testing.T) {
cases := [][]string{
{"hello world", "foo"},
{"a", "b c", "d"},
{"with 'quote", "ok"},
{"$VAR;echo pwned", "safe"},
{},
}
for _, in := range cases {
joined := shellJoin(in)
// Echo each argument on its own line through /bin/sh, then split back.
script := "for a in " + joined + `; do printf '%s\n' "$a"; done`
out, err := exec.Command("/bin/sh", "-c", script).Output()
require.NoError(t, err, "script: %s", script)
var got []string
if s := strings.TrimRight(string(out), "\n"); s != "" {
got = strings.Split(s, "\n")
}
assert.Equal(t, append([]string(nil), in...), got, "round-trip failed for %q", in)
}
}
func TestWatchInterval(t *testing.T) {
d, err := watchInterval("")
require.NoError(t, err)
assert.Equal(t, "500ms", d.String())
d, err = watchInterval("2s")
require.NoError(t, err)
assert.Equal(t, "2s", d.String())
_, err = watchInterval("not-a-duration")
require.Error(t, err)
assert.Contains(t, err.Error(), "not-a-duration")
}