88 "log"
99 "net/http"
1010 "os"
11+ "strings"
1112 "testing"
1213
1314 "github.com/stretchr/testify/assert"
@@ -32,16 +33,19 @@ func (t *t1) f3() *Err {
3233
3334func TestStackTrace (t * testing.T ) {
3435 path , _ := os .Getwd ()
36+ if os .Getenv ("GOPATH" ) != "" {
37+ path = strings .Replace (path , os .Getenv ("GOPATH" )+ "/src/" , "" , - 1 )
38+ }
3539 ts := t1 {}
3640 err := ts .f3 ()
3741
3842 assert .NotNil (t , err )
3943 assert .Equal (t , "message" , err .Error ())
4044 assert .Equal (t ,
4145 fmt .Sprintf (`Error Stacktrace:
42- -> %[1]s/goerr_test.go:36 (stackerr.TestStackTrace)
43- -> %[1]s/goerr_test.go:30 (stackerr.(*t1).f3)
44- -> %[1]s/goerr_test.go:23 (stackerr.f2) context
46+ -> %[1]s/goerr_test.go:40 (stackerr.TestStackTrace)
47+ -> %[1]s/goerr_test.go:31 (stackerr.(*t1).f3)
48+ -> %[1]s/goerr_test.go:24 (stackerr.f2) context
4549-> %[1]s/goerr_test.go:18 (stackerr.f1)
4650` , path ), err .Sprint ())
4751}
@@ -77,15 +81,19 @@ func TestNewWithStatusCode(t *testing.T) {
7781}
7882func TestLog (t * testing.T ) {
7983 path , _ := os .Getwd ()
84+ if os .Getenv ("GOPATH" ) != "" {
85+ path = strings .Replace (path , os .Getenv ("GOPATH" )+ "/src/" , "" , - 1 )
86+ }
87+
8088 var buf bytes.Buffer
8189 log .SetOutput (& buf )
8290 err := f2 ()
8391 err .Log ()
8492 log .SetOutput (os .Stderr )
8593 assert .Contains (t , buf .String (),
8694 fmt .Sprintf (`Error Stacktrace:
87- -> %[1]s/goerr_test.go:83 (stackerr.TestLog)
88- -> %[1]s/goerr_test.go:23 (stackerr.f2) context
95+ -> %[1]s/goerr_test.go:91 (stackerr.TestLog)
96+ -> %[1]s/goerr_test.go:24 (stackerr.f2) context
8997-> %[1]s/goerr_test.go:18 (stackerr.f1)
9098` , path ))
9199}
@@ -102,6 +110,9 @@ func TestIsNotFound(t *testing.T) {
102110
103111func TestPrint (t * testing.T ) {
104112 path , _ := os .Getwd ()
113+ if os .Getenv ("GOPATH" ) != "" {
114+ path = strings .Replace (path , os .Getenv ("GOPATH" )+ "/src/" , "" , - 1 )
115+ }
105116 old := os .Stdout // keep backup of the real stdout
106117 r , w , _ := os .Pipe ()
107118 os .Stdout = w
@@ -122,8 +133,8 @@ func TestPrint(t *testing.T) {
122133
123134 assert .Equal (t , out ,
124135 fmt .Sprintf (`Error Stacktrace:
125- -> %[1]s/goerr_test.go:117 (stackerr.TestPrint)
126- -> %[1]s/goerr_test.go:23 (stackerr.f2) context
136+ -> %[1]s/goerr_test.go:128 (stackerr.TestPrint)
137+ -> %[1]s/goerr_test.go:24 (stackerr.f2) context
127138-> %[1]s/goerr_test.go:18 (stackerr.f1)
128139` , path ))
129140}
0 commit comments