2021-08-23 07:19:49 +08:00
|
|
|
//go:build !windows && !plan9
|
2018-07-10 06:36:38 +08:00
|
|
|
// +build !windows,!plan9
|
|
|
|
|
2019-12-02 20:42:08 +08:00
|
|
|
package cli_test
|
2018-07-10 06:36:38 +08:00
|
|
|
|
|
|
|
import (
|
2020-04-04 06:28:50 +08:00
|
|
|
"os"
|
2018-07-10 06:36:38 +08:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
2021-01-27 09:28:38 +08:00
|
|
|
. "src.elv.sh/pkg/cli"
|
2018-07-10 06:36:38 +08:00
|
|
|
)
|
|
|
|
|
2019-08-26 06:39:57 +08:00
|
|
|
func TestTTYSignal(t *testing.T) {
|
2021-02-12 02:49:00 +08:00
|
|
|
tty := NewTTY(os.Stdin, os.Stderr)
|
2019-08-26 06:39:57 +08:00
|
|
|
sigch := tty.NotifySignals()
|
2018-07-10 06:36:38 +08:00
|
|
|
|
|
|
|
err := unix.Kill(unix.Getpid(), unix.SIGUSR1)
|
|
|
|
if err != nil {
|
|
|
|
t.Skip("cannot send SIGUSR1 to myself:", err)
|
|
|
|
}
|
|
|
|
|
2020-04-04 06:28:50 +08:00
|
|
|
if sig := nextSig(sigch); sig != unix.SIGUSR1 {
|
2018-07-10 07:20:42 +08:00
|
|
|
t.Errorf("Got signal %v, want SIGUSR1", sig)
|
|
|
|
}
|
|
|
|
|
2019-08-26 06:39:57 +08:00
|
|
|
tty.StopSignals()
|
2018-07-10 06:36:38 +08:00
|
|
|
|
2018-07-10 06:51:40 +08:00
|
|
|
err = unix.Kill(unix.Getpid(), unix.SIGUSR2)
|
2018-07-10 06:36:38 +08:00
|
|
|
if err != nil {
|
2018-07-10 06:51:40 +08:00
|
|
|
t.Skip("cannot send SIGUSR2 to myself:", err)
|
2018-07-10 06:36:38 +08:00
|
|
|
}
|
|
|
|
|
2020-04-04 06:28:50 +08:00
|
|
|
if sig := nextSig(sigch); sig != nil {
|
2018-07-10 07:20:42 +08:00
|
|
|
t.Errorf("Got signal %v, want nil", sig)
|
2018-07-10 06:36:38 +08:00
|
|
|
}
|
|
|
|
}
|
2020-04-04 06:28:50 +08:00
|
|
|
|
|
|
|
// Gets the next signal from the channel, ignoring all SIGURG generated by the
|
|
|
|
// Go runtime. See https://github.com/golang/go/issues/37942.
|
|
|
|
func nextSig(sigch <-chan os.Signal) os.Signal {
|
|
|
|
for {
|
|
|
|
sig := <-sigch
|
|
|
|
if sig != unix.SIGURG {
|
|
|
|
return sig
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|