Skip to content

Commit aa2ed30

Browse files
authored
Merge pull request #4136 from apostasie/tigron-2025-04-contains-multi
[Tigron]: support multi-parameters for `expect.Contains` and `expect.DoesNotContain`
2 parents 65658d3 + 4e96113 commit aa2ed30

14 files changed

+95
-98
lines changed

cmd/nerdctl/completion/completion_test.go

+3-12
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,7 @@ func TestCompletion(t *testing.T) {
9191
Command: test.Command("__complete", "run", "--net", ""),
9292
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
9393
return &test.Expected{
94-
Output: expect.All(
95-
expect.Contains("host\n"),
96-
expect.Contains(data.Labels().Get("identifier")+"\n"),
97-
),
94+
Output: expect.Contains("host\n", data.Labels().Get("identifier")+"\n"),
9895
}
9996
},
10097
},
@@ -103,10 +100,7 @@ func TestCompletion(t *testing.T) {
103100
Command: test.Command("__complete", "run", "-it", "--net", ""),
104101
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
105102
return &test.Expected{
106-
Output: expect.All(
107-
expect.Contains("host\n"),
108-
expect.Contains(data.Labels().Get("identifier")+"\n"),
109-
),
103+
Output: expect.Contains("host\n", data.Labels().Get("identifier")+"\n"),
110104
}
111105
},
112106
},
@@ -115,10 +109,7 @@ func TestCompletion(t *testing.T) {
115109
Command: test.Command("__complete", "run", "-it", "--rm", "--net", ""),
116110
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
117111
return &test.Expected{
118-
Output: expect.All(
119-
expect.Contains("host\n"),
120-
expect.Contains(data.Labels().Get("identifier")+"\n"),
121-
),
112+
Output: expect.Contains("host\n", data.Labels().Get("identifier")+"\n"),
122113
}
123114
},
124115
},

cmd/nerdctl/compose/compose_build_linux_test.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,7 @@ services:
9595

9696
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
9797
return &test.Expected{
98-
Output: expect.All(
99-
expect.Contains(data.Labels().Get("imageSvc0")),
100-
expect.Contains(data.Labels().Get("imageSvc1")),
101-
),
98+
Output: expect.Contains(data.Labels().Get("imageSvc0"), data.Labels().Get("imageSvc1")),
10299
}
103100
},
104101
},

cmd/nerdctl/compose/compose_config_test.go

+15-18
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,11 @@ services:
162162
"config",
163163
)
164164
},
165-
Expected: test.Expects(expect.ExitCodeSuccess, nil, expect.All(
166-
expect.Contains("alpine:3.13"),
167-
expect.Contains("alpine:3.14"),
168-
expect.Contains("hello1"),
169-
expect.Contains("hello2"),
170-
)),
165+
Expected: test.Expects(
166+
expect.ExitCodeSuccess,
167+
nil,
168+
expect.Contains("alpine:3.13", "alpine:3.14", "hello1", "hello2"),
169+
),
171170
},
172171
{
173172
Description: "project dir",
@@ -230,12 +229,11 @@ services:
230229
cmd.Setenv("COMPOSE_PATH_SEPARATOR", ",")
231230
return cmd
232231
},
233-
Expected: test.Expects(expect.ExitCodeSuccess, nil, expect.All(
234-
expect.Contains("alpine:3.13"),
235-
expect.Contains("alpine:3.14"),
236-
expect.Contains("hello1"),
237-
expect.Contains("hello2"),
238-
)),
232+
Expected: test.Expects(
233+
expect.ExitCodeSuccess,
234+
nil,
235+
expect.Contains("alpine:3.13", "alpine:3.14", "hello1", "hello2"),
236+
),
239237
},
240238
{
241239
Description: "env with project dir",
@@ -249,12 +247,11 @@ services:
249247
cmd.Setenv("COMPOSE_PATH_SEPARATOR", ",")
250248
return cmd
251249
},
252-
Expected: test.Expects(expect.ExitCodeSuccess, nil, expect.All(
253-
expect.Contains("alpine:3.13"),
254-
expect.Contains("alpine:3.14"),
255-
expect.Contains("hello1"),
256-
expect.Contains("hello2"),
257-
)),
250+
Expected: test.Expects(
251+
expect.ExitCodeSuccess,
252+
nil,
253+
expect.Contains("alpine:3.13", "alpine:3.14", "hello1", "hello2"),
254+
),
258255
},
259256
}
260257

