Skip to content

Commit 29c8643

Browse files
committed
Remove all go 1.20 features from test usage
This commit removes all test code that references: - go 1.20 `errors.Join` function. - go 1.20 multi-cause `fmt.Errorf` that contain multiple `%w` formatting directives. This commit also reverts go.mod to use 1.17 to match the state of the codebase from before the go 1.20 upgrade. This allows older codebases to upgrade to a version of this library that can encode/decode multi-cause errors from newer versions without completely discarding the child cause error info.
1 parent ca59e56 commit 29c8643

20 files changed

+1638
-3032
lines changed

.github/workflows/ci.yaml

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ name: Go
22

33
on:
44
push:
5-
branches: [ master ]
5+
branches: [ master, go-1.17-compat ]
66
pull_request:
7-
branches: [ master ]
7+
branches: [ master, go-1.17-compat ]
88

99
jobs:
1010

@@ -13,8 +13,10 @@ jobs:
1313
strategy:
1414
matrix:
1515
go:
16+
- "1.17"
17+
- "1.18"
18+
- "1.19"
1619
- "1.20"
17-
- "1.21"
1820
steps:
1921
- uses: actions/checkout@v2
2022

errbase/adapters_test.go

-25
Original file line numberDiff line numberDiff line change
@@ -68,31 +68,6 @@ func TestAdaptGoSingleWrapErr(t *testing.T) {
6868
tt.CheckContains(newErr.Error(), "hello")
6969
}
7070

