From 199484c748a0a41b5c0a3d126fa68849edb5cf40 Mon Sep 17 00:00:00 2001 From: Mura Li Date: Sat, 8 Apr 2017 15:11:19 +0800 Subject: [PATCH 1/2] Rectify error handling --- command.go | 6 +++++- command_test.go | 6 ++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/command.go b/command.go index c3534a145..095619f48 100644 --- a/command.go +++ b/command.go @@ -70,7 +70,11 @@ func (c *Command) RunInDirTimeoutPipeline(timeout time.Duration, dir string, std return err } - return cmd.Wait() + if err := cmd.Wait(); err == nil { + return nil + } + + return ctx.Err() } // RunInDirTimeout executes the command in given directory with given timeout, diff --git a/command_test.go b/command_test.go index a01aa042d..2f35b3c32 100644 --- a/command_test.go +++ b/command_test.go @@ -7,6 +7,7 @@ package git import ( + "context" "testing" "time" ) @@ -32,10 +33,7 @@ func TestRunInDirTimeoutPipelineAlwaysTimeout(t *testing.T) { cmd := NewCommand("hash-object --stdin") for i := 0; i < maxLoops; i++ { if err := cmd.RunInDirTimeoutPipeline(1*time.Microsecond, "", nil, nil); err != nil { - // 'context deadline exceeded' when the error is returned by exec.Start - // 'signal: killed' when the error is returned by exec.Wait - // It depends on the point of the time (before or after exec.Start returns) at which the timeout is triggered. - if err.Error() != "context deadline exceeded" && err.Error() != "signal: killed" { + if err != context.DeadlineExceeded { t.Fatalf("Testing %d/%d: %v", i, maxLoops, err) } } From 1a38a291aac3081f64cb93b61ad5fc534f369f92 Mon Sep 17 00:00:00 2001 From: Mura Li Date: Sat, 8 Apr 2017 17:26:19 +0800 Subject: [PATCH 2/2] Log the error returned by exec.Wait --- command.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/command.go b/command.go index 095619f48..145480e58 100644 --- a/command.go +++ b/command.go @@ -70,8 +70,8 @@ func (c *Command) RunInDirTimeoutPipeline(timeout time.Duration, dir string, std return err } - if err := cmd.Wait(); err == nil { - return nil + if err := cmd.Wait(); err != nil { + log("exec.Wait: %v", err) } return ctx.Err()