-
Notifications
You must be signed in to change notification settings - Fork 0
/
command_test.go
36 lines (32 loc) · 936 Bytes
/
command_test.go
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
// Copyright (C) 2020 Michael J. Fromberger. All Rights Reserved.
package command_test
import (
"errors"
"strings"
"testing"
"github.com/creachadair/command"
)
func TestRun_panic(t *testing.T) {
const message = "omg the sky is falling"
cmd := &command.C{
Name: "freak-out",
Run: func(*command.Env) error {
panic(message)
},
}
err := command.Run(cmd.NewEnv(nil), []string{"freak-out"})
var got command.PanicError
if !errors.As(err, &got) {
t.Fatalf("Run: got error %[1]% %[1]v, want PanicError", err)
}
if !strings.Contains(err.Error(), message) {
t.Error("Panic error does not contain the probe string")
}
if got := got.Value(); got != message {
t.Errorf("Panic value: got %v, want %v", got, message)
}
if env := got.Env(); env.Command != cmd {
t.Errorf("Panic env: got %+v, want %+v", env.Command, cmd)
}
t.Log("--- Captured panic stack (not a panic in the test, don't worry):\n", got.Stack())
}