-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrunner_test.go
96 lines (84 loc) · 1.87 KB
/
runner_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
package coderunner
import (
"errors"
"reflect"
"testing"
"github.com/golang/mock/gomock"
"github.com/runner-x/runner-x/engine/runtime"
"github.com/runner-x/runner-x/engine/runtime/mocks"
)
func TestCodeRunner_Run(t *testing.T) {
type fields struct {
runner runtime.Runtime
workdirPath string
}
type args struct {
props *RunnerProps
}
signalKilledError := errors.New("signal: killed")
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockSuccess := mocks.NewMockRuntime(ctrl)
mockFails := mocks.NewMockRuntime(ctrl)
// happy case
mockSuccess.EXPECT().RunCmd(
gomock.Any(),
).Return(&runtime.RunOutput{Stdout: "hello world", Stderr: ""}, nil)
mockFails.EXPECT().RunCmd(
gomock.Any(),
).Return(&runtime.RunOutput{Stdout: "", Stderr: "error"}, signalKilledError)
tests := []struct {
name string
mock runtime.Runtime
args args
want *RunnerOutput
wantErr bool
}{
{
name: "Test Successful Run",
mock: mockSuccess,
args: args{
props: &RunnerProps{
Lang: PYTHON3,
Source: "print(\"hello world\")",
},
},
want: &RunnerOutput{
Stdout: "hello world",
Stderr: "",
CommandError: nil,
},
wantErr: false,
},
{
name: "Runtime Failure",
mock: mockFails,
args: args{
props: &RunnerProps{
Lang: SHELL,
Source: `
#!/bin/bash
sleep 10
`},
},
want: &RunnerOutput{
Stdout: "",
Stderr: "error",
CommandError: signalKilledError,
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := &CodeRunner{runner: tt.mock, workdirPath: ""}
got, err := d.Run(tt.args.props)
if (err != nil) != tt.wantErr {
t.Errorf("Run() error : %v, wantErr %v", err, tt.wantErr)
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Run() got = %v, want %v", got, tt.want)
}
})
}
}