@@ -9,160 +9,42 @@ import (
99
1010func TestStacktraceIntegration (t * testing.T ) {
1111 err := run ([]string {"testdata" })
12-
1312 if err == nil {
1413 t .Fatal ("expected error from run" )
1514 }
16-
17- // Use ErrorHandler to extract stacktrace
18- handler := rogerr .NewErrorHandler ()
19- frames := handler .Stacktrace (err )
20- if len (frames ) == 0 {
21- t .Fatal ("expected stacktrace frames" )
22- }
23-
24- // Log all frames for debugging
25- t .Log ("Captured stacktrace frames:" )
26- for i , frame := range frames {
27- t .Logf (" [%d] %s (%s:%d) InApp=%v" , i , frame .Function , frame .File , frame .Line , frame .InApp )
28- }
29-
30- // Define expected frames in reverse order (closest to error first)
31- expectedFrames := []struct {
32- functionPattern string
33- filePattern string
34- inApp bool
35- description string
36- }{
37- {
38- functionPattern : "executeBusinessLogic.func1" ,
39- filePattern : "/internal/myapp/pkg/service/processing.go" ,
40- inApp : true ,
41- description : "anonymous function in service package" ,
42- },
43- {
44- functionPattern : "executeBusinessLogic" ,
45- filePattern : "/internal/myapp/pkg/service/processing.go" ,
46- inApp : true ,
47- description : "method on ProcessingService" ,
48- },
49- {
50- functionPattern : "ProcessData" ,
51- filePattern : "/internal/myapp/pkg/service/processing.go" ,
52- inApp : true ,
53- description : "method on ProcessingService" ,
54- },
55- {
56- functionPattern : "processRequest.func1" ,
57- filePattern : "/internal/myapp/pkg/handler/request.go" ,
58- inApp : true ,
59- description : "anonymous function in handler package" ,
60- },
61- {
62- functionPattern : "processRequest" ,
63- filePattern : "/internal/myapp/pkg/handler/request.go" ,
64- inApp : true ,
65- description : "method on RequestHandler" ,
66- },
67- {
68- functionPattern : "HandleRequest" ,
69- filePattern : "/internal/myapp/pkg/handler/request.go" ,
70- inApp : true ,
71- description : "method on RequestHandler" ,
72- },
73- {
74- functionPattern : "Execute" ,
75- filePattern : "/internal/myapp/cmd/app.go" ,
76- inApp : true ,
77- description : "method on App struct" ,
78- },
79- {
80- functionPattern : "run" ,
81- filePattern : "/internal/myapp/main.go" ,
82- inApp : true ,
83- description : "main package function" ,
84- },
85- }
86-
87- // Validate that myapp frames are marked as InApp=true
88- myappFrameCount := 0
15+
16+ frames := rogerr .NewErrorHandler ().Stacktrace (err )
17+ var myappFrames , mylibFrames , rogerrFrames int
18+
8919 for _ , frame := range frames {
90- if strings .Contains (frame .File , "/internal/myapp/" ) {
91- myappFrameCount ++
20+ if frame .File == "" || frame .Function == "" || frame .Line <= 0 {
21+ t .Errorf ("Invalid frame: %+v" , frame )
22+ }
23+
24+ switch {
25+ case strings .Contains (frame .File , "/internal/myapp/" ):
26+ myappFrames ++
9227 if ! frame .InApp {
93- t .Errorf ("Frame from myapp should be InApp=true: %s (%s:%d)" ,
94- frame .Function , frame .File , frame .Line )
28+ t .Errorf ("myapp frame should be InApp=true: %s" , frame .Function )
9529 }
96- }
97- }
98-
99- if myappFrameCount == 0 {
100- t .Error ("expected to find frames from myapp module" )
101- }
102-
103- // Validate that mylib frames are marked as InApp=false
104- mylibFrameCount := 0
105- for _ , frame := range frames {
106- if strings .Contains (frame .File , "/internal/mylib/" ) {
107- mylibFrameCount ++
30+ case strings .Contains (frame .File , "/internal/mylib/" ):
31+ mylibFrames ++
10832 if frame .InApp {
109- t .Errorf ("Frame from mylib should be InApp=false: %s (%s:%d)" ,
110- frame .Function , frame .File , frame .Line )
33+ t .Errorf ("mylib frame should be InApp=false: %s" , frame .Function )
11134 }
35+ case strings .HasPrefix (frame .Function , "github.com/kinbiko/rogerr." ):
36+ rogerrFrames ++
37+ t .Errorf ("Found rogerr main package frame: %s" , frame .Function )
11238 }
11339 }
114-
115- if mylibFrameCount == 0 {
116- t .Error ("expected to find frames from mylib module" )
117- }
118-
119- // Validate specific function patterns exist (only for myapp frames)
120- foundFunctions := make (map [string ]bool )
121- for _ , frame := range frames {
122- // Skip testing framework frames and mylib frames for our validation
123- if strings .Contains (frame .Function , "testing." ) ||
124- strings .Contains (frame .Function , "runtime." ) ||
125- strings .Contains (frame .Function , "github.com/kinbiko/rogerr/internal/mylib" ) {
126- continue
127- }
128-
129- for _ , expected := range expectedFrames {
130- if strings .Contains (frame .Function , expected .functionPattern ) {
131- foundFunctions [expected .functionPattern ] = true
132-
133- // Validate file path
134- if ! strings .Contains (frame .File , expected .filePattern ) {
135- t .Errorf ("Frame %s should be in file containing %s, got %s" ,
136- frame .Function , expected .filePattern , frame .File )
137- }
138-
139- // Validate InApp status
140- if frame .InApp != expected .inApp {
141- t .Errorf ("Frame %s should have InApp=%v, got %v" ,
142- frame .Function , expected .inApp , frame .InApp )
143- }
144-
145- // Validate line number is reasonable
146- if frame .Line <= 0 {
147- t .Errorf ("Frame %s should have positive line number, got %d" ,
148- frame .Function , frame .Line )
149- }
150- }
151- }
40+
41+ if myappFrames != 9 {
42+ t .Errorf ("expected 9 frames from myapp module, got %d" , myappFrames )
15243 }
153-
154- // Check that we found all expected function patterns
155- for _ , expected := range expectedFrames {
156- if ! foundFunctions [expected .functionPattern ] {
157- t .Errorf ("Expected to find function containing '%s' (%s) in stacktrace" ,
158- expected .functionPattern , expected .description )
159- }
44+ if mylibFrames != 5 {
45+ t .Errorf ("expected 5 frames from mylib module, got %d" , mylibFrames )
16046 }
161-
162- // Validate that no rogerr main package frames are present
163- for _ , frame := range frames {
164- if strings .HasPrefix (frame .Function , "github.com/kinbiko/rogerr." ) {
165- t .Errorf ("Found rogerr main package frame in stacktrace: %s" , frame .Function )
166- }
47+ if rogerrFrames != 0 {
48+ t .Errorf ("expected 0 frames from rogerr module, got %d" , rogerrFrames )
16749 }
168- }
50+ }
0 commit comments