-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_test.go
70 lines (56 loc) · 1.42 KB
/
main_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
package bintest_test
import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"github.com/buildkite/bintest/v3"
)
func ExampleLinkTestBinaryAsProxy() {
// create a proxy for the git command that echos some debug
p, err := bintest.LinkTestBinaryAsProxy("git")
if err != nil {
log.Fatal(err)
}
// call the proxy like a normal binary in the background
cmd := exec.Command(p.Path, "rev-parse")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
// windows needs all the environment variables
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, p.Environ()...)
cmd.Env = append(cmd.Env, `MY_MESSAGE=Llama party! 🎉`)
if err := cmd.Start(); err != nil {
log.Fatal(err)
}
// handle invocations of the proxy binary
call := <-p.Ch
fmt.Fprintln(call.Stdout, call.GetEnv(`MY_MESSAGE`))
call.Exit(0)
// wait for the command to finish
if err := cmd.Wait(); err != nil {
log.Fatal(err)
}
// cleanup the proxy
if err := p.Close(); err != nil {
log.Fatal(err)
}
// Output: Llama party! 🎉
}
func TestMain(m *testing.M) {
// flag.BoolVar(&proxy.Debug, "proxy.debug", false, "Whether to show proxy debug")
// flag.Parse()
if strings.TrimSuffix(filepath.Base(os.Args[0]), `.exe`) != `bintest.test` {
os.Exit(bintest.NewClientFromEnv().Run())
}
_, err := bintest.StartServer()
if err != nil {
fmt.Printf("Failed to start proxy server: %v", err)
os.Exit(1)
}
code := m.Run()
os.Exit(code)
}