helper: test non-existent helpers

Signed-off-by: Ophestra Umiker <cat@ophivana.moe>
This commit is contained in:
Ophestra Umiker 2024-10-06 16:00:59 +09:00
parent ba76e2919b
commit 18d9ce733e
Signed by: cat
SSH Key Fingerprint: SHA256:gQ67O0enBZ7UdZypgtspB2FDM1g3GVw8nX0XSdcFw8Q
3 changed files with 30 additions and 7 deletions

View File

@ -96,13 +96,15 @@ func (h *Helper) StartNotify(ready chan error) error {
// write arguments and close args pipe // write arguments and close args pipe
if _, err := h.args.WriteTo(argsP); err != nil { if _, err := h.args.WriteTo(argsP); err != nil {
if err1 := h.Cmd.Process.Kill(); err1 != nil { if err1 := h.Cmd.Process.Kill(); err1 != nil {
panic(err1) // should be unreachable
panic(err1.Error())
} }
return err return err
} else { } else {
if err = argsP.Close(); err != nil { if err = argsP.Close(); err != nil {
if err1 := h.Cmd.Process.Kill(); err1 != nil { if err1 := h.Cmd.Process.Kill(); err1 != nil {
panic(err1) // should be unreachable
panic(err1.Error())
} }
return err return err
} }
@ -117,7 +119,8 @@ func (h *Helper) StartNotify(ready chan error) error {
switch n { switch n {
case -1: case -1:
if err1 := h.Cmd.Process.Kill(); err1 != nil { if err1 := h.Cmd.Process.Kill(); err1 != nil {
panic(err1) // should be unreachable
panic(err1.Error())
} }
// ensure error is not nil // ensure error is not nil
if err == nil { if err == nil {
@ -155,18 +158,22 @@ func (h *Helper) Wait() error {
// ensure pipe close // ensure pipe close
defer func() { defer func() {
if err := h.argsP[0].Close(); err != nil && !errors.Is(err, os.ErrClosed) { if err := h.argsP[0].Close(); err != nil && !errors.Is(err, os.ErrClosed) {
panic(err) // unreachable
panic(err.Error())
} }
if err := h.argsP[1].Close(); err != nil && !errors.Is(err, os.ErrClosed) { if err := h.argsP[1].Close(); err != nil && !errors.Is(err, os.ErrClosed) {
panic(err) // unreachable
panic(err.Error())
} }
if h.ready != nil { if h.ready != nil {
if err := h.statP[0].Close(); err != nil && !errors.Is(err, os.ErrClosed) { if err := h.statP[0].Close(); err != nil && !errors.Is(err, os.ErrClosed) {
panic(err) // unreachable
panic(err.Error())
} }
if err := h.statP[1].Close(); err != nil && !errors.Is(err, os.ErrClosed) { if err := h.statP[1].Close(); err != nil && !errors.Is(err, os.ErrClosed) {
panic(err) // unreachable
panic(err.Error())
} }
} }
}() }()

View File

@ -1,7 +1,9 @@
package helper_test package helper_test
import ( import (
"errors"
"io" "io"
"os"
"strings" "strings"
"sync" "sync"
"testing" "testing"
@ -38,6 +40,15 @@ func TestHelper_StartNotify_Close_Wait(t *testing.T) {
helper.InternalReplaceExecCommand(t) helper.InternalReplaceExecCommand(t)
argsOnce.Do(prepareArgs) argsOnce.Do(prepareArgs)
t.Run("start non-existent helper path", func(t *testing.T) {
h := helper.New(argsWt, "/nonexistent")
if err := h.Start(); !errors.Is(err, os.ErrNotExist) {
t.Errorf("Start() error = %v, wantErr %v",
err, os.ErrNotExist)
}
})
t.Run("start helper with status channel", func(t *testing.T) { t.Run("start helper with status channel", func(t *testing.T) {
h := helper.New(argsWt, "crash-test-dummy", "--args=3", "--fd=4") h := helper.New(argsWt, "crash-test-dummy", "--args=3", "--fd=4")
ready := make(chan error, 1) ready := make(chan error, 1)

View File

@ -85,6 +85,11 @@ func InternalReplaceExecCommand(t *testing.T) {
// replace execCommand to have the resulting *exec.Cmd launch TestHelperChildStub // replace execCommand to have the resulting *exec.Cmd launch TestHelperChildStub
execCommand = func(name string, arg ...string) *exec.Cmd { execCommand = func(name string, arg ...string) *exec.Cmd {
// pass through nonexistent path
if name == "/nonexistent" && len(arg) == 0 {
return exec.Command(name)
}
return exec.Command(os.Args[0], append([]string{"-test.run=TestHelperChildStub", "--", name}, arg...)...) return exec.Command(os.Args[0], append([]string{"-test.run=TestHelperChildStub", "--", name}, arg...)...)
} }
} }