src.elv.sh@v0.21.0-dev.0.20240515223629-06979efb9a2a/pkg/cli/tty_unix_test.go (about)

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