-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathmemexec_test.go
133 lines (115 loc) · 2.66 KB
/
memexec_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
package memexec
import (
"bytes"
"context"
"os"
"os/exec"
"runtime"
"strings"
"testing"
"time"
)
func TestCommand(t *testing.T) {
exe := newEchoExec(t)
defer func() {
if err := exe.Close(); err != nil {
t.Fatalf("close error: %s", err)
}
}()
c := exe.Command("test")
if have := runCommand(t, c); !strings.HasPrefix(have, "test") {
t.Errorf("command output = %q doesn't contain %q", have, "test")
}
}
func TestCommand_Shell(t *testing.T) {
if runtime.GOOS == "windows" {
return
}
exe, err := New([]byte("#!/bin/sh\necho hello"))
if err != nil {
panic(err)
}
defer exe.Close()
b, err := exe.Command().CombinedOutput()
if err != nil {
t.Fatalf("err = %v, output = %q", err, b)
}
if !bytes.Equal(b, []byte("hello\n")) {
t.Fatalf("output = %q, want %q", string(b), []byte("hello\n"))
}
}
func TestCommandContext(t *testing.T) {
// the test is failing on windows, probably due to missing sleep command,
// unfortunately I have no windows machines around to fix this
if runtime.GOOS == "windows" {
return
}
exe := newSleepExec(t)
defer func() {
if err := exe.Close(); err != nil {
t.Fatalf("close error: %s", err)
}
}()
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*500)
defer cancel()
c := exe.CommandContext(ctx, "1")
start := time.Now()
if err := c.Start(); err != nil {
t.Fatalf("start failed: %s", err)
}
if err := c.Wait(); err != nil && err.Error() != "signal: killed" {
t.Fatalf("command execution failed: %s", err)
}
stop := time.Now()
if delta := stop.Sub(start); delta > time.Millisecond*600 || delta < time.Millisecond*500 {
t.Fatalf("unexpected command execution time: delta=%s", delta)
}
}
func BenchmarkCommand(b *testing.B) {
exe := newEchoExec(b)
defer func() {
if err := exe.Close(); err != nil {
b.Fatalf("close error: %s", err)
}
}()
for i := 0; i < b.N; i++ {
cmd := exe.Command("test")
if _, err := cmd.Output(); err != nil {
b.Fatal(err)
}
}
}
func newExec(t testing.TB, name string) *Exec {
path, err := exec.LookPath(name)
if err != nil {
t.Fatal(err)
}
b, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
exe, err := New(b)
if err != nil {
t.Fatal(err)
}
return exe
}
func newEchoExec(t testing.TB) *Exec {
// lookup echo binary that is provided on all unix systems
// and it's not a built-in opposed to `ls` and `type`
return newExec(t, "echo")
}
func newSleepExec(t testing.TB) *Exec {
return newExec(t, "sleep")
}
func runCommand(t *testing.T, cmd *exec.Cmd) string {
t.Helper()
b, err := cmd.CombinedOutput()
if err != nil {
if b != nil {
t.Fatalf("failed to run: %q", string(b))
}
t.Fatal(err)
}
return string(b)
}