71-
func TestAdaptBaseGoJoinErr(t *testing.T) {
72-
origErr := goErr.Join(goErr.New("hello"), goErr.New("world"))
73-
t.Logf("start err: %# v", pretty.Formatter(origErr))
74-
75-
newErr := network(t, origErr)
76-
77-
tt := testutils.T{T: t}
78-
// The library preserves the error message.
79-
tt.CheckEqual(newErr.Error(), origErr.Error())
80-
81-
}
82-
83-
func TestAdaptGoMultiWrapErr(t *testing.T) {
84-
origErr := fmt.Errorf("an error %w and also %w", goErr.New("hello"), goErr.New("world"))
85-
t.Logf("start err: %# v", pretty.Formatter(origErr))
86-
87-
newErr := network(t, origErr)
88-
89-
tt := testutils.T{T: t}
90-
// The library preserves the causes. It's not possible to preserve the fmt string.
91-
tt.CheckEqual(newErr.Error(), origErr.Error())
92-
tt.CheckContains(newErr.Error(), "hello")
93-
tt.CheckContains(newErr.Error(), "world")
94-
}
95-
9671
func TestAdaptPkgWithMessage(t *testing.T) {
9772
// Simple message wrappers from github.com/pkg/errors are preserved
9873
// completely.

errbase/format_error_internal_test.go

-97
Original file line numberDiff line numberDiff line change
@@ -130,18 +130,6 @@ func TestFormatErrorInternal(t *testing.T) {
130130
Wraps: (2) ab
131131
Wraps: (3) b
132132
Error types: (1) *fmt.wrapError (2) *fmt.wrapError (3) *errors.errorString`,
133-
},
134-
{
135-
name: "simple multi-wrapper",
136-
err: goErr.Join(goErr.New("a"), goErr.New("b")),
137-
expectedSimple: "a\nb",
138-
// TODO(davidh): verbose test case should have line break
139-
// between `a` and `b` on second line.
140-
expectedVerbose: `a
141-
(1) ab
142-
Wraps: (2) b
143-
Wraps: (3) a
144-
Error types: (1) *errors.joinError (2) *errors.errorString (3) *errors.errorString`,
145133
},
146134
{
147135
name: "multi-wrapper with custom formatter and partial elide",
@@ -190,91 +178,6 @@ Error types: (1) *errbase.wrapNoElideCauses (2) *errbase.wrapNoElideCauses (3) *
190178
| c
191179
| d
192180
Error types: (1) *errors.errorString`,
193-
},
194-
{
195-
name: "two-level multi-wrapper",
196-
err: goErr.Join(
197-
goErr.Join(goErr.New("a"), goErr.New("b")),
198-
goErr.Join(goErr.New("c"), goErr.New("d")),
199-
),
200-
expectedSimple: "a\nb\nc\nd",
201-
// TODO(davidh): verbose output should preserve line breaks after (1)
202-
// and also after (2) and (5) in `c\nd` and `a\nb`.
203-
expectedVerbose: `a
204-
(1) ab
205-
|
206-
| c
207-
| d
208-
Wraps: (2) cd
209-
└─ Wraps: (3) d
210-
└─ Wraps: (4) c
211-
Wraps: (5) ab
212-
└─ Wraps: (6) b
213-
└─ Wraps: (7) a
214-
Error types: (1) *errors.joinError (2) *errors.joinError (3) *errors.errorString (4) *errors.errorString (5) *errors.joinError (6) *errors.errorString (7) *errors.errorString`,
215-
},
216-
{
217-
name: "simple multi-wrapper with single-cause chains inside",
218-
err: goErr.Join(
219-
fmt.Errorf("a%w", goErr.New("b")),
220-
fmt.Errorf("c%w", goErr.New("d")),
221-
),
222-
expectedSimple: "ab\ncd",
223-
expectedVerbose: `ab
224-
(1) ab
225-
| cd
226-
Wraps: (2) cd
227-
└─ Wraps: (3) d
228-
Wraps: (4) ab
229-
└─ Wraps: (5) b
230-
Error types: (1) *errors.joinError (2) *fmt.wrapError (3) *errors.errorString (4) *fmt.wrapError (5) *errors.errorString`,
231-
},
232-
{
233-
name: "multi-cause wrapper with single-cause chains inside",
234-
err: goErr.Join(
235-
fmt.Errorf("a%w", fmt.Errorf("b%w", fmt.Errorf("c%w", goErr.New("d")))),
236-
fmt.Errorf("e%w", fmt.Errorf("f%w", fmt.Errorf("g%w", goErr.New("h")))),
237-
),
238-
expectedSimple: `abcd
239-
efgh`,
240-
expectedVerbose: `abcd
241-
(1) abcd
242-
| efgh
243-
Wraps: (2) efgh
244-
└─ Wraps: (3) fgh
245-
└─ Wraps: (4) gh
246-
└─ Wraps: (5) h
247-
Wraps: (6) abcd
248-
└─ Wraps: (7) bcd
249-
└─ Wraps: (8) cd
250-
└─ Wraps: (9) d
251-
Error types: (1) *errors.joinError (2) *fmt.wrapError (3) *fmt.wrapError (4) *fmt.wrapError (5) *errors.errorString (6) *fmt.wrapError (7) *fmt.wrapError (8) *fmt.wrapError (9) *errors.errorString`},
252-
{
253-
name: "single cause chain with multi-cause wrapper inside with single-cause chains inside",
254-
err: fmt.Errorf(
255-
"prefix1: %w",
256-
fmt.Errorf(
257-
"prefix2: %w",
258-
goErr.Join(
259-
fmt.Errorf("a%w", fmt.Errorf("b%w", fmt.Errorf("c%w", goErr.New("d")))),
260-
fmt.Errorf("e%w", fmt.Errorf("f%w", fmt.Errorf("g%w", goErr.New("h")))),
261-
))),
262-
expectedSimple: `prefix1: prefix2: abcd
263-
efgh`,
264-
expectedVerbose: `prefix1: prefix2: abcd
265-
(1) prefix1
266-
Wraps: (2) prefix2
267-
Wraps: (3) abcd
268-
| efgh
269-
└─ Wraps: (4) efgh
270-
└─ Wraps: (5) fgh
271-
└─ Wraps: (6) gh
272-
└─ Wraps: (7) h
273-
└─ Wraps: (8) abcd
274-
└─ Wraps: (9) bcd
275-
└─ Wraps: (10) cd
276-
└─ Wraps: (11) d
277-
Error types: (1) *fmt.wrapError (2) *fmt.wrapError (3) *errors.joinError (4) *fmt.wrapError (5) *fmt.wrapError (6) *fmt.wrapError (7) *errors.errorString (8) *fmt.wrapError (9) *fmt.wrapError (10) *fmt.wrapError (11) *errors.errorString`,
278181
},
279182
{
280183
name: "test wrapMini elides cause error string",

errbase/unwrap_test.go

-13
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package errbase_test
1616

1717
import (
1818
"errors"
19-
"fmt"
2019
"testing"
2120

2221
"github.com/cockroachdb/errors/errbase"
@@ -59,18 +58,6 @@ func TestMixedErrorWrapping(t *testing.T) {
5958
tt.CheckEqual(errbase.UnwrapAll(err3), err)
6059
}
6160

62-
func TestMultiErrorUnwrap(t *testing.T) {
63-
tt := testutils.T{T: t}
64-
65-
err := errors.New("hello")
66-
err2 := pkgErr.WithMessage(err, "woo")
67-
err3 := fmt.Errorf("%w %w", err, err2)
68-
69-
tt.CheckEqual(errbase.UnwrapOnce(err3), nil)
70-
tt.CheckEqual(errbase.UnwrapAll(err3), err3)
71-
tt.CheckDeepEqual(errbase.UnwrapMulti(err3), []error{err, err2})
72-
}
73-
7461
type myWrapper struct{ cause error }
7562

7663
func (w *myWrapper) Error() string { return w.cause.Error() }

errutil/as_test.go

+3-21
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package errutil_test
1616

1717
import (
18-
goErr "errors"
1918
"fmt"
2019
"testing"
2120

@@ -61,12 +60,6 @@ func TestAs(t *testing.T) {
6160
tt.Check(errors.As(multiWrapErr, &mywSlot))
6261
tt.Check(errors.Is(mywSlot, refwErr))
6362

64-
// Check that it works even if hidden in multi-cause wrapErrors
65-
multiWrapErr = fmt.Errorf("error: %w and %w", errors.Wrap(refwErr, "hidden"), errors.New("world"))
66-
mywSlot = nil
67-
tt.Check(errors.As(multiWrapErr, &mywSlot))
68-
tt.Check(errors.Is(mywSlot, refwErr))
69-
7063
// Check that it works even if hidden in custom multi-error
7164
multiWrapErr = &myMultiWrapper{
7265
causes: []error{errors.Wrap(refwErr, "hidden"), errors.New("world")},
@@ -75,19 +68,6 @@ func TestAs(t *testing.T) {
7568
mywSlot = nil
7669
tt.Check(errors.As(multiWrapErr, &mywSlot))
7770
tt.Check(errors.Is(mywSlot, refwErr))
78-
79-
// Check that it works even if hidden in a multi-level multi-cause chain
80-
multiWrapErr = fmt.Errorf("error: %w and %w",
81-
&myMultiWrapper{
82-
causes: []error{errors.New("ignoreme"), errors.New("also ignore")},
83-
msg: "red herring",
84-
}, &myMultiWrapper{
85-
causes: []error{errors.Wrap(refwErr, "hidden"), errors.New("world")},
86-
msg: "errors",
87-
})
88-
mywSlot = nil
89-
tt.Check(errors.As(multiWrapErr, &mywSlot))
90-
tt.Check(errors.Is(mywSlot, refwErr))
9171
}
9272

9373
type myType struct{ msg string }
@@ -106,7 +86,9 @@ type myMultiWrapper struct {
10686
msg string
10787
}
10888

109-
func (m *myMultiWrapper) Error() string { return fmt.Sprintf("%s: %v", m.msg, goErr.Join(m.causes...)) }
89+
func (m *myMultiWrapper) Error() string {
90+
return fmt.Sprintf("%s: %v", m.msg, errors.Join(m.causes...))
91+
}
11092

11193
func (m *myMultiWrapper) Unwrap() []error {
11294
return m.causes

errutil_api_test.go

-11
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,8 @@ import (
66
"testing"
77

88
"github.com/cockroachdb/errors"
9-
"github.com/cockroachdb/errors/testutils"
109
)
1110

12-
func TestUnwrap(t *testing.T) {
13-
tt := testutils.T{t}
14-
15-
e := fmt.Errorf("foo %w %w", fmt.Errorf("bar"), fmt.Errorf("baz"))
16-
17-
// Compatibility with go 1.20: Unwrap() on a multierror returns nil
18-
// (per API documentation)
19-
tt.Check(errors.Unwrap(e) == nil)
20-
}
21-
2211
// More detailed testing of Join is in datadriven_test.go. Here we make
2312
// sure that the public API includes the stacktrace wrapper.
2413
func TestJoin(t *testing.T) {

fmttests/datadriven_test.go

-3
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,6 @@ var wrapCommands = map[string]commandFn{
158158
"go-errorf-suffix": func(e error, args []arg) error {
159159
return fmt.Errorf("%w - %s", e, strfy(args))
160160
},
161-
"go-errorf-multi": func(err error, args []arg) error {
162-
return fmt.Errorf("%s - %w %w", strfy(args), err, pkgErr.New("sibling error in wrapper"))
163-
},
164161
"opaque": func(err error, _ []arg) error {
165162
return errbase.DecodeError(context.Background(),
166163
errbase.EncodeError(context.Background(), err))

0 commit comments

Comments
 (0)