Skip to content

Commit 5bfe6eb

Browse files
authored
Merge pull request #4042 from apostasie/ci-tigron-break-2
[CI] Tigron breakout 2: remove gotest.tools assert dependency internally
2 parents 3a6ceb2 + 4ad52bc commit 5bfe6eb

File tree

6 files changed

+263
-16
lines changed

6 files changed

+263
-16
lines changed

mod/tigron/expect/comparators.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ import (
2222
"strings"
2323
"testing"
2424

25-
"gotest.tools/v3/assert"
26-
25+
"github.com/containerd/nerdctl/mod/tigron/internal/assertive"
2726
"github.com/containerd/nerdctl/mod/tigron/test"
2827
)
2928

@@ -45,7 +44,7 @@ func Contains(compare string) test.Comparator {
4544
//nolint:thelper
4645
return func(stdout, info string, t *testing.T) {
4746
t.Helper()
48-
assert.Check(t, strings.Contains(stdout, compare),
47+
assertive.Check(t, strings.Contains(stdout, compare),
4948
fmt.Sprintf("Output does not contain: %q", compare)+info)
5049
}
5150
}
@@ -56,8 +55,8 @@ func DoesNotContain(compare string) test.Comparator {
5655
//nolint:thelper
5756
return func(stdout, info string, t *testing.T) {
5857
t.Helper()
59-
assert.Check(t, !strings.Contains(stdout, compare),
60-
fmt.Sprintf("Output does contain: %q", compare)+info)
58+
assertive.Check(t, !strings.Contains(stdout, compare),
59+
fmt.Sprintf("Output should not contain: %q", compare)+info)
6160
}
6261
}
6362

@@ -66,7 +65,11 @@ func Equals(compare string) test.Comparator {
6665
//nolint:thelper
6766
return func(stdout, info string, t *testing.T) {
6867
t.Helper()
69-
assert.Equal(t, compare, stdout, info)
68+
assertive.Check(
69+
t,
70+
compare == stdout,
71+
fmt.Sprintf("Output is not equal to: %q", compare)+info,
72+
)
7073
}
7174
}
7275

@@ -76,6 +79,11 @@ func Match(reg *regexp.Regexp) test.Comparator {
7679
//nolint:thelper
7780
return func(stdout, info string, t *testing.T) {
7881
t.Helper()
79-
assert.Check(t, reg.MatchString(stdout), "Output does not match: "+reg.String(), info)
82+
assertive.Check(
83+
t,
84+
reg.MatchString(stdout),
85+
fmt.Sprintf("Output does not match: %q", reg.String()),
86+
info,
87+
)
8088
}
8189
}
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package assertive
18+
19+
import (
20+
"errors"
21+
"strings"
22+
"time"
23+
)
24+
25+
type testingT interface {
26+
Helper()
27+
FailNow()
28+
Fail()
29+
Log(args ...interface{})
30+
}
31+
32+
// ErrorIsNil immediately fails a test if err is not nil.
33+
func ErrorIsNil(t testingT, err error, msg ...string) {
34+
t.Helper()
35+
36+
if err != nil {
37+
t.Log("expecting nil error, but got:", err)
38+
failNow(t, msg...)
39+
}
40+
}
41+
42+
// ErrorIs immediately fails a test if err is not the comparison error.
43+
func ErrorIs(t testingT, err, compErr error, msg ...string) {
44+
t.Helper()
45+
46+
if !errors.Is(err, compErr) {
47+
t.Log("expected error to be:", compErr, "- instead it is:", err)
48+
failNow(t, msg...)
49+
}
50+
}
51+
52+
// IsEqual immediately fails a test if the two interfaces are not equal.
53+
func IsEqual(t testingT, actual, expected interface{}, msg ...string) {
54+
t.Helper()
55+
56+
if !isEqual(t, actual, expected) {
57+
t.Log("expected:", actual, " - to be equal to:", expected)
58+
failNow(t, msg...)
59+
}
60+
}
61+
62+
// IsNotEqual immediately fails a test if the two interfaces are equal.
63+
func IsNotEqual(t testingT, actual, expected interface{}, msg ...string) {
64+
t.Helper()
65+
66+
if isEqual(t, actual, expected) {
67+
t.Log("expected:", actual, " - to be equal to:", expected)
68+
failNow(t, msg...)
69+
}
70+
}
71+
72+
// StringContains immediately fails a test if the actual string does not contain the other string.
73+
func StringContains(t testingT, actual, contains string, msg ...string) {
74+
t.Helper()
75+
76+
if !strings.Contains(actual, contains) {
77+
t.Log("expected:", actual, " - to contain:", contains)
78+
failNow(t, msg...)
79+
}
80+
}
81+
82+
// StringDoesNotContain immediately fails a test if the actual string contains the other string.
83+
func StringDoesNotContain(t testingT, actual, contains string, msg ...string) {
84+
t.Helper()
85+
86+
if strings.Contains(actual, contains) {
87+
t.Log("expected:", actual, " - to NOT contain:", contains)
88+
failNow(t, msg...)
89+
}
90+
}
91+
92+
// StringHasSuffix immediately fails a test if the string does not end with suffix.
93+
func StringHasSuffix(t testingT, actual, suffix string, msg ...string) {
94+
t.Helper()
95+
96+
if !strings.HasSuffix(actual, suffix) {
97+
t.Log("expected:", actual, " - to end with:", suffix)
98+
failNow(t, msg...)
99+
}
100+
}
101+
102+
// StringHasPrefix immediately fails a test if the string does not start with prefix.
103+
func StringHasPrefix(t testingT, actual, prefix string, msg ...string) {
104+
t.Helper()
105+
106+
if !strings.HasPrefix(actual, prefix) {
107+
t.Log("expected:", actual, " - to start with:", prefix)
108+
failNow(t, msg...)
109+
}
110+
}
111+
112+
// DurationIsLessThan immediately fails a test if the duration is more than the reference.
113+
func DurationIsLessThan(t testingT, actual, expected time.Duration, msg ...string) {
114+
t.Helper()
115+
116+
if actual >= expected {
117+
t.Log("expected:", actual, " - to be less than:", expected)
118+
failNow(t, msg...)
119+
}
120+
}
121+
122+
// True immediately fails a test if the boolean is not true...
123+
func True(t testingT, comp bool, msg ...string) bool {
124+
t.Helper()
125+
126+
if !comp {
127+
failNow(t, msg...)
128+
}
129+
130+
return comp
131+
}
132+
133+
// Check marks a test as failed if the boolean is not true (safe in go routines)
134+
//
135+
//nolint:varnamelen
136+
func Check(t testingT, comp bool, msg ...string) bool {
137+
t.Helper()
138+
139+
if !comp {
140+
for _, m := range msg {
141+
t.Log(m)
142+
}
143+
144+
t.Fail()
145+
}
146+
147+
return comp
148+
}
149+
150+
//nolint:varnamelen
151+
func failNow(t testingT, msg ...string) {
152+
t.Helper()
153+
154+
if len(msg) > 0 {
155+
for _, m := range msg {
156+
t.Log(m)
157+
}
158+
}
159+
160+
t.FailNow()
161+
}
162+
163+
func isEqual(t testingT, actual, expected interface{}) bool {
164+
t.Helper()
165+
166+
// FIXME: this is risky and limited. Right now this is fine internally, but do better if this
167+
// becomes public.
168+
return actual == expected
169+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package assertive_test
18+
19+
import (
20+
"errors"
21+
"fmt"
22+
"testing"
23+
24+
"github.com/containerd/nerdctl/mod/tigron/internal/assertive"
25+
)
26+
27+
func TestY(t *testing.T) {
28+
t.Parallel()
29+
30+
var err error
31+
32+
assertive.ErrorIsNil(t, err)
33+
34+
//nolint:err113
35+
someErr := errors.New("test error")
36+
37+
err = fmt.Errorf("wrap: %w", someErr)
38+
assertive.ErrorIs(t, err, someErr)
39+
40+
foo := "foo"
41+
assertive.IsEqual(t, foo, "foo")
42+
43+
bar := 10
44+
assertive.IsEqual(t, bar, 10)
45+
46+
baz := true
47+
assertive.IsEqual(t, baz, true)
48+
}

mod/tigron/internal/assertive/doc.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Package assertive is an experimental, zero-dependencies assert library.
18+
// Right now, it is not public and meant to be used only inside tigron.
19+
// Consumers of tigron are free to use whatever assert library they want.
20+
// In the future, this may become public for peeps who want `assert` to be
21+
// bundled in.
22+
package assertive

mod/tigron/test/case.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"slices"
2121
"testing"
2222

23-
"gotest.tools/v3/assert"
23+
"github.com/containerd/nerdctl/mod/tigron/internal/assertive"
2424
)
2525

2626
// Case describes an entire test-case, including data, setup and cleanup routines, command and
@@ -72,16 +72,16 @@ func (test *Case) Run(t *testing.T) {
7272
testRun := func(subT *testing.T) {
7373
subT.Helper()
7474

75-
assert.Assert(subT, test.t == nil, "You cannot run a test multiple times")
75+
assertive.True(subT, test.t == nil, "You cannot run a test multiple times")
7676

7777
// Attach testing.T
7878
test.t = subT
79-
assert.Assert(
79+
assertive.True(
8080
test.t,
8181
test.Description != "" || test.parent == nil,
8282
"A test description cannot be empty",
8383
)
84-
assert.Assert(test.t, test.Command == nil || test.Expected != nil,
84+
assertive.True(test.t, test.Command == nil || test.Expected != nil,
8585
"Expectations for a test command cannot be nil. You may want to use Setup instead.")
8686

8787
// Ensure we have env

mod/tigron/test/command.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ import (
2828

2929
"golang.org/x/sync/errgroup"
3030
"golang.org/x/term"
31-
"gotest.tools/v3/assert"
3231
"gotest.tools/v3/icmd"
3332

33+
"github.com/containerd/nerdctl/mod/tigron/internal/assertive"
3434
"github.com/containerd/nerdctl/mod/tigron/test/internal"
3535
"github.com/containerd/nerdctl/mod/tigron/test/internal/pty"
3636
)
@@ -215,19 +215,19 @@ func (gc *GenericCommand) Run(expect *Expected) {
215215
// success, or a timeout.
216216
case internal.ExitCodeGenericFail:
217217
// ExitCodeGenericFail means we expect an error (excluding timeout).
218-
assert.Assert(gc.t, result.ExitCode != 0,
218+
assertive.True(gc.t, result.ExitCode != 0,
219219
"Expected exit code to be different than 0\n"+debug)
220220
case internal.ExitCodeTimeout:
221-
assert.Assert(gc.t, expect.ExitCode == internal.ExitCodeTimeout,
221+
assertive.True(gc.t, expect.ExitCode == internal.ExitCodeTimeout,
222222
"Command unexpectedly timed-out\n"+debug)
223223
default:
224-
assert.Assert(gc.t, expect.ExitCode == result.ExitCode,
224+
assertive.True(gc.t, expect.ExitCode == result.ExitCode,
225225
fmt.Sprintf("Expected exit code: %d\n", expect.ExitCode)+debug)
226226
}
227227

228228
// Range through the expected errors and confirm they are seen on stderr
229229
for _, expectErr := range expect.Errors {
230-
assert.Assert(gc.t, strings.Contains(gc.rawStdErr, expectErr.Error()),
230+
assertive.True(gc.t, strings.Contains(gc.rawStdErr, expectErr.Error()),
231231
fmt.Sprintf("Expected error: %q to be found in stderr\n", expectErr.Error())+debug)
232232
}
233233

0 commit comments

Comments
 (0)