File: tty_unix_test.go

package info (click to toggle)
elvish 0.21.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,372 kB
  • sloc: javascript: 236; sh: 130; python: 104; makefile: 88; xml: 9
file content (47 lines) | stat: -rw-r--r-- 919 bytes parent folder | download
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
37
38
39
40
41
42
43
44
45
46
47
//go:build unix

package cli_test

import (
	"os"
	"testing"

	"golang.org/x/sys/unix"
	. "src.elv.sh/pkg/cli"
)

func TestTTYSignal(t *testing.T) {
	tty := NewTTY(os.Stdin, os.Stderr)
	sigch := tty.NotifySignals()

	err := unix.Kill(unix.Getpid(), unix.SIGUSR1)
	if err != nil {
		t.Skip("cannot send SIGUSR1 to myself:", err)
	}

	if sig := nextSig(sigch); sig != unix.SIGUSR1 {
		t.Errorf("Got signal %v, want SIGUSR1", sig)
	}

	tty.StopSignals()

	err = unix.Kill(unix.Getpid(), unix.SIGUSR2)
	if err != nil {
		t.Skip("cannot send SIGUSR2 to myself:", err)
	}

	if sig := nextSig(sigch); sig != nil {
		t.Errorf("Got signal %v, want nil", sig)
	}
}

// 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
		}
	}
}