cmd/nerdctl/compose/compose_exec_linux_test.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,16 @@ services:
128128
"env")
129129
},
130130
Expected: test.Expects(expect.ExitCodeSuccess, nil, expect.All(
131-
expect.Contains("\nFOO=foo1,foo2\n"),
132-
expect.Contains("\nBAR=bar1 bar2\n"),
133-
expect.Contains("\nBAZ=\n"),
131+
expect.Contains(
132+
"\nFOO=foo1,foo2\n",
133+
"\nBAR=bar1 bar2\n",
134+
"\nBAZ=\n",
135+
"\nQUUX=quux2\n",
136+
"\nCORGE=corge-value-in-host\n",
137+
"\nGRAULT=grault_key=grault_value\n",
138+
"\nGARPLY=\n",
139+
"\nWALDO=\n"),
134140
expect.DoesNotContain("QUX"),
135-
expect.Contains("\nQUUX=quux2\n"),
136-
expect.Contains("\nCORGE=corge-value-in-host\n"),
137-
expect.Contains("\nGRAULT=grault_key=grault_value\n"),
138-
expect.Contains("\nGARPLY=\n"),
139-
expect.Contains("\nWALDO=\n"),
140141
)),
141142
},
142143
}

cmd/nerdctl/container/container_diff_test.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,15 @@ func TestDiff(t *testing.T) {
5151
return helpers.Command("diff", data.Identifier())
5252
}
5353

54-
testCase.Expected = test.Expects(0, nil, expect.All(
55-
expect.Contains("A /a"),
56-
expect.Contains("C /bin"),
57-
expect.Contains("A /bin/b"),
58-
expect.Contains("D /bin/base64"),
59-
))
54+
testCase.Expected = test.Expects(
55+
0,
56+
nil,
57+
expect.Contains(
58+
"A /a",
59+
"C /bin",
60+
"A /bin/b",
61+
"D /bin/base64"),
62+
)
6063

6164
testCase.Run(t)
6265
}

cmd/nerdctl/container/container_logs_test.go

+4-8
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,7 @@ func TestLogsWithForegroundContainers(t *testing.T) {
247247
return helpers.Command("logs", data.Identifier())
248248
},
249249
Expected: test.Expects(0, nil, expect.All(
250-
expect.Contains("foo"),
251-
expect.Contains("bar"),
250+
expect.Contains("foo", "bar"),
252251
expect.DoesNotContain("baz"),
253252
)),
254253
},
@@ -264,8 +263,7 @@ func TestLogsWithForegroundContainers(t *testing.T) {
264263
return helpers.Command("logs", data.Identifier())
265264
},
266265
Expected: test.Expects(0, nil, expect.All(
267-
expect.Contains("foo"),
268-
expect.Contains("bar"),
266+
expect.Contains("foo", "bar"),
269267
expect.DoesNotContain("baz"),
270268
)),
271269
},
@@ -283,8 +281,7 @@ func TestLogsWithForegroundContainers(t *testing.T) {
283281
return helpers.Command("logs", data.Identifier())
284282
},
285283
Expected: test.Expects(0, nil, expect.All(
286-
expect.Contains("foo"),
287-
expect.Contains("bar"),
284+
expect.Contains("foo", "bar"),
288285
expect.DoesNotContain("baz"),
289286
)),
290287
},
@@ -302,8 +299,7 @@ func TestLogsWithForegroundContainers(t *testing.T) {
302299
return helpers.Command("logs", data.Identifier())
303300
},
304301
Expected: test.Expects(0, nil, expect.All(
305-
expect.Contains("foo"),
306-
expect.Contains("bar"),
302+
expect.Contains("foo", "bar"),
307303
expect.DoesNotContain("baz"),
308304
)),
309305
},

cmd/nerdctl/container/container_run_test.go

+19-17
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@ CMD ["echo", "bar"]
8787
},
8888
Expected: test.Expects(expect.ExitCodeSuccess, nil, expect.All(
8989
expect.Contains("blah"),
90-
expect.DoesNotContain("foo"),
91-
expect.DoesNotContain("bar"),
90+
expect.DoesNotContain("foo", "bar"),
9291
)),
9392
},
9493
{
@@ -98,8 +97,7 @@ CMD ["echo", "bar"]
9897
},
9998
Expected: test.Expects(expect.ExitCodeSuccess, nil, expect.All(
10099
expect.Contains("blah"),
101-
expect.DoesNotContain("foo"),
102-
expect.DoesNotContain("bar"),
100+
expect.DoesNotContain("foo", "bar"),
103101
)),
104102
},
105103
},
@@ -207,11 +205,11 @@ func TestRunEnvFile(t *testing.T) {
207205
testutil.CommonImage, "env")
208206
}
209207

