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

     1  //go:build unix
     2  
     3  package shell
     4  
     5  import (
     6  	"fmt"
     7  	"strings"
     8  	"syscall"
     9  	"testing"
    10  	"time"
    11  
    12  	"src.elv.sh/pkg/must"
    13  	. "src.elv.sh/pkg/prog/progtest"
    14  	"src.elv.sh/pkg/testutil"
    15  )
    16  
    17  func TestSignal_USR1(t *testing.T) {
    18  	Test(t, &Program{},
    19  		ThatElvish("-c", killCmd("USR1")).WritesStderrContaining("src.elv.sh/pkg/shell"))
    20  }
    21  
    22  func TestSignal_Ignored(t *testing.T) {
    23  	testutil.InTempDir(t)
    24  
    25  	Test(t, &Program{},
    26  		ThatElvish("-log", "logCHLD", "-c", killCmd("CHLD")).DoesNothing())
    27  
    28  	wantLogCHLD := "signal " + syscall.SIGCHLD.String()
    29  	if logCHLD := must.ReadFileString("logCHLD"); !strings.Contains(logCHLD, wantLogCHLD) {
    30  		t.Errorf("want log when getting SIGCHLD to contain %q; got:\n%s", wantLogCHLD, logCHLD)
    31  	}
    32  }
    33  
    34  func killCmd(name string) string {
    35  	// Add a delay after kill to ensure that the signal is handled.
    36  	return fmt.Sprintf("kill -%v $pid; sleep %v", name, testutil.Scaled(10*time.Millisecond))
    37  }