-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexec_test.go
More file actions
75 lines (58 loc) · 1.41 KB
/
exec_test.go
File metadata and controls
75 lines (58 loc) · 1.41 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
package main
import (
"context"
"fmt"
"os"
"os/exec"
"strconv"
"strings"
"testing"
)
const (
envHelper = "GO_WANT_HELPER_PROCESS"
envResult = "GO_EXEC_RESULT"
envExitCode = "GO_EXEC_EXIT_CODE"
)
type trapExec struct {
text string
code int
called int
}
var result = make(map[string]*trapExec)
func TestHelperProcess(t *testing.T) {
if os.Getenv(envHelper) != "1" {
return
}
fmt.Println(os.Getenv(envResult))
code, err := strconv.Atoi(os.Getenv(envExitCode))
if err != nil {
fmt.Println(err)
os.Exit(2)
}
if code != 0 {
os.Exit(code)
}
//bluetoothctl говно по сути, и какого-то хера он яростно кеширует всё, что не падает в stdout
select {}
}
func helperCommandContext(ctx context.Context, name string, args ...string) (cmd *exec.Cmd) {
cs := []string{"-test.run=TestHelperProcess", "--"}
cs = append(cs, name)
cs = append(cs, args...)
if ctx != context.TODO() {
cmd = exec.CommandContext(ctx, os.Args[0], cs...)
} else {
cmd = exec.Command(os.Args[0], cs...)
}
res := result[strings.Join(cs[2:], " ")]
res.called++
cmd.Env = append(os.Environ(),
fmt.Sprintf("%s=%d", envHelper, 1),
fmt.Sprintf("%s=%d", envExitCode, res.code),
fmt.Sprintf("%s=%s", envResult, res.text),
)
return cmd
}
func helperCommand(name string, args ...string) *exec.Cmd {
return helperCommandContext(context.TODO(), name, args...)
}