From 9df80c20d5daaa955372295e3bd8d1a0fb09f58d Mon Sep 17 00:00:00 2001 From: "Serge A. Zaitsev" Date: Fri, 5 Dec 2014 20:21:42 +0200 Subject: [PATCH] added tests for eval command runner --- zs.go | 7 +++++-- zs_test.go | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/zs.go b/zs.go index 697d778..1db764c 100644 --- a/zs.go +++ b/zs.go @@ -110,10 +110,13 @@ func eval(cmd []string, vars map[string]string) (string, error) { outbuf := bytes.NewBuffer(nil) err := run(path.Join(ZSDIR, cmd[0]), cmd[1:], vars, outbuf) if err != nil { - log.Println(err) + if _, ok := err.(*exec.ExitError); ok { + return "", err + } outbuf = bytes.NewBuffer(nil) err := run(cmd[0], cmd[1:], vars, outbuf) - if err != nil { + // Return exit errors, but ignore if the command was not found + if _, ok := err.(*exec.ExitError); ok { return "", err } } diff --git a/zs_test.go b/zs_test.go index b4ee3b1..5aeb1c8 100644 --- a/zs_test.go +++ b/zs_test.go @@ -5,6 +5,7 @@ import ( "fmt" "log" "os" + "os/exec" "strings" "testing" ) @@ -123,6 +124,24 @@ func TestRun(t *testing.T) { } } +func TestEvalCommand(t *testing.T) { + s, err := eval([]string{"echo", "hello"}, map[string]string{}) + if err != nil { + t.Error(err) + } + if s != "hello\n" { + t.Error(s) + } + _, err = eval([]string{"cat", "bogus/file"}, map[string]string{}) + if _, ok := err.(*exec.ExitError); !ok { + t.Error("expected ExitError") + } + _, err = eval([]string{"missing command"}, map[string]string{}) + if err != nil { + t.Error("missing command should be ignored") + } +} + func TestHelperProcess(*testing.T) { if os.Getenv("ZS_HELPER") != "1" { return -- 2.43.0