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

     1  //go:build unix
     2  
     3  package eval_test
     4  
     5  import (
     6  	"fmt"
     7  	"runtime"
     8  	"syscall"
     9  	"testing"
    10  
    11  	. "src.elv.sh/pkg/eval"
    12  
    13  	"src.elv.sh/pkg/tt"
    14  )
    15  
    16  func TestExternalCmdExit_Error(t *testing.T) {
    17  	tt.Test(t, error.Error,
    18  		Args(ExternalCmdExit{0x0, "ls", 1}).Rets("ls exited with 0"),
    19  		Args(ExternalCmdExit{0x100, "ls", 1}).Rets("ls exited with 1"),
    20  		// Note: all Unix'es have SIGINT = 2, but syscall package has different
    21  		// string in gccgo("Interrupt") and gc("interrupt").
    22  		Args(ExternalCmdExit{0x2, "ls", 1}).Rets("ls killed by signal "+syscall.SIGINT.String()),
    23  		// 0x80 + signal for core dumped
    24  		Args(ExternalCmdExit{0x82, "ls", 1}).Rets("ls killed by signal "+syscall.SIGINT.String()+" (core dumped)"),
    25  		// 0x7f + signal<<8 for stopped
    26  		Args(ExternalCmdExit{0x27f, "ls", 1}).Rets("ls stopped by signal "+syscall.SIGINT.String()+" (pid=1)"),
    27  	)
    28  	if runtime.GOOS == "linux" {
    29  		tt.Test(t, error.Error,
    30  			// 0x057f + cause<<16 for trapped. SIGTRAP is 5 on all Unix'es but have
    31  			// different string representations on different OSes.
    32  			Args(ExternalCmdExit{0x1057f, "ls", 1}).Rets(fmt.Sprintf(
    33  				"ls stopped by signal %s (pid=1) (trapped 1)", syscall.SIGTRAP)),
    34  			// 0xff is the only exit code that is not exited, signaled or stopped.
    35  			Args(ExternalCmdExit{0xff, "ls", 1}).Rets("ls has unknown WaitStatus 255"),
    36  		)
    37  	}
    38  }