-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunProgram_test.go
More file actions
308 lines (246 loc) · 6.95 KB
/
RunProgram_test.go
File metadata and controls
308 lines (246 loc) · 6.95 KB
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
package gorun
import (
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
)
func TestRunProgram_Basic(t *testing.T) {
// Build test program
execPath := buildTestProgram(t, "simple_program")
defer os.Remove(execPath)
exitChan := make(chan bool)
buf, logger := createTestLogger()
config := &Config{
ExecProgramPath: execPath,
RunArguments: func() []string { return []string{} },
ExitChan: exitChan,
Logger: logger,
}
gr := New(config)
// Run the program
err := gr.RunProgram()
if err != nil {
t.Fatalf("RunProgram() failed: %v", err)
}
// Should be running
if !gr.IsRunning() {
t.Error("Program should be running after RunProgram()")
}
// Clean up
gr.StopProgram()
_ = buf // Use buf to avoid unused variable error
}
func TestRunProgram_WithArguments(t *testing.T) {
// Create a program that prints its arguments
argProgram := `package main
import (
"fmt"
"os"
"strings"
"time"
)
func main() {
fmt.Println("ARGS_START")
fmt.Println("ARGS:" + strings.Join(os.Args[1:], ","))
time.Sleep(100 * time.Millisecond) // Keep it alive long enough to test
fmt.Println("ARGS_END")
}`
// Write to temporary file
tmpFile := filepath.Join(os.TempDir(), "arg_program.go")
err := os.WriteFile(tmpFile, []byte(argProgram), 0644)
if err != nil {
t.Fatalf("Failed to write test program: %v", err)
}
defer os.Remove(tmpFile)
// Build it
ext := ""
if runtime.GOOS == "windows" {
ext = ".exe"
}
execPath := filepath.Join(os.TempDir(), "arg_program"+ext)
cmd := exec.Command("go", "build", "-o", execPath, tmpFile)
if err := cmd.Run(); err != nil {
t.Fatalf("Failed to build arg program: %v", err)
}
defer os.Remove(execPath)
exitChan := make(chan bool)
buf, logger := createTestLogger()
testArgs := []string{"test1", "test2", "test3"}
config := &Config{
ExecProgramPath: execPath,
RunArguments: func() []string { return testArgs },
ExitChan: exitChan,
Logger: logger,
}
gr := New(config)
// Run the program
err = gr.RunProgram()
if err != nil {
t.Fatalf("RunProgram() failed: %v", err)
}
// Wait for output
time.Sleep(200 * time.Millisecond)
output := gr.getOutput()
// Check if the program ran (arguments are passed correctly)
// Since the logger captures output, we should see either the expected args or program completion
if !strings.Contains(output, "ARGS:") && !strings.Contains(output, "closed successfully") {
t.Errorf("Expected either program output or completion message. Got: %s", output)
}
// Clean up
gr.StopProgram()
_ = buf // Use buf to avoid unused variable error
}
func TestRunProgram_InvalidExecutable(t *testing.T) {
exitChan := make(chan bool)
buf, logger := createTestLogger()
config := &Config{
ExecProgramPath: "/nonexistent/path/program",
RunArguments: func() []string { return []string{} },
ExitChan: exitChan,
Logger: logger,
}
gr := New(config)
err := gr.RunProgram()
if err == nil {
t.Error("Expected error for invalid executable path")
}
_ = buf // Use buf to avoid unused variable error
}
func TestRunProgram_StopPrevious(t *testing.T) {
// Build test program
execPath := buildTestProgram(t, "long_program")
defer os.Remove(execPath)
exitChan := make(chan bool)
buf, logger := createTestLogger()
config := &Config{
ExecProgramPath: execPath,
RunArguments: func() []string { return []string{} },
ExitChan: exitChan,
Logger: logger,
}
gr := New(config)
// Start first program
err := gr.RunProgram()
if err != nil {
t.Fatalf("First RunProgram() failed: %v", err)
}
if !gr.IsRunning() {
t.Error("First program should be running")
}
// Wait a bit
time.Sleep(100 * time.Millisecond)
// Start second program - should stop the first one
err = gr.RunProgram()
if err != nil {
t.Fatalf("Second RunProgram() failed: %v", err)
}
if !gr.IsRunning() {
t.Error("Second program should be running")
}
// Clean up
gr.StopProgram()
_ = buf // Use buf to avoid unused variable error
}
func TestRunProgram_ExitChannel(t *testing.T) {
// Build test program
execPath := buildTestProgram(t, "long_program")
defer os.Remove(execPath)
exitChan := make(chan bool, 1)
buf, logger := createTestLogger()
config := &Config{
ExecProgramPath: execPath,
RunArguments: func() []string { return []string{} },
ExitChan: exitChan,
Logger: logger,
}
gr := New(config)
// Start program
err := gr.RunProgram()
if err != nil {
t.Fatalf("RunProgram() failed: %v", err)
}
if !gr.IsRunning() {
t.Error("Program should be running")
}
// Signal exit through channel
exitChan <- true
// Wait for the program to detect the exit signal and stop
time.Sleep(200 * time.Millisecond)
if gr.IsRunning() {
t.Error("Program should have stopped after exit signal")
}
_ = buf // Use buf to avoid unused variable error
}
func TestRunProgram_EmptyPath(t *testing.T) {
exitChan := make(chan bool)
buf, logger := createTestLogger()
config := &Config{
ExecProgramPath: "",
RunArguments: func() []string { return []string{} },
ExitChan: exitChan,
Logger: logger,
}
gr := New(config)
// Try to run with empty path
err := gr.RunProgram()
if err == nil {
t.Error("RunProgram() should fail for empty executable path")
}
if gr.IsRunning() {
t.Error("Program should not be running with empty path")
}
_ = buf // Use buf to avoid unused variable error
}
func TestRunProgram_ProgramExitsQuickly(t *testing.T) {
// Create a program that exits immediately
quickProgram := `package main
import "fmt"
func main() {
fmt.Println("QUICK_PROGRAM_EXECUTED")
}`
// Write to temporary file
tmpFile := filepath.Join(os.TempDir(), "quick_program.go")
err := os.WriteFile(tmpFile, []byte(quickProgram), 0644)
if err != nil {
t.Fatalf("Failed to write test program: %v", err)
}
defer os.Remove(tmpFile)
// Build it
ext := ""
if runtime.GOOS == "windows" {
ext = ".exe"
}
execPath := filepath.Join(os.TempDir(), "quick_program"+ext)
cmd := exec.Command("go", "build", "-o", execPath, tmpFile)
if err := cmd.Run(); err != nil {
t.Fatalf("Failed to build quick program: %v", err)
}
defer os.Remove(execPath)
exitChan := make(chan bool)
_, logger := createTestLogger()
config := &Config{
ExecProgramPath: execPath,
RunArguments: func() []string { return []string{} },
ExitChan: exitChan,
Logger: logger,
}
gr := New(config)
// Run the program
err = gr.RunProgram()
if err != nil {
t.Fatalf("RunProgram() failed: %v", err)
}
// Wait for program to complete
time.Sleep(200 * time.Millisecond)
output := gr.getOutput()
// Check that the program ran and completed (either output or completion message)
if !strings.Contains(output, "QUICK_PROGRAM_EXECUTED") && !strings.Contains(output, "closed successfully") {
t.Errorf("Expected either program output or completion message. Got: %s", output)
}
// Program should have exited on its own
// The IsRunning state depends on how the implementation handles finished processes
}