-
Notifications
You must be signed in to change notification settings - Fork 2
/
func_test.go
1205 lines (1167 loc) · 24.4 KB
/
func_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package puffin
import (
"bytes"
"context"
"errors"
"io"
"os"
"reflect"
"testing"
"time"
)
func TestExec_LookPath(t *testing.T) {
type fields struct {
funcMap map[string]CmdFunc
}
type args struct {
file string
}
tests := []struct {
name string
fields fields
args args
want string
wantErr bool
}{
{
"missing func",
fields{
funcMap: map[string]CmdFunc{},
},
args{
file: "test",
},
"",
true,
},
{
"found command",
fields{
funcMap: map[string]CmdFunc{
"test": func(fc *FuncCmd) int { return 0 },
},
},
args{
file: "test",
},
"test",
false,
},
{
"found command file",
fields{
funcMap: map[string]CmdFunc{
"/path/to/test": func(fc *FuncCmd) int { return 0 },
},
},
args{
file: "test",
},
"/path/to/test",
false,
},
{
"found file",
fields{
funcMap: map[string]CmdFunc{
"/path/to/test": func(fc *FuncCmd) int { return 0 },
},
},
args{
file: "/path/to/test",
},
"/path/to/test",
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := &FuncExec{
funcMap: tt.fields.funcMap,
}
got, err := e.LookPath(tt.args.file)
if (err != nil) != tt.wantErr {
t.Fatalf("FuncExec.LookPath() error = %v, wantErr %v", err, tt.wantErr)
}
if got != tt.want {
t.Errorf("FuncExec.LookPath() = %v, want %v", got, tt.want)
}
})
}
}
func TestFuncExec_Command(t *testing.T) {
type fields struct {
funcMap map[string]CmdFunc
envs map[string]string
}
type args struct {
name string
arg []string
}
tests := []struct {
name string
fields fields
args args
want *FuncCmd
}{
{
"cmd with args",
fields{
funcMap: map[string]CmdFunc{
"test": func(fc *FuncCmd) int { return 0 },
},
},
args{
name: "test",
arg: []string{"arg1", "arg2"},
},
&FuncCmd{
path: "test",
args: []string{"test", "arg1", "arg2"},
ctxErr: make(chan error, 1),
exitCode: make(chan int, 1),
},
},
{
"different path",
fields{
funcMap: map[string]CmdFunc{
"/path/to/test": func(fc *FuncCmd) int { return 0 },
},
},
args{
name: "test",
arg: []string{"arg1", "arg2"},
},
&FuncCmd{
path: "/path/to/test",
args: []string{"test", "arg1", "arg2"},
ctxErr: make(chan error, 1),
exitCode: make(chan int, 1),
},
},
{
"lookpath failure",
fields{
funcMap: map[string]CmdFunc{},
},
args{
name: "test",
arg: []string{"arg1", "arg2"},
},
&FuncCmd{
path: "test",
args: []string{"test", "arg1", "arg2"},
ctxErr: make(chan error, 1),
exitCode: make(chan int, 1),
err: errors.New(`exec: "test": executable file not found in $PATH`),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := &FuncExec{
funcMap: tt.fields.funcMap,
envs: tt.fields.envs,
}
// set up tt.want with the correct fExec
tt.want.fExec = e
got := e.Command(tt.args.name, tt.args.arg...)
// test err filrst since errors don't play nice with DeepEqual
var gotErr, wantErr string
if got.Err() != nil {
gotErr = got.Err().Error()
got.(*FuncCmd).err = nil
}
if tt.want.Err() != nil {
wantErr = tt.want.Err().Error()
tt.want.err = nil
}
if gotErr != wantErr {
t.Errorf("FuncExec.Command() got err %s, wanted err %s", gotErr, wantErr)
}
// check channels seperately as well
if len(got.(*FuncCmd).exitCode) != len(tt.want.exitCode) ||
cap(got.(*FuncCmd).exitCode) != cap(tt.want.exitCode) {
t.Errorf("FuncExec.CommandContext() exitCode chan did not match what was expected")
}
if len(got.(*FuncCmd).ctxErr) != len(tt.want.ctxErr) ||
cap(got.(*FuncCmd).ctxErr) != cap(tt.want.ctxErr) {
t.Errorf("FuncExec.CommandContext() ctxErr chan did not match what was expected")
}
got.(*FuncCmd).exitCode = nil
got.(*FuncCmd).ctxErr = nil
tt.want.exitCode = nil
tt.want.ctxErr = nil
// now test the rest of the Cmd
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("FuncExec.Command() = %v, want %v", got, tt.want)
}
})
}
}
func TestFuncExec_CommandContext(t *testing.T) {
timeoutCtx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
type fields struct {
funcMap map[string]CmdFunc
envs map[string]string
}
type args struct {
ctx context.Context
name string
arg []string
}
tests := []struct {
name string
fields fields
args args
want *FuncCmd
}{
{
"cmd with args",
fields{
funcMap: map[string]CmdFunc{
"test": func(fc *FuncCmd) int { return 0 },
},
},
args{
ctx: context.Background(),
name: "test",
arg: []string{"arg1", "arg2"},
},
&FuncCmd{
ctx: context.Background(),
path: "test",
args: []string{"test", "arg1", "arg2"},
ctxErr: make(chan error, 1),
exitCode: make(chan int, 1),
},
},
{
"different path",
fields{
funcMap: map[string]CmdFunc{
"/path/to/test": func(fc *FuncCmd) int { return 0 },
},
},
args{
ctx: context.Background(),
name: "test",
arg: []string{"arg1", "arg2"},
},
&FuncCmd{
ctx: context.Background(),
path: "/path/to/test",
args: []string{"test", "arg1", "arg2"},
ctxErr: make(chan error, 1),
exitCode: make(chan int, 1),
},
},
{
"lookpath failure",
fields{
funcMap: map[string]CmdFunc{},
},
args{
ctx: context.Background(),
name: "test",
arg: []string{"arg1", "arg2"},
},
&FuncCmd{
ctx: context.Background(),
path: "test",
args: []string{"test", "arg1", "arg2"},
ctxErr: make(chan error, 1),
exitCode: make(chan int, 1),
err: errors.New(`exec: "test": executable file not found in $PATH`),
},
},
{
"with timeout context",
fields{
funcMap: map[string]CmdFunc{
"test": func(fc *FuncCmd) int { return 0 },
},
},
args{
ctx: timeoutCtx,
name: "test",
arg: []string{"arg1", "arg2"},
},
&FuncCmd{
ctx: timeoutCtx,
path: "test",
args: []string{"test", "arg1", "arg2"},
ctxErr: make(chan error, 1),
exitCode: make(chan int, 1),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := &FuncExec{
funcMap: tt.fields.funcMap,
envs: tt.fields.envs,
}
// set up tt.want with the correct fExec
tt.want.fExec = e
got := e.CommandContext(tt.args.ctx, tt.args.name, tt.args.arg...)
// test err filrst since errors don't play nice with DeepEqual
var gotErr, wantErr string
if got.Err() != nil {
gotErr = got.Err().Error()
got.(*FuncCmd).err = nil
}
if tt.want.Err() != nil {
wantErr = tt.want.Err().Error()
tt.want.err = nil
}
if gotErr != wantErr {
t.Errorf("FuncExec.CommandContext() got err %s, wanted err %s", gotErr, wantErr)
}
// check channels seperately as well
if len(got.(*FuncCmd).exitCode) != len(tt.want.exitCode) ||
cap(got.(*FuncCmd).exitCode) != cap(tt.want.exitCode) {
t.Errorf("FuncExec.CommandContext() exitCode chan did not match what was expected")
}
if len(got.(*FuncCmd).ctxErr) != len(tt.want.ctxErr) ||
cap(got.(*FuncCmd).ctxErr) != cap(tt.want.ctxErr) {
t.Errorf("FuncExec.CommandContext() ctxErr chan did not match what was expected")
}
got.(*FuncCmd).exitCode = nil
got.(*FuncCmd).ctxErr = nil
tt.want.exitCode = nil
tt.want.ctxErr = nil
// now test the rest of the Cmd
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("FuncExec.CommandContext() = \n%#v, want \n%#v", got, tt.want)
}
})
}
}
func TestFuncCmd_Start(t *testing.T) {
type fields struct {
path string
stdout io.Writer
process *os.Process
exitCode chan int
ctx context.Context
err error
fExec *FuncExec
}
tests := []struct {
name string
fields fields
wantErr bool
}{
{
"empty path",
fields{
path: "",
},
true,
},
{
"lookpath err",
fields{
err: errors.New("lookpath error"),
},
true,
},
{
"already run",
fields{
process: &os.Process{},
},
true,
},
{
"context is already done",
fields{
ctx: func() context.Context {
ctx, cancel := context.WithCancel(context.Background())
cancel()
return ctx
}(),
},
true,
},
{
"run cmd",
fields{
path: "test",
exitCode: make(chan int, 1),
fExec: &FuncExec{
funcMap: map[string]CmdFunc{
"test": func(fc *FuncCmd) int {
fc.stdout.Write([]byte("test was run"))
return 0
},
},
},
stdout: &bytes.Buffer{},
},
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &FuncCmd{
path: tt.fields.path,
stdout: tt.fields.stdout,
process: tt.fields.process,
exitCode: tt.fields.exitCode,
err: tt.fields.err,
fExec: tt.fields.fExec,
}
if err := c.Start(); (err != nil) != tt.wantErr {
t.Errorf("FuncCmd.Start() error = %v, wantErr %v", err, tt.wantErr)
}
if tt.wantErr {
return
}
// check that process and pid are set
if c.process == nil {
t.Fatalf("FuncCmd.Start() start was run but process was not set")
}
if c.process.Pid == 0 {
t.Fatalf("FuncCmd.Start() start was run but process id was not set")
}
// sleep for a short time to make sure the cmf func has time to run
time.Sleep(time.Millisecond)
// check that the function has been run (or at least started)
gotStdout := c.stdout.(*bytes.Buffer).String()
wantStdout := "test was run"
if gotStdout != wantStdout {
t.Errorf("FuncCmd.Start() cmd func was not run, got stdout %s but wanted %s", gotStdout, wantStdout)
}
})
}
}
func TestFuncCmd_Wait(t *testing.T) {
type fields struct {
process *os.Process
processState *os.ProcessState
ctx context.Context
ctxErr chan error
startErr error
exitCode chan int
}
tests := []struct {
name string
fields fields
wantErr bool
}{
{
"not started",
fields{
process: nil,
exitCode: func() chan int {
code := make(chan int, 1)
code <- 0
return code
}(),
},
true,
},
{
"start error",
fields{
startErr: errors.New("start error"),
exitCode: func() chan int {
code := make(chan int, 1)
code <- 0
return code
}(),
},
true,
},
{
"wait already called",
fields{
process: &os.Process{},
processState: &os.ProcessState{},
exitCode: func() chan int {
code := make(chan int, 1)
code <- 0
return code
}(),
},
true,
},
{
"process failed",
fields{
process: &os.Process{},
exitCode: func() chan int {
code := make(chan int, 1)
code <- 1 // non zero exit code
return code
}(),
},
true,
},
{
"context already canceled",
fields{
process: &os.Process{},
ctx: func() context.Context {
ctx, cancel := context.WithCancel(context.Background())
cancel()
return ctx
}(),
ctxErr: func() chan error {
err := make(chan error, 1)
err <- errors.New("context canceled")
return err
}(),
exitCode: func() chan int {
code := make(chan int, 1)
code <- 0
return code
}(),
},
true,
},
{
"success",
fields{
process: &os.Process{},
exitCode: func() chan int {
code := make(chan int, 1)
code <- 0
return code
}(),
},
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &FuncCmd{
process: tt.fields.process,
processState: tt.fields.processState,
ctx: tt.fields.ctx,
ctxErr: tt.fields.ctxErr,
startErr: tt.fields.startErr,
exitCode: tt.fields.exitCode,
}
if err := c.Wait(); (err != nil) != tt.wantErr {
t.Errorf("FuncCmd.Wait() error = %v, wantErr %v", err, tt.wantErr)
}
if tt.wantErr {
return
}
// check that process state was set
if c.processState == nil {
t.Errorf("FuncCmd.Wait() process state was not set")
}
})
}
}
func TestFuncCmd_Run(t *testing.T) {
type fields struct {
path string
stdout io.Writer
ctx context.Context
ctxErr chan error
exitCode chan int
fExec *FuncExec
}
tests := []struct {
name string
fields fields
wantErr bool
wantStart bool
}{
{
"canceled command stdout locked",
fields{
path: "slow",
ctx: func() context.Context {
ctx, _ := context.WithTimeout(context.Background(), time.Millisecond*5)
return ctx
}(),
exitCode: make(chan int, 1),
ctxErr: make(chan error, 1),
stdout: newLockableBuffer(),
fExec: &FuncExec{
funcMap: map[string]CmdFunc{
"slow": func(fc *FuncCmd) int {
fc.stdout.Write([]byte("test was run"))
time.Sleep(time.Millisecond * 10)
fc.stdout.Write([]byte("sleep finished"))
return 0
},
},
},
},
true,
true,
},
{
"uncanceled command",
fields{
path: "fast",
ctx: func() context.Context {
ctx, _ := context.WithTimeout(context.Background(), time.Minute)
return ctx
}(),
exitCode: make(chan int, 1),
ctxErr: make(chan error, 1),
stdout: newLockableBuffer(),
fExec: &FuncExec{
funcMap: map[string]CmdFunc{
"fast": func(fc *FuncCmd) int {
_, err := fc.stdout.Write([]byte("test was run"))
if err != nil {
return 1
}
return 0
},
},
},
},
false,
true,
},
{
"command failed",
fields{
path: "test",
exitCode: make(chan int, 1),
ctxErr: make(chan error, 1),
stdout: newLockableBuffer(),
fExec: &FuncExec{
funcMap: map[string]CmdFunc{
"test": func(fc *FuncCmd) int {
fc.stdout.Write([]byte("test was run"))
return 1 // no-zero exit code
},
},
},
},
true,
true,
},
{
"missing command",
fields{
path: "test",
fExec: &FuncExec{},
},
true,
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &FuncCmd{
path: tt.fields.path,
stdout: tt.fields.stdout,
ctx: tt.fields.ctx,
ctxErr: tt.fields.ctxErr,
exitCode: tt.fields.exitCode,
fExec: tt.fields.fExec,
}
if err := c.Run(); (err != nil) != tt.wantErr {
t.Errorf("FuncCmd.Run() error = %v, wantErr %v", err, tt.wantErr)
}
if !tt.wantStart {
return
}
// check that the function was started
// make sure to wait long enough for the cmd func to finish
time.Sleep(time.Millisecond * 10)
gotStdout := c.stdout.(*lockableBuffer).String()
wantStdout := "test was run"
if gotStdout != wantStdout {
t.Errorf("FuncCmd.Run() cmd func was not run, got stdout %s but wanted %s", gotStdout, wantStdout)
}
})
}
}
func TestFuncCmd_CombinedOutput(t *testing.T) {
type fields struct {
path string
stdout io.Writer
stderr io.Writer
ctxErr chan error
exitCode chan int
fExec *FuncExec
}
tests := []struct {
name string
fields fields
want []byte
wantErr bool
}{
{
"with stdout",
fields{
path: "test",
exitCode: make(chan int, 1),
ctxErr: make(chan error, 1),
fExec: &FuncExec{
funcMap: map[string]CmdFunc{
"test": func(fc *FuncCmd) int {
fc.stdout.Write([]byte("test was run"))
return 0
},
},
},
},
[]byte("test was run"),
false,
},
{
"with stderr",
fields{
path: "test",
exitCode: make(chan int, 1),
ctxErr: make(chan error, 1),
fExec: &FuncExec{
funcMap: map[string]CmdFunc{
"test": func(fc *FuncCmd) int {
fc.stderr.Write([]byte("test was run"))
return 0
},
},
},
},
[]byte("test was run"),
false,
},
{
"both",
fields{
path: "test",
exitCode: make(chan int, 1),
ctxErr: make(chan error, 1),
fExec: &FuncExec{
funcMap: map[string]CmdFunc{
"test": func(fc *FuncCmd) int {
fc.stdout.Write([]byte("test was run"))
fc.stderr.Write([]byte("there was an err"))
return 0
},
},
},
},
[]byte("test was runthere was an err"),
false,
},
{
"missing command",
fields{
path: "test",
exitCode: make(chan int, 1),
ctxErr: make(chan error, 1),
fExec: &FuncExec{},
},
[]byte{},
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &FuncCmd{
path: tt.fields.path,
stdout: tt.fields.stdout,
stderr: tt.fields.stderr,
ctxErr: tt.fields.ctxErr,
exitCode: tt.fields.exitCode,
fExec: tt.fields.fExec,
}
got, err := c.CombinedOutput()
if (err != nil) != tt.wantErr {
t.Errorf("FuncCmd.CombinedOutput() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("FuncCmd.CombinedOutput() = %v, want %v", got, tt.want)
}
})
}
}
func TestFuncCmd_Environ(t *testing.T) {
type fields struct {
env map[string]string
fExec *FuncExec
}
tests := []struct {
name string
fields fields
want []string
}{
{
"exec environ",
fields{
fExec: &FuncExec{
envs: map[string]string{
"TEST": "true",
"DEBUG": "false",
"EXEC": "10",
},
},
},
[]string{"DEBUG=false", "EXEC=10", "TEST=true"},
},
{
"cmd environ",
fields{
env: map[string]string{
"TEST": "false",
"DEBUG": "true",
"EXEC": "15",
},
fExec: &FuncExec{
envs: map[string]string{
"TEST": "true",
"DEBUG": "false",
"EXEC": "10",
},
},
},
[]string{"DEBUG=true", "EXEC=15", "TEST=false"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &FuncCmd{
env: tt.fields.env,
fExec: tt.fields.fExec,
}
if got := c.Environ(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("FuncCmd.Environ() = %v, want %v", got, tt.want)
}
})
}
}
func TestFuncCmd_Output(t *testing.T) {
type fields struct {
path string
ctxErr chan error
exitCode chan int
fExec *FuncExec
}
tests := []struct {
name string
fields fields
want []byte
wantErr bool
}{
{
"with output",
fields{
path: "test",
ctxErr: make(chan error, 1),
exitCode: make(chan int, 1),
fExec: &FuncExec{
funcMap: map[string]CmdFunc{
"test": func(fc *FuncCmd) int {
fc.stdout.Write([]byte("test was run"))
return 0
},
},
},
},
[]byte("test was run"),
false,
},
{
"with std error",
fields{
path: "test",
ctxErr: make(chan error, 1),
exitCode: make(chan int, 1),
fExec: &FuncExec{
funcMap: map[string]CmdFunc{
"test": func(fc *FuncCmd) int {
fc.stderr.Write([]byte("test was run"))
return 1
},
},
},
},
[]byte{},
true,
},
{
"missing cmd",
fields{
path: "test",
fExec: &FuncExec{},
},
[]byte{},
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &FuncCmd{
path: tt.fields.path,
ctxErr: tt.fields.ctxErr,
exitCode: tt.fields.exitCode,
fExec: tt.fields.fExec,
}
got, err := c.Output()
if (err != nil) != tt.wantErr {
t.Errorf("FuncCmd.Output() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("FuncCmd.Output() = %v, want %v", string(got), string(tt.want))
}
})
}
}
func TestFuncCmd_StderrPipe(t *testing.T) {
type fields struct {
path string
stderr io.Writer
process *os.Process
ctxErr chan error
exitCode chan int
fExec *FuncExec
}
tests := []struct {
name string
fields fields
wantOutput []byte
wantErr bool
}{
{
"stderr already set",
fields{
stderr: &bytes.Buffer{},
},
nil,
true,
},
{
"process already started",
fields{
process: &os.Process{},
},
nil,
true,
},
{
"write to stderr",
fields{
path: "test",
ctxErr: make(chan error, 1),
exitCode: make(chan int, 1),
fExec: &FuncExec{
funcMap: map[string]CmdFunc{
"test": func(fc *FuncCmd) int {
fc.stderr.Write([]byte("test was run"))
return 0
},
},
},
},
[]byte("test was run"),
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &FuncCmd{