210-
testCase.Expected = test.Expects(expect.ExitCodeSuccess, nil, expect.All(
211-
expect.Contains("TESTKEY1=TESTVAL1"),
212-
expect.Contains("TESTKEY2=TESTVAL2"),
213-
expect.Contains("HOST_ENV=ENV-IN-HOST"),
214-
))
208+
testCase.Expected = test.Expects(
209+
expect.ExitCodeSuccess,
210+
nil,
211+
expect.Contains("TESTKEY1=TESTVAL1", "TESTKEY2=TESTVAL2", "HOST_ENV=ENV-IN-HOST"),
212+
)
215213

216214
testCase.Run(t)
217215
}
@@ -240,20 +238,24 @@ func TestRunEnv(t *testing.T) {
240238
}
241239

242240
validate := []test.Comparator{
243-
expect.Contains("\nFOO=foo1,foo2\n"),
244-
expect.Contains("\nBAR=bar1 bar2\n"),
241+
expect.Contains(
242+
"\nFOO=foo1,foo2\n",
243+
"\nBAR=bar1 bar2\n",
244+
"\nQUUX=quux2\n",
245+
"\nCORGE=corge-value-in-host\n",
246+
"\nGRAULT=grault_key=grault_value\n",
247+
),
245248
expect.DoesNotContain("QUX"),
246-
expect.Contains("\nQUUX=quux2\n"),
247-
expect.Contains("\nCORGE=corge-value-in-host\n"),
248-
expect.Contains("\nGRAULT=grault_key=grault_value\n"),
249249
}
250250

251251
if runtime.GOOS != "windows" {
252252
validate = append(
253253
validate,
254-
expect.Contains("\nBAZ=\n"),
255-
expect.Contains("\nGARPLY=\n"),
256-
expect.Contains("\nWALDO=\n"),
254+
expect.Contains(
255+
"\nBAZ=\n",
256+
"\nGARPLY=\n",
257+
"\nWALDO=\n",
258+
),
257259
)
258260
}
259261

