Repository navigation
Expand file tree
/
Copy patherrors_unwrap_test.go
More file actions
120 lines (87 loc) · 2.84 KB
/
Copy patherrors_unwrap_test.go
File metadata and controls
120 lines (87 loc) · 2.84 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package gogenfilter
import (
"errors"
"fmt"
"os"
"testing"
)
func TestProjectRootErrorUnwrap(t *testing.T) {
t.Parallel()
t.Run("returns cause", func(t *testing.T) {
t.Parallel()
err := testProjectRootErrorWithErr(t, CodeProjectRootInvalidPath, "/path", os.ErrInvalid)
assertErrorsIs(t, err, os.ErrInvalid)
})
t.Run("returns nil for nil cause", func(t *testing.T) {
t.Parallel()
err := testProjectRootErrorNotFound(t)
if err.Unwrap() != nil {
t.Error("Expected nil unwrap for nil cause")
}
})
}
func TestProjectRootErrorUnwrapChainIntegration(t *testing.T) {
t.Parallel()
t.Run("unwraps through nested fmt.Errorf to sentinel", func(t *testing.T) {
t.Parallel()
inner := &ProjectRootError{
Code: CodeProjectRootInvalidPath,
StartPath: "/path",
Markers: []string{testMarkerGoMod},
Err: os.ErrPermission,
}
wrapped := fmt.Errorf("layer2: %w", fmt.Errorf("layer1: %w", inner))
extracted, ok := errors.AsType[*ProjectRootError](wrapped)
if !ok {
t.Fatal("errors.AsType should find ProjectRootError through nested wrapping")
}
assertEqual(t, "ErrorCode", extracted.ErrorCode(), CodeProjectRootInvalidPath)
assertEqual(t, "StartPath", extracted.StartPath, "/path")
})
t.Run("errors.Is reaches sentinel through multiple wraps", func(t *testing.T) {
t.Parallel()
inner := &ProjectRootError{
Code: CodeProjectRootNotFound,
StartPath: "/path",
Markers: []string{testMarkerGoMod},
Err: nil,
}
wrapped := fmt.Errorf("outer: %w", inner)
assertErrorsIs(t, wrapped, ErrProjectRootNotFound)
})
}
func TestSQLCConfigErrorUnwrap(t *testing.T) {
t.Parallel()
err := newSQLCConfigErrorRead("", "reading sqlc config")
assertErrorsIs(t, err, os.ErrPermission)
}
func TestSQLCConfigErrorUnwrapChainIntegration(t *testing.T) {
t.Parallel()
t.Run("unwraps collect error to inner parse error", testSQLCUnwrapCollectToInner)
t.Run("errors.Is matches collect sentinel", testSQLCUnwrapCollectSentinel)
t.Run(
"errors.Is reaches inner parse sentinel through Unwrap chain",
testSQLCUnwrapInnerSentinel,
)
}
func testSQLCUnwrapCollectToInner(t *testing.T) {
t.Parallel()
_, collectErr := sqlcConfigUnwrapTestSetup()
inner, ok := errors.AsType[*SQLCConfigError](collectErr.Unwrap())
if !ok {
t.Fatal("Unwrap() should expose inner SQLCConfigError")
}
assertEqual(t, "inner ErrorCode", inner.ErrorCode(), CodeSQLCConfigParse)
assertEqual(t, "inner ConfigPath", inner.ConfigPath, "sqlc.yaml")
}
func testSQLCUnwrapCollectSentinel(t *testing.T) {
t.Parallel()
_, collectErr := sqlcConfigUnwrapTestSetup()
assertErrorsIs(t, collectErr, ErrSQLCConfigCollect)
}
func testSQLCUnwrapInnerSentinel(t *testing.T) {
t.Parallel()
_, collectErr := sqlcConfigUnwrapTestSetup()
assertErrorsIs(t, collectErr, ErrSQLCConfigCollect)
assertErrorsIs(t, collectErr, ErrSQLCConfigParse)
}