pkg/shell: Test signal handling.

This commit is contained in:
Qi Xiao 2022-06-23 21:21:49 +01:00
parent fbf0bfc13b
commit 4fb96d515c
3 changed files with 41 additions and 1 deletions

View File

@ -53,6 +53,16 @@ func ReadAllAndClose(r io.ReadCloser) []byte {
return v
}
// ReadFile wraps os.ReadFile.
func ReadFile(fname string) []byte {
return OK1(os.ReadFile(fname))
}
// ReadFileString converts the result of ReadFile to a string.
func ReadFileString(fname string) string {
return string(ReadFile(fname))
}
// MkdirAll calls os.MkdirAll for each argument.
func MkdirAll(names ...string) {
for _, name := range names {

View File

@ -70,7 +70,9 @@ func Run(fds [3]*os.File, args []string, p Program) int {
if log != "" {
err = logutil.SetOutputFile(log)
if err != nil {
if err == nil {
defer logutil.SetOutput(io.Discard)
} else {
fmt.Fprintln(fds[2], err)
}
}

View File

@ -0,0 +1,28 @@
package shell
import (
"strings"
"syscall"
"testing"
"src.elv.sh/pkg/must"
. "src.elv.sh/pkg/prog/progtest"
"src.elv.sh/pkg/testutil"
)
func TestSignal_USR1(t *testing.T) {
Test(t, &Program{},
ThatElvish("-c", "kill -USR1 $pid").WritesStderrContaining("src.elv.sh/pkg/shell"))
}
func TestSignal_Ignored(t *testing.T) {
testutil.InTempDir(t)
Test(t, &Program{},
ThatElvish("-log", "logCHLD", "-c", "kill -CHLD $pid").DoesNothing())
wantLogCHLD := "signal " + syscall.SIGCHLD.String()
if logCHLD := must.ReadFileString("logCHLD"); !strings.Contains(logCHLD, wantLogCHLD) {
t.Errorf("want log when getting SIGCHLD to contain %q; got:\n%s", wantLogCHLD, logCHLD)
}
}