-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutor_test.go
More file actions
77 lines (64 loc) · 2.14 KB
/
executor_test.go
File metadata and controls
77 lines (64 loc) · 2.14 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
package machineid
import (
"context"
"fmt"
"testing"
"time"
)
// mockExecutor is a test double that implements CommandExecutor for testing.
type mockExecutor struct {
// outputs maps command name to expected output
outputs map[string]string
// errors maps command name to expected error
errors map[string]error
// callCount tracks how many times each command was called
callCount map[string]int
}
// newMockExecutor creates a new mock executor for testing.
func newMockExecutor() *mockExecutor {
return &mockExecutor{
outputs: make(map[string]string),
errors: make(map[string]error),
callCount: make(map[string]int),
}
}
// Execute implements CommandExecutor interface.
func (m *mockExecutor) Execute(ctx context.Context, name string, args ...string) (string, error) {
m.callCount[name]++
if err, exists := m.errors[name]; exists {
return "", err
}
if output, exists := m.outputs[name]; exists {
return output, nil
}
return "", fmt.Errorf("command %q not configured in mock", name)
}
// setOutput configures the mock to return the given output for a command.
func (m *mockExecutor) setOutput(command, output string) {
m.outputs[command] = output
}
// setError configures the mock to return an error for a command.
func (m *mockExecutor) setError(command string, err error) {
m.errors[command] = err
}
// TestExecuteTimeout tests that command execution respects timeout.
func TestExecuteTimeout(t *testing.T) {
executor := &defaultCommandExecutor{}
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Nanosecond)
defer cancel()
time.Sleep(2 * time.Millisecond) // Ensure timeout expires
_, err := executor.Execute(ctx, "echo", "test")
if err == nil {
t.Error("Expected timeout error but got none")
}
}
// TestExecuteCommandWithNilExecutor tests executeCommand with nil executor.
func TestExecuteCommandWithNilExecutor(t *testing.T) {
// This should use the default realExecutor
_, err := executeCommand(context.Background(), nil, nil, "echo", "test")
// We expect this to work or fail gracefully
if err != nil {
// That's fine, we just want to ensure no panic
t.Logf("Command execution with nil executor: %v", err)
}
}