tty_unix_test.go 933 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. //go:build !windows && !plan9
  2. package cli_test
  3. import (
  4. "os"
  5. "testing"
  6. "golang.org/x/sys/unix"
  7. . "src.elv.sh/pkg/cli"
  8. )
  9. func TestTTYSignal(t *testing.T) {
  10. tty := NewTTY(os.Stdin, os.Stderr)
  11. sigch := tty.NotifySignals()
  12. err := unix.Kill(unix.Getpid(), unix.SIGUSR1)
  13. if err != nil {
  14. t.Skip("cannot send SIGUSR1 to myself:", err)
  15. }
  16. if sig := nextSig(sigch); sig != unix.SIGUSR1 {
  17. t.Errorf("Got signal %v, want SIGUSR1", sig)
  18. }
  19. tty.StopSignals()
  20. err = unix.Kill(unix.Getpid(), unix.SIGUSR2)
  21. if err != nil {
  22. t.Skip("cannot send SIGUSR2 to myself:", err)
  23. }
  24. if sig := nextSig(sigch); sig != nil {
  25. t.Errorf("Got signal %v, want nil", sig)
  26. }
  27. }
  28. // Gets the next signal from the channel, ignoring all SIGURG generated by the
  29. // Go runtime. See https://github.com/golang/go/issues/37942.
  30. func nextSig(sigch <-chan os.Signal) os.Signal {
  31. for {
  32. sig := <-sigch
  33. if sig != unix.SIGURG {
  34. return sig
  35. }
  36. }
  37. }