Skip to content

Commit dcebf30

Browse files
committed
panic: make it more user friendly
Implements -h / -help / --help / help Had to update a lot of hardcoded offsets in expectations.
1 parent 8efd67e commit dcebf30

3 files changed

Lines changed: 38 additions & 32 deletions

File tree

cmd/panic/main.go

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,14 @@ import (
4242

4343
func main() {
4444
if len(os.Args) == 2 {
45-
n := os.Args[1]
46-
if f, ok := types[n]; ok {
47-
fmt.Printf("GOTRACEBACK=%s\n", os.Getenv("GOTRACEBACK"))
48-
if n == "simple" {
49-
// Since the map lookup creates another call stack entry, add a one-off
50-
// "simple" panic style to test the very minimal case.
51-
// types["simple"].f is never called.
52-
panic("simple")
53-
}
54-
f.f()
55-
os.Exit(3)
56-
}
57-
// Undocumented command to do a raw dump of the supported commands. This is
58-
// used by unit tests in ../../stack.
59-
if n == "dump_commands" {
45+
switch n := os.Args[1]; n {
46+
case "-h", "-help", "--help", "help":
47+
usage()
48+
os.Exit(0)
49+
50+
case "dump_commands":
51+
// Undocumented command to do a raw dump of the supported commands. This
52+
// is used by unit tests in ../../stack.
6053
items := make([]string, 0, len(types))
6154
for n := range types {
6255
items = append(items, n)
@@ -66,11 +59,25 @@ func main() {
6659
fmt.Printf("%s\n", n)
6760
}
6861
os.Exit(0)
62+
63+
default:
64+
if f, ok := types[n]; ok {
65+
fmt.Printf("GOTRACEBACK=%s\n", os.Getenv("GOTRACEBACK"))
66+
if n == "simple" {
67+
// Since the map lookup creates another call stack entry, add a
68+
// one-off "simple" panic style to test the very minimal case.
69+
// types["simple"].f is never called.
70+
panic("simple")
71+
}
72+
f.f()
73+
os.Exit(3)
74+
}
75+
fmt.Fprintf(stdErr, "unknown panic style %q\n", n)
76+
os.Exit(1)
6977
}
70-
fmt.Fprintf(stdErr, "unknown panic style %q\n", n)
71-
os.Exit(1)
7278
}
7379
usage()
80+
os.Exit(1)
7481
}
7582

7683
// Mocked in test.
@@ -426,7 +433,6 @@ Select the way to panic:
426433
for _, n := range names {
427434
fmt.Fprintf(stdErr, "- %-*s %s\n", m, n, types[n].desc)
428435
}
429-
os.Exit(2)
430436
}
431437

432438
//

internal/main_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,22 @@ func TestProcess(t *testing.T) {
4141
palette: testPalette,
4242
simil: stack.AnyPointer,
4343
path: basePath,
44-
want: "GOTRACEBACK=all\npanic: simple\n\nC1: runningA\n Emain Fmain.go:52 ImainL()A\n",
44+
want: "GOTRACEBACK=all\npanic: simple\n\nC1: runningA\n Emain Fmain.go:70 ImainL()A\n",
4545
},
4646
{
4747
name: "FullPath",
4848
palette: testPalette,
4949
simil: stack.AnyValue,
5050
path: fullPath,
5151
// "/" is used even on Windows.
52-
want: fmt.Sprintf("GOTRACEBACK=all\npanic: simple\n\nC1: runningA\n Emain F%s:52 ImainL()A\n", strings.Replace(filepath.Join(filepath.Dir(d), "cmd", "panic", "main.go"), "\\", "/", -1)),
52+
want: fmt.Sprintf("GOTRACEBACK=all\npanic: simple\n\nC1: runningA\n Emain F%s:70 ImainL()A\n", strings.Replace(filepath.Join(filepath.Dir(d), "cmd", "panic", "main.go"), "\\", "/", -1)),
5353
},
5454
{
5555
name: "NoColor",
5656
palette: &Palette{},
5757
simil: stack.AnyValue,
5858
path: basePath,
59-
want: "GOTRACEBACK=all\npanic: simple\n\n1: running\n main main.go:52 main()\n",
59+
want: "GOTRACEBACK=all\npanic: simple\n\n1: running\n main main.go:70 main()\n",
6060
},
6161
{
6262
name: "Match",
@@ -72,7 +72,7 @@ func TestProcess(t *testing.T) {
7272
simil: stack.AnyValue,
7373
path: basePath,
7474
filter: regexp.MustCompile(`notpresent`),
75-
want: "GOTRACEBACK=all\npanic: simple\n\nC1: runningA\n Emain Fmain.go:52 ImainL()A\n",
75+
want: "GOTRACEBACK=all\npanic: simple\n\nC1: runningA\n Emain Fmain.go:70 ImainL()A\n",
7676
},
7777
}
7878
for i, line := range data {
@@ -106,14 +106,14 @@ func TestProcessTwoSnapshots(t *testing.T) {
106106
"GOTRACEBACK=all\n" +
107107
"panic: simple\n\n" +
108108
"1: running\n" +
109-
" main main.go:52 main()\n" +
109+
" main main.go:70 main()\n" +
110110
"Ye\n" +
111111
"GOTRACEBACK=all\n" +
112112
"panic: 42\n\n" +
113113
"1: running\n" +
114-
" main main.go:82 panicint(0x2a)\n" +
115-
" main main.go:280 glob..func7()\n" +
116-
" main main.go:54 main()\n" +
114+
" main main.go:89 panicint(0x2a)\n" +
115+
" main main.go:287 glob..func7()\n" +
116+
" main main.go:72 main()\n" +
117117
"Yo\n")
118118
compareString(t, want, out.String())
119119
}

stack/context_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,7 +1178,7 @@ func TestScanSnapshotSyntheticTwoSnapshots(t *testing.T) {
11781178
"main.main",
11791179
Args{},
11801180
pathJoin(ppDir, "main.go"),
1181-
52,
1181+
70,
11821182
),
11831183
},
11841184
},
@@ -1207,19 +1207,19 @@ func TestScanSnapshotSyntheticTwoSnapshots(t *testing.T) {
12071207
"main.panicint",
12081208
Args{Values: []Arg{{Value: 42}}},
12091209
pathJoin(ppDir, "main.go"),
1210-
82,
1210+
89,
12111211
),
12121212
newCallLocal(
12131213
"main.glob..func7",
12141214
Args{},
12151215
pathJoin(ppDir, "main.go"),
1216-
280,
1216+
287,
12171217
),
12181218
newCallLocal(
12191219
"main.main",
12201220
Args{},
12211221
pathJoin(ppDir, "main.go"),
1222-
54,
1222+
72,
12231223
),
12241224
},
12251225
},
@@ -1689,7 +1689,7 @@ func testPanicRace(t *testing.T, s *Snapshot, b *bytes.Buffer, ppDir string) {
16891689
"main.main",
16901690
Args{},
16911691
pathJoin(ppDir, "main.go"),
1692-
54,
1692+
72,
16931693
),
16941694
},
16951695
},
@@ -1725,7 +1725,7 @@ func testPanicRace(t *testing.T, s *Snapshot, b *bytes.Buffer, ppDir string) {
17251725
"main.main",
17261726
Args{},
17271727
pathJoin(ppDir, "main.go"),
1728-
54,
1728+
72,
17291729
),
17301730
},
17311731
},

0 commit comments

Comments
 (0)