forked from git-lfs/git-lfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subprocess_test.go
91 lines (77 loc) · 2.88 KB
/
subprocess_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 subprocess
import (
"testing"
"github.com/stretchr/testify/assert"
)
type ShellQuoteTestCase struct {
Given []string
Expected []string
}
func (c *ShellQuoteTestCase) Assert(t *testing.T) {
actual := ShellQuote(c.Given)
assert.Equal(t, c.Expected, actual,
"tools: expected ShellQuote(%q) to equal %#v (was %#v)",
c.Given, c.Expected, actual,
)
}
func TestShellQuote(t *testing.T) {
for desc, c := range map[string]ShellQuoteTestCase{
"simple": {[]string{"foo", "bar", "an_id"}, []string{"foo", "bar", "an_id"}},
"leading space": {[]string{" foo", "bar"}, []string{"' foo'", "bar"}},
"trailing space": {[]string{"foo", "bar "}, []string{"foo", "'bar '"}},
"internal space": {[]string{"foo bar", "baz quux"}, []string{"'foo bar'", "'baz quux'"}},
"backslash": {[]string{`foo\bar`, `b\az`}, []string{`'foo\bar'`, `'b\az'`}},
"quotes": {[]string{`foo"bar`, "b'az"}, []string{`'foo"bar'`, "'b'\\''az'"}},
"mixed quotes": {[]string{`"foo'ba\"r\"'"`}, []string{`'"foo'\''ba\"r\"'\''"'`}},
} {
t.Run(desc, c.Assert)
}
}
type FormatForShellQuotedArgsTestCase struct {
GivenCmd string
GivenArgs []string
ExpectedArgs []string
}
func (c *FormatForShellQuotedArgsTestCase) Assert(t *testing.T) {
actualCmd, actualArgs := FormatForShellQuotedArgs(c.GivenCmd, c.GivenArgs)
assert.Equal(t, "sh", actualCmd,
"tools: expected FormatForShell command to equal 'sh' (was #%v)",
actualCmd)
assert.Equal(t, c.ExpectedArgs, actualArgs,
"tools: expected FormatForShell(%q, %v) to equal %#v (was %#v)",
c.GivenCmd, c.GivenArgs, c.ExpectedArgs, actualArgs,
)
}
func TestFormatForShellQuotedArgs(t *testing.T) {
for desc, c := range map[string]FormatForShellQuotedArgsTestCase{
"simple": {"foo", []string{"bar", "baz"}, []string{"-c", "foo bar baz"}},
"spaces": {"foo quux", []string{" bar", "baz "}, []string{"-c", "foo quux ' bar' 'baz '"}},
"backslashes": {"bin/foo", []string{"\\bar", "b\\az"}, []string{"-c", "bin/foo '\\bar' 'b\\az'"}},
} {
t.Run(desc, c.Assert)
}
}
type FormatForShellTestCase struct {
GivenCmd string
GivenArgs string
ExpectedArgs []string
}
func (c *FormatForShellTestCase) Assert(t *testing.T) {
actualCmd, actualArgs := FormatForShell(c.GivenCmd, c.GivenArgs)
assert.Equal(t, "sh", actualCmd,
"tools: expected FormatForShell command to equal 'sh' (was #%v)",
actualCmd)
assert.Equal(t, c.ExpectedArgs, actualArgs,
"tools: expected FormatForShell(%q, %v) to equal %#v (was %#v)",
c.GivenCmd, c.GivenArgs, c.ExpectedArgs, actualArgs,
)
}
func TestFormatForShell(t *testing.T) {
for desc, c := range map[string]FormatForShellTestCase{
"simple": {"foo", "bar", []string{"-c", "foo bar"}},
"spaces": {"foo quux", "bar baz", []string{"-c", "foo quux bar baz"}},
"quotes": {"bin/foo", "bar \"baz quux\" 'fred wilma'", []string{"-c", "bin/foo bar \"baz quux\" 'fred wilma'"}},
} {
t.Run(desc, c.Assert)
}
}