-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient_test.go
83 lines (69 loc) · 1.54 KB
/
client_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
package watchman
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"regexp"
"testing"
"time"
)
var (
sock string
)
func TestMain(m *testing.M) {
os.Exit(testMain(m))
}
func testMain(m *testing.M) int {
d, err := ioutil.TempDir("", "watchman")
if err != nil {
fmt.Printf("Error creating tempdir %s\n", err)
os.Exit(1)
}
var stderr bytes.Buffer
sock = filepath.Join(d, "sock")
cmd := exec.Command("watchman", "--foreground", "--no-save-state", "--logfile="+filepath.Join(d, "log"), "--pidfile="+filepath.Join(d, "pid"), "--sockname="+sock)
cmd.Stderr = &stderr
if err := cmd.Start(); err != nil {
fmt.Printf("Error starting watchman %s\n", err)
os.Exit(1)
}
ch := make(chan error)
go func() {
ch <- cmd.Wait()
}()
time.Sleep(500 * time.Millisecond)
select {
case <-ch:
fmt.Printf("Error running watchman %s\n", stderr.String())
os.Exit(1)
default:
}
defer func() {
if err := cmd.Process.Kill(); err != nil {
fmt.Printf("Error stopping watchman %s\n", err)
os.Exit(1)
}
}()
return m.Run()
}
func expectErrEqual(t *testing.T, err error, msg string) {
t.Helper()
if err == nil {
t.Fatalf("Expected error")
}
if err.Error() != msg {
t.Fatalf("Expected error message to be \"%s\" but found \"%s\"", msg, err.Error())
}
}
func expectErrRegex(t *testing.T, err error, pattern string) {
t.Helper()
if err == nil {
t.Fatalf("Expected error")
}
if !regexp.MustCompile(pattern).MatchString(err.Error()) {
t.Fatalf("Expected error to match \"%s\" but found \"%s\"", pattern, err.Error())
}
}