File tree Expand file tree Collapse file tree 2 files changed +19
-18
lines changed
Expand file tree Collapse file tree 2 files changed +19
-18
lines changed Original file line number Diff line number Diff line change @@ -2,12 +2,14 @@ package cmd
22
33import (
44 "bytes"
5- "github.com/stretchr/testify/assert "
5+ "context "
66 "io/ioutil"
77 "os"
88 "strings"
99 "testing"
1010 "time"
11+
12+ "github.com/stretchr/testify/assert"
1113)
1214
1315func TestCommand_ExecuteStderr (t * testing.T ) {
@@ -138,3 +140,19 @@ func TestWithEnvironmentVariables(t *testing.T) {
138140
139141 assertEqualWithLineBreak (t , "value" , c .Stdout ())
140142}
143+
144+ func TestCommand_WithContext (t * testing.T ) {
145+ // ensure legacy timeout is honored
146+ cmd := NewCommand ("sleep 3;" , WithTimeout (1 * time .Second ))
147+ err := cmd .Execute ()
148+ assert .NotNil (t , err )
149+ assert .Equal (t , "Command timed out after 1s" , err .Error ())
150+
151+ // set context timeout to 2 seconds to ensure
152+ // context takes precedence over timeout
153+ ctx , cancel := context .WithTimeout (context .Background (), 2 * time .Second )
154+ defer cancel ()
155+ err = cmd .ExecuteContext (ctx )
156+ assert .NotNil (t , err )
157+ assert .Equal (t , "context deadline exceeded" , err .Error ())
158+ }
Original file line number Diff line number Diff line change @@ -2,7 +2,6 @@ package cmd
22
33import (
44 "bytes"
5- "context"
65 "fmt"
76 "os"
87 "runtime"
@@ -131,19 +130,3 @@ func TestCommand_SetOptions(t *testing.T) {
131130 assert .Equal (t , time .Duration (1000000000 ), c .Timeout )
132131 assertEqualWithLineBreak (t , "test" , writer .String ())
133132}
134-
135- func TestCommand_WithContext (t * testing.T ) {
136- // ensure legacy timeout is honored
137- cmd := NewCommand ("sleep 3;" , WithTimeout (1 * time .Second ))
138- err := cmd .Execute ()
139- assert .NotNil (t , err )
140- assert .Equal (t , "Command timed out after 1s" , err .Error ())
141-
142- // set context timeout to 2 seconds to ensure
143- // context takes precedence over timeout
144- ctx , cancel := context .WithTimeout (context .Background (), 2 * time .Second )
145- defer cancel ()
146- err = cmd .ExecuteContext (ctx )
147- assert .NotNil (t , err )
148- assert .Equal (t , "context deadline exceeded" , err .Error ())
149- }
You can’t perform that action at this time.
0 commit comments