Skip to content

Commit a939452

Browse files
committed
return ctx.Err() instead of nil when <-ctx.Done() returns
1 parent 3fba9dd commit a939452

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

wgo_cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ func (wgoCmd *WgoCmd) Run() error {
536536
case <-wgoCmd.ctx.Done():
537537
stop(cmd)
538538
<-waitDone
539-
return nil
539+
return wgoCmd.ctx.Err()
540540
case err := <-cmdResult:
541541
if i == len(wgoCmd.ArgsList)-1 {
542542
if wgoCmd.Exit {

wgo_cmd_test.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -588,12 +588,11 @@ func TestWgoCmd_Run(t *testing.T) {
588588
wgoCmd.Stdout = buf
589589
err = wgoCmd.Run()
590590
if err != nil {
591-
t.Fatal(err)
592-
}
593-
got := strings.TrimSpace(buf.String())
594-
want := ""
595-
if got != want {
596-
t.Errorf("\ngot: %q\nwant: %q", got, want)
591+
if !errors.Is(err, context.DeadlineExceeded) {
592+
t.Fatal(err)
593+
}
594+
} else {
595+
t.Errorf("\ngot: nil error\nwant: context.DeadlineExceeded")
597596
}
598597
})
599598

@@ -611,7 +610,9 @@ func TestWgoCmd_Run(t *testing.T) {
611610
wgoCmd.Stdout = buf
612611
err = wgoCmd.Run()
613612
if err != nil {
614-
t.Fatal(err)
613+
if !errors.Is(err, context.DeadlineExceeded) {
614+
t.Fatal(err)
615+
}
615616
}
616617
got := strings.TrimSpace(buf.String())
617618
want := "Waiting..."
@@ -660,7 +661,9 @@ func TestWgoCmd_Run(t *testing.T) {
660661
wgoCmd.Stdout = buf
661662
err = wgoCmd.Run()
662663
if err != nil {
663-
t.Fatal(err)
664+
if !errors.Is(err, context.DeadlineExceeded) {
665+
t.Fatal(err)
666+
}
664667
}
665668
got := strings.TrimSpace(buf.String())
666669
want := "hello"
@@ -749,7 +752,7 @@ func TestWgoCmd_FileEvent(t *testing.T) {
749752

750753
cancel()
751754
err = <-cmdResult
752-
if err != nil {
755+
if err != nil && !errors.Is(err, context.Canceled) {
753756
t.Fatal(err)
754757
}
755758
got := strings.TrimSpace(buf.String())
@@ -831,7 +834,7 @@ func TestWgoCmd_Polling(t *testing.T) {
831834

832835
cancel()
833836
err = <-cmdResult
834-
if err != nil {
837+
if err != nil && !errors.Is(err, context.Canceled) {
835838
t.Fatal(err)
836839
}
837840
got := strings.TrimSpace(buf.String())

0 commit comments

Comments
 (0)