generated from moul/golang-repo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexec_test.go
69 lines (62 loc) · 1.27 KB
/
exec_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
package u_test
import (
"fmt"
"os/exec"
"testing"
"moul.io/u"
)
func ExampleExecStandaloneOutputs() {
stdout, stderr, err := u.ExecStandaloneOutputs(exec.Command("sh", "-c", "echo stdout; echo 1>&2 stderr"))
fmt.Print("stdout: ", string(stdout))
fmt.Print("stderr: ", string(stderr))
fmt.Println("err: ", err)
// Output:
// stdout: stdout
// stderr: stderr
// err: <nil>
}
func ExampleSafeExec() {
out := u.SafeExec(exec.Command("sh", "-c", "echo stdout; echo 1>&2 stderr; exit 1"))
fmt.Println(out)
// Output:
// stdout
// stderr
//
// error: exit status 1
}
func ExampleCommandExists() {
fmt.Println(u.CommandExists("go"))
fmt.Println(u.CommandExists("asldkglsakdjaslkdg"))
// Output:
// true
// false
}
func BenchmarkCommandExists(b *testing.B) {
cases := []struct {
Command string
}{
{"go"},
{"asddsa"},
}
for _, bc := range cases {
b.Run(bc.Command, func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
u.CommandExists(bc.Command)
}
})
b.Run(bc.Command+"-parallel", func(b *testing.B) {
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
u.CommandExists(bc.Command)
}
})
})
}
}
func BenchmarkSafeExec(b *testing.B) {
for i := 0; i < b.N; i++ {
u.SafeExec(exec.Command("true"))
}
}