@@ -32,49 +32,60 @@ func TestMonitoredCmd(t *testing.T) {
32
32
}
33
33
defer os .Remove ("./echosleep" )
34
34
35
- cmd := mkTestCmd (2 )
36
- err = cmd .run (context .Background ())
37
- if err != nil {
38
- t .Errorf ("Expected command not to fail: %s" , err )
35
+ tests := []struct {
36
+ name string
37
+ iterations int
38
+ output string
39
+ err bool
40
+ timeout bool
41
+ }{
42
+ {"success" , 2 , "foo\n foo\n " , false , false },
43
+ {"timeout" , 5 , "foo\n foo\n foo\n foo\n " , true , true },
39
44
}
40
45
41
- expectedOutput := "foo\n foo\n "
42
- if cmd .stdout .buf .String () != expectedOutput {
43
- t .Errorf ("Unexpected output:\n \t (GOT): %s\n \t (WNT): %s" , cmd .stdout .buf .String (), expectedOutput )
44
- }
46
+ for _ , want := range tests {
47
+ t .Run (want .name , func (t * testing.T ) {
48
+ cmd := mkTestCmd (want .iterations )
45
49
46
- cmd2 := mkTestCmd (10 )
47
- err = cmd2 .run (context .Background ())
48
- if err == nil {
49
- t .Error ("Expected command to fail" )
50
- }
50
+ err := cmd .run (context .Background ())
51
+ if ! want .err && err != nil {
52
+ t .Errorf ("Eexpected command not to fail, got error: %s" , err )
53
+ } else if want .err && err == nil {
54
+ t .Error ("expected command to fail" )
55
+ }
51
56
52
- _ , ok := err .( * noProgressError )
53
- if ! ok {
54
- t .Errorf ("Expected a timeout error, but got: %s " , err )
55
- }
57
+ got := cmd . stdout . String ( )
58
+ if want . output != got {
59
+ t .Errorf ("unexpected output: \n \t (GOT): \n %s \n \t (WNT): \n %s " , got , want . output )
60
+ }
56
61
57
- expectedOutput = "foo\n foo\n foo\n foo\n "
58
- if cmd2 .stdout .buf .String () != expectedOutput {
59
- t .Errorf ("Unexpected output:\n \t (GOT): %s\n \t (WNT): %s" , cmd2 .stdout .buf .String (), expectedOutput )
62
+ if want .timeout {
63
+ _ , ok := err .(* noProgressError )
64
+ if ! ok {
65
+ t .Errorf ("Expected a timeout error, but got: %s" , err )
66
+ }
67
+ }
68
+ })
60
69
}
61
70
62
- ctx , cancel := context .WithCancel (context .Background ())
63
- sync1 , errchan := make (chan struct {}), make (chan error )
64
- cmd3 := mkTestCmd (2 )
65
- go func () {
66
- close (sync1 )
67
- errchan <- cmd3 .run (ctx )
68
- }()
71
+ t .Run ("cancel" , func (t * testing.T ) {
72
+ ctx , cancel := context .WithCancel (context .Background ())
73
+ sync , errchan := make (chan struct {}), make (chan error )
74
+ cmd := mkTestCmd (2 )
75
+ go func () {
76
+ close (sync )
77
+ errchan <- cmd .run (ctx )
78
+ }()
69
79
70
- // Make sure goroutine is at least started before we cancel the context.
71
- <- sync1
72
- // Give it a bit to get the process started.
73
- <- time .After (5 * time .Millisecond )
74
- cancel ()
80
+ // Make sure goroutine is at least started before we cancel the context.
81
+ <- sync
82
+ // Give it a bit to get the process started.
83
+ <- time .After (5 * time .Millisecond )
84
+ cancel ()
75
85
76
- err = <- errchan
77
- if err != context .Canceled {
78
- t .Errorf ("should have gotten canceled error, got %s" , err )
79
- }
86
+ err := <- errchan
87
+ if err != context .Canceled {
88
+ t .Errorf ("expected a canceled error, got %s" , err )
89
+ }
90
+ })
80
91
}
0 commit comments