-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
75 lines (65 loc) · 1.55 KB
/
util.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
package main
import (
"fmt"
"log"
"net/url"
)
var numTests int // for numbering output
// allOk can be set to false for a non-zero exit status.
// The assert functions do this for you.
var allOk = true
// test wraps test functions with logging.
func test(descr string, block func()) {
numTests++
log.Printf("Test %d: %s", numTests, descr)
// sync up
sync := fmt.Sprintf("test %d", numTests)
proc.writeLine("echo", sync)
cmd := proc.readLine()
if cmd[0] != "ok" || cmd[1] != sync {
log.Fatal("Test runner out of sync")
}
block()
}
// assert logs the result of a check.
func assert(val bool, descr string) bool {
if !val {
allOk = false
log.Printf(" ERR %s", descr)
return false
}
log.Printf(" OK %s", descr)
return true
}
// assertEq logs values if they are unequal.
func assertEq(a, b interface{}, descr string) bool {
if !assert(a == b, descr) {
log.Printf("got: %+v", a)
log.Printf("want: %+v", b)
return false
}
return true
}
// assertOK logs the error if not nil.
func assertOK(err error, descr string) bool {
if !assert(err == nil, descr) {
log.Printf("%s", err.Error())
return false
}
return true
}
// quickStart sends the authentication request and returns a nonce.
// Useful for tests focussing on the verification step.
func quickStart(email string) string {
var nonce string
proc.writeLine("auth", email)
cmd := proc.readLine()
if cmd[0] == "ok" {
authURL, err := url.Parse(cmd[1])
if err == nil {
nonce = authURL.Query().Get("nonce")
}
}
assert(nonce != "", "start authentication request")
return nonce
}