cmd/nerdctl/image/image_list_test.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,11 @@ RUN echo "actually creating a layer so that docker sets the createdAt time"
223223
{
224224
Description: "reference=tagged*:*fragment*",
225225
Command: test.Command("images", "--filter", "reference=tagged*:*fragment*"),
226-
Expected: test.Expects(0, nil, expect.All(
227-
expect.Contains("one-"),
228-
expect.Contains("two-"),
229-
)),
226+
Expected: test.Expects(
227+
0,
228+
nil,
229+
expect.Contains("one-", "two-"),
230+
),
230231
},
231232
{
232233
Description: "before=ID:latest",
@@ -259,9 +260,9 @@ RUN echo "actually creating a layer so that docker sets the createdAt time"
259260
Command: test.Command("images", "--filter", fmt.Sprintf("since=%s", testutil.CommonImage), testutil.CommonImage),
260261
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
261262
return &test.Expected{
262-
Output: expect.All(
263-
expect.DoesNotContain(data.Labels().Get("builtImageID")),
264-
expect.DoesNotContain(testutil.ImageRepo(testutil.CommonImage)),
263+
Output: expect.DoesNotContain(
264+
data.Labels().Get("builtImageID"),
265+
testutil.ImageRepo(testutil.CommonImage),
265266
),
266267
}
267268
},

cmd/nerdctl/image/image_remove_test.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,8 @@ func TestRemove(t *testing.T) {
186186
return &test.Expected{
187187
Output: func(stdout string, info string, t *testing.T) {
188188
helpers.Command("images").Run(&test.Expected{
189-
Output: expect.All(
190-
expect.DoesNotContain(repoName),
191-
// a created container with removed image doesn't impact other `rmi` command
192-
expect.DoesNotContain(nginxRepoName),
193-
),
189+
// a created container with removed image doesn't impact other `rmi` command
190+
Output: expect.DoesNotContain(repoName, nginxRepoName),
194191
})
195192
},
196193
}

cmd/nerdctl/volume/volume_inspect_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,7 @@ func TestVolumeInspect(t *testing.T) {
152152
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
153153
return &test.Expected{
154154
Output: expect.All(
155-
expect.Contains(data.Labels().Get("vol1")),
156-
expect.Contains(data.Labels().Get("vol2")),
155+
expect.Contains(data.Labels().Get("vol1"), data.Labels().Get("vol2")),
157156
expect.JSON([]native.Volume{}, func(dc []native.Volume, info string, t tig.T) {
158157
assert.Assert(t, len(dc) == 2, fmt.Sprintf("two results, not %d", len(dc)))
159158
assert.Assert(t, dc[0].Name == data.Labels().Get("vol1"), fmt.Sprintf("expected name to be %q (was %q)", data.Labels().Get("vol1"), dc[0].Name))

cmd/nerdctl/volume/volume_prune_linux_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,12 @@ func TestVolumePrune(t *testing.T) {
6969
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
7070
return &test.Expected{
7171
Output: expect.All(
72-
expect.DoesNotContain(data.Labels().Get("anonIDBusy")),
7372
expect.Contains(data.Labels().Get("anonIDDangling")),
74-
expect.DoesNotContain(data.Labels().Get("namedBusy")),
75-
expect.DoesNotContain(data.Labels().Get("namedDangling")),
73+
expect.DoesNotContain(
74+
data.Labels().Get("anonIDBusy"),
75+
data.Labels().Get("namedBusy"),
76+
data.Labels().Get("namedDangling"),
77+
),
7678
func(stdout string, info string, t *testing.T) {
7779
helpers.Ensure("volume", "inspect", data.Labels().Get("anonIDBusy"))
7880
helpers.Fail("volume", "inspect", data.Labels().Get("anonIDDangling"))
@@ -92,10 +94,8 @@ func TestVolumePrune(t *testing.T) {
9294
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
9395
return &test.Expected{
9496
Output: expect.All(
95-
expect.DoesNotContain(data.Labels().Get("anonIDBusy")),
96-
expect.Contains(data.Labels().Get("anonIDDangling")),
97-
expect.DoesNotContain(data.Labels().Get("namedBusy")),
98-
expect.Contains(data.Labels().Get("namedDangling")),
97+
expect.DoesNotContain(data.Labels().Get("anonIDBusy"), data.Labels().Get("namedBusy")),
98+
expect.Contains(data.Labels().Get("anonIDDangling"), data.Labels().Get("namedDangling")),
9999
func(stdout string, info string, t *testing.T) {
100100
helpers.Ensure("volume", "inspect", data.Labels().Get("anonIDBusy"))
101101
helpers.Fail("volume", "inspect", data.Labels().Get("anonIDDangling"))

mod/tigron/expect/comparators.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,28 @@ func All(comparators ...test.Comparator) test.Comparator {
4141

4242
// Contains can be used as a parameter for expected.Output and ensures a comparison string is found contained in the
4343
// output.
44-
func Contains(compare string) test.Comparator {
44+
func Contains(compare string, more ...string) test.Comparator {
4545
return func(stdout, _ string, t *testing.T) {
4646
t.Helper()
47+
4748
assertive.Contains(assertive.WithFailLater(t), stdout, compare, "Inspecting output (contains)")
49+
50+
for _, m := range more {
51+
assertive.Contains(assertive.WithFailLater(t), stdout, m, "Inspecting output (contains)")
52+
}
4853
}
4954
}
5055

5156
// DoesNotContain is to be used for expected.Output to ensure a comparison string is NOT found in the output.
52-
func DoesNotContain(compare string) test.Comparator {
57+
func DoesNotContain(compare string, more ...string) test.Comparator {
5358
return func(stdout, _ string, t *testing.T) {
5459
t.Helper()
60+
5561
assertive.DoesNotContain(assertive.WithFailLater(t), stdout, compare, "Inspecting output (does not contain)")
62+
63+
for _, m := range more {
64+
assertive.DoesNotContain(assertive.WithFailLater(t), stdout, m, "Inspecting output (does not contain)")
65+
}
5666
}
5767
}
5868

mod/tigron/expect/comparators_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
)
3131

3232
func TestExpect(t *testing.T) {
33+
// TODO: write more tests once we can mock t in Comparator signature
3334
t.Parallel()
3435

3536
expect.Contains("b")("a b c", "contains works", t)
@@ -39,7 +40,9 @@ func TestExpect(t *testing.T) {
3940

4041
expect.All(
4142
expect.Contains("b"),
43+
expect.Contains("b", "c"),
4244
expect.DoesNotContain("d"),
45+
expect.DoesNotContain("d", "e"),
4346
expect.Equals("a b c"),
4447
expect.Match(regexp.MustCompile("[a-z ]+")),
4548
)("a b c", "all", t)

0 commit comments

Comments
 (0)