-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexec_test.go
More file actions
151 lines (129 loc) · 4.52 KB
/
Copy pathexec_test.go
File metadata and controls
151 lines (129 loc) · 4.52 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
package main
import (
"strings"
"testing"
"time"
)
func TestCmdResultOK(t *testing.T) {
res1 := CmdResult{ExitCode: 0, Err: nil}
if !res1.OK() {
t.Error("expected ExitCode 0 and Err nil to be OK")
}
res2 := CmdResult{ExitCode: 1, Err: nil}
if res2.OK() {
t.Error("expected ExitCode 1 to NOT be OK")
}
}
func TestRunCmdReal(t *testing.T) {
// Simple echo check
res := runCmdReal([]string{"echo", "hello world"}, CmdOpts{Capture: true})
if !res.OK() {
t.Errorf("expected OK command, got result: %+v", res)
}
out := strings.TrimSpace(string(res.Stdout))
if out != "hello world" {
t.Errorf("expected 'hello world', got %q", out)
}
// Exit failure check
resFail := runCmdReal([]string{"false"}, CmdOpts{Capture: true})
if resFail.OK() {
t.Error("expected false command to fail")
}
if resFail.ExitCode != 1 {
t.Errorf("expected exit code 1, got %d", resFail.ExitCode)
}
// Timeout check (using a short timeout)
resTimeout := runCmdReal([]string{"sleep", "5"}, CmdOpts{Timeout: 10 * time.Millisecond, Capture: true})
if resTimeout.ExitCode != 124 {
t.Errorf("expected exit code 124 (timeout), got %d", resTimeout.ExitCode)
}
}
func TestRunShellReal(t *testing.T) {
// Simple shell command
res := runShellReal("echo shell hello", CmdOpts{Capture: true})
if !res.OK() {
t.Errorf("expected OK shell command, got result: %+v", res)
}
out := strings.TrimSpace(string(res.Stdout))
if out != "shell hello" {
t.Errorf("expected 'shell hello', got %q", out)
}
// Failed command
resFail := runShellReal("exit 42", CmdOpts{Capture: true})
if resFail.OK() {
t.Error("expected shell command with exit 42 to fail")
}
if resFail.ExitCode != 42 {
t.Errorf("expected exit code 42, got %d", resFail.ExitCode)
}
}
func TestHasCmdReal(t *testing.T) {
if !hasCmdReal("go") {
t.Error("expected hasCmdReal('go') to return true (running go test)")
}
if hasCmdReal("nonexistent-command-xyz") {
t.Error("expected nonexistent command to return false")
}
}
func TestProbeReal(t *testing.T) {
res, ok := probeReal([]string{"echo", "probe"}, 0)
if !ok {
t.Error("expected probe to succeed")
}
if strings.TrimSpace(string(res.Stdout)) != "probe" {
t.Errorf("expected 'probe', got %q", string(res.Stdout))
}
// Test a failing command probe
resFail, okFail := probeReal([]string{"false"}, 0)
if !okFail {
t.Error("expected probe check of false command to return ok=true (meaning it completed and didn't crash/timeout)")
}
if resFail.ExitCode != 1 {
t.Errorf("expected exit code 1, got %d", resFail.ExitCode)
}
// Test an invalid command (launch failure)
_, okErr := probeReal([]string{"nonexistent-executable-file"}, 0)
if okErr {
t.Error("expected probe to return false on launch failure")
}
}
func TestExecRealEdgeCases(t *testing.T) {
// 2. Cwd and Input in runCmdReal
tmp := t.TempDir()
resCwd := runCmdReal([]string{"pwd"}, CmdOpts{Cwd: tmp, Capture: true})
if !resCwd.OK() {
t.Errorf("pwd failed: %+v", resCwd)
}
resInput := runCmdReal([]string{"cat"}, CmdOpts{Input: []byte("my-input"), Capture: true})
if !resInput.OK() || strings.TrimSpace(string(resInput.Stdout)) != "my-input" {
t.Errorf("cat input failed, got result: %+v", resInput)
}
// 3. Capture = false in runCmdReal
_ = runCmdReal([]string{"echo", "capture-false-cmd"}, CmdOpts{Capture: false})
// 4. Launch failure in runCmdReal (not exit error)
resLaunch := runCmdReal([]string{"nonexistent-command-12345"}, CmdOpts{Capture: true})
if resLaunch.OK() || resLaunch.ExitCode != 1 || resLaunch.Err == nil {
t.Errorf("expected launch failure, got: %+v", resLaunch)
}
// 5. Cwd and Input in runShellReal
resShellCwd := runShellReal("pwd", CmdOpts{Cwd: tmp, Capture: true})
if !resShellCwd.OK() {
t.Errorf("shell pwd failed: %+v", resShellCwd)
}
resShellInput := runShellReal("cat", CmdOpts{Input: []byte("my-shell-input"), Capture: true})
if !resShellInput.OK() || strings.TrimSpace(string(resShellInput.Stdout)) != "my-shell-input" {
t.Errorf("shell cat input failed, got: %+v", resShellInput)
}
// 6. Capture = false in runShellReal
_ = runShellReal("echo capture-false-shell", CmdOpts{Capture: false})
// 7. Timeout in runShellReal
resShellTimeout := runShellReal("sleep 5", CmdOpts{Timeout: 10 * time.Millisecond, Capture: true})
if resShellTimeout.ExitCode != 124 {
t.Errorf("expected exit code 124 for shell timeout, got %d", resShellTimeout.ExitCode)
}
// 8. Timeout in probeReal
_, okProbeTimeout := probeReal([]string{"sleep", "5"}, 10*time.Millisecond)
if okProbeTimeout {
t.Error("expected probe to return ok=false on timeout")
}
}