Skip to content
This repository was archived by the owner on Dec 1, 2021. It is now read-only.

Commit 30136e2

Browse files
philoserfdavecheney
authored andcommitted
Remove deadcode (#146)
1 parent e881fd5 commit 30136e2

File tree

2 files changed

+12
-70
lines changed

2 files changed

+12
-70
lines changed

stack.go

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -145,43 +145,3 @@ func funcname(name string) string {
145145
i = strings.Index(name, ".")
146146
return name[i+1:]
147147
}
148-
149-
func trimGOPATH(name, file string) string {
150-
// Here we want to get the source file path relative to the compile time
151-
// GOPATH. As of Go 1.6.x there is no direct way to know the compiled
152-
// GOPATH at runtime, but we can infer the number of path segments in the
153-
// GOPATH. We note that fn.Name() returns the function name qualified by
154-
// the import path, which does not include the GOPATH. Thus we can trim
155-
// segments from the beginning of the file path until the number of path
156-
// separators remaining is one more than the number of path separators in
157-
// the function name. For example, given:
158-
//
159-
// GOPATH /home/user
160-
// file /home/user/src/pkg/sub/file.go
161-
// fn.Name() pkg/sub.Type.Method
162-
//
163-
// We want to produce:
164-
//
165-
// pkg/sub/file.go
166-
//
167-
// From this we can easily see that fn.Name() has one less path separator
168-
// than our desired output. We count separators from the end of the file
169-
// path until it finds two more than in the function name and then move
170-
// one character forward to preserve the initial path segment without a
171-
// leading separator.
172-
const sep = "/"
173-
goal := strings.Count(name, sep) + 2
174-
i := len(file)
175-
for n := 0; n < goal; n++ {
176-
i = strings.LastIndex(file[:i], sep)
177-
if i == -1 {
178-
// not enough separators found, set i so that the slice expression
179-
// below leaves file unmodified
180-
i = -len(sep)
181-
break
182-
}
183-
}
184-
// get back to 0 or trim the leading separator
185-
file = file[i+len(sep):]
186-
return file
187-
}

stack_test.go

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -146,49 +146,31 @@ func TestFuncname(t *testing.T) {
146146
}
147147
}
148148

149-
func TestTrimGOPATH(t *testing.T) {
150-
var tests = []struct {
151-
Frame
152-
want string
153-
}{{
154-
Frame(initpc),
155-
"github.com/pkg/errors/stack_test.go",
156-
}}
157-
158-
for i, tt := range tests {
159-
pc := tt.Frame.pc()
160-
fn := runtime.FuncForPC(pc)
161-
file, _ := fn.FileLine(pc)
162-
got := trimGOPATH(fn.Name(), file)
163-
testFormatRegexp(t, i, got, "%s", tt.want)
164-
}
165-
}
166-
167149
func TestStackTrace(t *testing.T) {
168150
tests := []struct {
169151
err error
170152
want []string
171153
}{{
172154
New("ooh"), []string{
173155
"github.com/pkg/errors.TestStackTrace\n" +
174-
"\t.+/github.com/pkg/errors/stack_test.go:172",
156+
"\t.+/github.com/pkg/errors/stack_test.go:154",
175157
},
176158
}, {
177159
Wrap(New("ooh"), "ahh"), []string{
178160
"github.com/pkg/errors.TestStackTrace\n" +
179-
"\t.+/github.com/pkg/errors/stack_test.go:177", // this is the stack of Wrap, not New
161+
"\t.+/github.com/pkg/errors/stack_test.go:159", // this is the stack of Wrap, not New
180162
},
181163
}, {
182164
Cause(Wrap(New("ooh"), "ahh")), []string{
183165
"github.com/pkg/errors.TestStackTrace\n" +
184-
"\t.+/github.com/pkg/errors/stack_test.go:182", // this is the stack of New
166+
"\t.+/github.com/pkg/errors/stack_test.go:164", // this is the stack of New
185167
},
186168
}, {
187169
func() error { return New("ooh") }(), []string{
188170
`github.com/pkg/errors.(func·009|TestStackTrace.func1)` +
189-
"\n\t.+/github.com/pkg/errors/stack_test.go:187", // this is the stack of New
171+
"\n\t.+/github.com/pkg/errors/stack_test.go:169", // this is the stack of New
190172
"github.com/pkg/errors.TestStackTrace\n" +
191-
"\t.+/github.com/pkg/errors/stack_test.go:187", // this is the stack of New's caller
173+
"\t.+/github.com/pkg/errors/stack_test.go:169", // this is the stack of New's caller
192174
},
193175
}, {
194176
Cause(func() error {
@@ -197,11 +179,11 @@ func TestStackTrace(t *testing.T) {
197179
}()
198180
}()), []string{
199181
`github.com/pkg/errors.(func·010|TestStackTrace.func2.1)` +
200-
"\n\t.+/github.com/pkg/errors/stack_test.go:196", // this is the stack of Errorf
182+
"\n\t.+/github.com/pkg/errors/stack_test.go:178", // this is the stack of Errorf
201183
`github.com/pkg/errors.(func·011|TestStackTrace.func2)` +
202-
"\n\t.+/github.com/pkg/errors/stack_test.go:197", // this is the stack of Errorf's caller
184+
"\n\t.+/github.com/pkg/errors/stack_test.go:179", // this is the stack of Errorf's caller
203185
"github.com/pkg/errors.TestStackTrace\n" +
204-
"\t.+/github.com/pkg/errors/stack_test.go:198", // this is the stack of Errorf's caller's caller
186+
"\t.+/github.com/pkg/errors/stack_test.go:180", // this is the stack of Errorf's caller's caller
205187
},
206188
}}
207189
for i, tt := range tests {
@@ -271,19 +253,19 @@ func TestStackTraceFormat(t *testing.T) {
271253
}, {
272254
stackTrace()[:2],
273255
"%v",
274-
`\[stack_test.go:225 stack_test.go:272\]`,
256+
`\[stack_test.go:207 stack_test.go:254\]`,
275257
}, {
276258
stackTrace()[:2],
277259
"%+v",
278260
"\n" +
279261
"github.com/pkg/errors.stackTrace\n" +
280-
"\t.+/github.com/pkg/errors/stack_test.go:225\n" +
262+
"\t.+/github.com/pkg/errors/stack_test.go:207\n" +
281263
"github.com/pkg/errors.TestStackTraceFormat\n" +
282-
"\t.+/github.com/pkg/errors/stack_test.go:276",
264+
"\t.+/github.com/pkg/errors/stack_test.go:258",
283265
}, {
284266
stackTrace()[:2],
285267
"%#v",
286-
`\[\]errors.Frame{stack_test.go:225, stack_test.go:284}`,
268+
`\[\]errors.Frame{stack_test.go:207, stack_test.go:266}`,
287269
}}
288270

289271
for i, tt := range tests {

0 commit comments

Comments
 (0)