From 18d9ce733e19c500ca04b4e744e6cdc5c6faf137 Mon Sep 17 00:00:00 2001 From: Ophestra Umiker Date: Sun, 6 Oct 2024 16:00:59 +0900 Subject: [PATCH] helper: test non-existent helpers Signed-off-by: Ophestra Umiker --- helper/helper.go | 21 ++++++++++++++------- helper/helper_test.go | 11 +++++++++++ helper/stub.go | 5 +++++ 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/helper/helper.go b/helper/helper.go index 1470092..af6887c 100644 --- a/helper/helper.go +++ b/helper/helper.go @@ -96,13 +96,15 @@ func (h *Helper) StartNotify(ready chan error) error { // write arguments and close args pipe if _, err := h.args.WriteTo(argsP); err != nil { if err1 := h.Cmd.Process.Kill(); err1 != nil { - panic(err1) + // should be unreachable + panic(err1.Error()) } return err } else { if err = argsP.Close(); err != nil { if err1 := h.Cmd.Process.Kill(); err1 != nil { - panic(err1) + // should be unreachable + panic(err1.Error()) } return err } @@ -117,7 +119,8 @@ func (h *Helper) StartNotify(ready chan error) error { switch n { case -1: if err1 := h.Cmd.Process.Kill(); err1 != nil { - panic(err1) + // should be unreachable + panic(err1.Error()) } // ensure error is not nil if err == nil { @@ -155,18 +158,22 @@ func (h *Helper) Wait() error { // ensure pipe close defer func() { 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) { - panic(err) + // unreachable + panic(err.Error()) } if h.ready != nil { 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) { - panic(err) + // unreachable + panic(err.Error()) } } }() diff --git a/helper/helper_test.go b/helper/helper_test.go index 2caada4..e83b53b 100644 --- a/helper/helper_test.go +++ b/helper/helper_test.go @@ -1,7 +1,9 @@ package helper_test import ( + "errors" "io" + "os" "strings" "sync" "testing" @@ -38,6 +40,15 @@ func TestHelper_StartNotify_Close_Wait(t *testing.T) { helper.InternalReplaceExecCommand(t) 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) { h := helper.New(argsWt, "crash-test-dummy", "--args=3", "--fd=4") ready := make(chan error, 1) diff --git a/helper/stub.go b/helper/stub.go index 32dc11e..6a59173 100644 --- a/helper/stub.go +++ b/helper/stub.go @@ -85,6 +85,11 @@ func InternalReplaceExecCommand(t *testing.T) { // replace execCommand to have the resulting *exec.Cmd launch TestHelperChildStub 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...)...) } }