Skip to content

Commit e98c2fb

Browse files
committed
test: rework TestProcessWithMatches to use a matrix
Signed-off-by: Justin Chadwell <[email protected]>
1 parent 30df092 commit e98c2fb

File tree

1 file changed

+35
-16
lines changed

1 file changed

+35
-16
lines changed

frontend/dockerfile/shell/lex_test.go

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -226,24 +226,43 @@ func TestGetEnv(t *testing.T) {
226226
func TestProcessWithMatches(t *testing.T) {
227227
shlex := NewLex('\\')
228228

229-
w, matches, err := shlex.ProcessWordWithMatches("foo ${BAR} ${UNUSED}", map[string]string{
230-
"ANOTHER": "bar",
231-
"BAR": "baz",
232-
})
233-
require.NoError(t, err)
234-
require.Equal(t, "foo baz ", w)
235-
236-
require.Equal(t, 1, len(matches))
237-
_, ok := matches["BAR"]
238-
require.True(t, ok)
229+
tc := []struct {
230+
input string
231+
envs map[string]string
232+
expected string
233+
expectedErr bool
234+
matches map[string]struct{}
235+
}{
236+
{
237+
input: "foo ${BAR} ${UNUSED}",
238+
envs: map[string]string{"ANOTHER": "bar", "BAR": "baz"},
239+
expected: "foo baz ",
240+
matches: map[string]struct{}{"BAR": {}},
241+
},
242+
{
243+
input: "foo ${BAR:-abc} ${UNUSED}",
244+
envs: map[string]string{"ANOTHER": "bar"},
245+
expected: "foo abc ",
246+
matches: map[string]struct{}{},
247+
},
248+
}
239249

240-
w, matches, err = shlex.ProcessWordWithMatches("foo ${BAR:-abc} ${UNUSED}", map[string]string{
241-
"ANOTHER": "bar",
242-
})
243-
require.NoError(t, err)
244-
require.Equal(t, "foo abc ", w)
250+
for _, c := range tc {
251+
c := c
252+
t.Run(c.input, func(t *testing.T) {
253+
w, matches, err := shlex.ProcessWordWithMatches(c.input, c.envs)
254+
if c.expectedErr {
255+
require.Error(t, err)
256+
return
257+
}
258+
require.NoError(t, err)
259+
require.Equal(t, c.expected, w)
245260

246-
require.Equal(t, 0, len(matches))
261+
require.Equal(t, len(c.matches), len(matches))
262+
for k := range c.matches {
263+
require.Contains(t, matches, k)
264+
}
265+
}
247266
}
248267

249268
func TestProcessWithMatchesPlatform(t *testing.T) {

0 commit comments

Comments
 (0)