Debian Codegraph @

subprocess/subprocess_test.go

raw symbols SCIP
1 package subprocess
2
3 import (
4 "testing"
5
6 "github.com/stretchr/testify/assert"
7 )
8
9 type ShellQuoteTestCase struct {
10 Given []string
11 Expected []string
12 }
13
14 func (c *ShellQuoteTestCase) Assert(t *testing.T) {
15 actual := ShellQuote(c.Given)
16
17 assert.Equal(t, c.Expected, actual,
18 "subprocess: expected ShellQuote(%q) to equal %#v (was %#v)",
19 c.Given, c.Expected, actual,
20 )
21 }
22
23 func TestShellQuote(t *testing.T) {
24 for desc, c := range map[string]ShellQuoteTestCase{
25 "simple": {[]string{"foo", "bar", "an_id"}, []string{"foo", "bar", "an_id"}},
26 "leading space": {[]string{" foo", "bar"}, []string{"' foo'", "bar"}},
27 "trailing space": {[]string{"foo", "bar "}, []string{"foo", "'bar '"}},
28 "internal space": {[]string{"foo bar", "baz quux"}, []string{"'foo bar'", "'baz quux'"}},
29 "backslash": {[]string{`foo\bar`, `b\az`}, []string{`'foo\bar'`, `'b\az'`}},
30 "quotes": {[]string{`foo"bar`, "b'az"}, []string{`'foo"bar'`, "'b'\\''az'"}},
31 "mixed quotes": {[]string{`"foo'ba\"r\"'"`}, []string{`'"foo'\''ba\"r\"'\''"'`}},
32 } {
33 t.Run(desc, c.Assert)
34 }
35 }
36
37 type FormatForShellQuotedArgsTestCase struct {
38 GivenCmd string
39 GivenArgs []string
40 ExpectedArgs []string
41 }
42
43 func (c *FormatForShellQuotedArgsTestCase) Assert(t *testing.T) {
44 actualCmd, actualArgs := FormatForShellQuotedArgs(c.GivenCmd, c.GivenArgs)
45
46 assert.Equal(t, "sh", actualCmd,
47 "subprocess: expected FormatForShell command to equal 'sh' (was #%v)",
48 actualCmd)
49 assert.Equal(t, c.ExpectedArgs, actualArgs,
50 "subprocess: expected FormatForShell(%q, %v) to equal %#v (was %#v)",
51 c.GivenCmd, c.GivenArgs, c.ExpectedArgs, actualArgs,
52 )
53 }
54
55 func TestFormatForShellQuotedArgs(t *testing.T) {
56 for desc, c := range map[string]FormatForShellQuotedArgsTestCase{
57 "simple": {"foo", []string{"bar", "baz"}, []string{"-c", "foo bar baz"}},
58 "spaces": {"foo quux", []string{" bar", "baz "}, []string{"-c", "foo quux ' bar' 'baz '"}},
59 "backslashes": {"bin/foo", []string{"\\bar", "b\\az"}, []string{"-c", "bin/foo '\\bar' 'b\\az'"}},
60 } {
61 t.Run(desc, c.Assert)
62 }
63 }
64
65 type FormatForShellTestCase struct {
66 GivenCmd string
67 GivenArgs string
68 ExpectedArgs []string
69 }
70
71 func (c *FormatForShellTestCase) Assert(t *testing.T) {
72 actualCmd, actualArgs := FormatForShell(c.GivenCmd, c.GivenArgs)
73
74 assert.Equal(t, "sh", actualCmd,
75 "subprocess: expected FormatForShell command to equal 'sh' (was #%v)",
76 actualCmd)
77 assert.Equal(t, c.ExpectedArgs, actualArgs,
78 "subprocess: expected FormatForShell(%q, %v) to equal %#v (was %#v)",
79 c.GivenCmd, c.GivenArgs, c.ExpectedArgs, actualArgs,
80 )
81 }
82
83 func TestFormatForShell(t *testing.T) {
84 for desc, c := range map[string]FormatForShellTestCase{
85 "simple": {"foo", "bar", []string{"-c", "foo bar"}},
86 "spaces": {"foo quux", "bar baz", []string{"-c", "foo quux bar baz"}},
87 "quotes": {"bin/foo", "bar \"baz quux\" 'fred wilma'", []string{"-c", "bin/foo bar \"baz quux\" 'fred wilma'"}},
88 } {
89 t.Run(desc, c.Assert)
90 }
91 }
92
93 type FormatPercentSequencesTestCase struct {
94 GivenPattern string
95 GivenReplacements map[string]string
96 ExpectedString string
97 }
98
99 func (c *FormatPercentSequencesTestCase) Assert(t *testing.T) {
100 actualString := FormatPercentSequences(c.GivenPattern, c.GivenReplacements)
101
102 assert.Equal(t, c.ExpectedString, actualString,
103 "subprocess: expected FormatForShell(%q, %v) to equal %q (was %q)",
104 c.GivenPattern, c.GivenReplacements, c.ExpectedString, actualString,
105 )
106 }
107
108 func TestFormatPercentSequences(t *testing.T) {
109 replacements := map[string]string{
110 "A": "current",
111 "B": "other file",
112 "P": "some ' output \" file",
113 }
114 for desc, c := range map[string]FormatPercentSequencesTestCase{
115 "simple": {"merge-foo %A", replacements, "merge-foo current"},
116 "double-percent": {"merge-foo %A %%A", replacements, "merge-foo current %A"},
117 "spaces": {"merge-foo %B", replacements, "merge-foo 'other file'"},
118 "weird filename": {"merge-foo %P", replacements, "merge-foo 'some '\\'' output \" file'"},
119 "no patterns": {"merge-foo /dev/null", replacements, "merge-foo /dev/null"},
120 "pattern adjacent to non-space": {"merge-foo >%B", replacements, "merge-foo >'other file'"},
121 } {
122 t.Run(desc, c.Assert)
123 }
124 }