forked from acronis/go-appkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
63 lines (55 loc) · 1.46 KB
/
Copy patherrors.go
File metadata and controls
63 lines (55 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
Copyright © 2024 Acronis International GmbH.
Released under MIT license.
*/
package testutil
import (
"errors"
"fmt"
"strings"
"github.com/stretchr/testify/require"
)
// RequireNoErrorInChannel asserts that there is an error in buffered channel.
func RequireNoErrorInChannel(t require.TestingT, c <-chan error, msgAndArgs ...interface{}) {
if h, ok := t.(tHelper); ok {
h.Helper()
}
var err error
select {
case err = <-c:
default:
}
require.NoError(t, err, msgAndArgs...)
}
// RequireErrorIsAny asserts that at least one of the errors in err's chain matches at least one target.
// This is a wrapper for errors.Is.
func RequireErrorIsAny(t require.TestingT, err error, targets []error, msgAndArgs ...interface{}) {
if h, ok := t.(tHelper); ok {
h.Helper()
}
for _, targetErr := range targets {
if errors.Is(err, targetErr) {
return
}
}
var expectedErrTexts []string
for _, targetErr := range targets {
expectedErrTexts = append(expectedErrTexts, fmt.Sprintf("%q", targetErr.Error()))
}
require.FailNow(t, fmt.Sprintf("At least one target error should be in err chain:\n"+
"expected: [%s]\n"+
"in chain: %s", strings.Join(expectedErrTexts, "; "), buildErrorChainString(err),
), msgAndArgs...)
}
func buildErrorChainString(err error) string {
if err == nil {
return ""
}
e := errors.Unwrap(err)
chain := fmt.Sprintf("%q", err.Error())
for e != nil {
chain += fmt.Sprintf("\n\t%q", e.Error())
e = errors.Unwrap(e)
}
return chain
}