forked from perkeep/perkeep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.go
81 lines (71 loc) · 1.95 KB
/
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
/*
Copyright 2013 The Camlistore Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package test
import (
"bytes"
"io"
"io/ioutil"
"log"
"os"
"strconv"
"testing"
)
// BrokenTest marks the test as broken and calls t.Skip, unless the environment
// variable RUN_BROKEN_TESTS is set to 1 (or some other boolean true value).
func BrokenTest(t *testing.T) {
if v, _ := strconv.ParseBool(os.Getenv("RUN_BROKEN_TESTS")); !v {
t.Skipf("Skipping broken tests without RUN_BROKEN_TESTS=1")
}
}
// TB is a copy of Go 1.2's testing.TB.
type TB interface {
Error(args ...interface{})
Errorf(format string, args ...interface{})
Fail()
FailNow()
Failed() bool
Fatal(args ...interface{})
Fatalf(format string, args ...interface{})
Log(args ...interface{})
Logf(format string, args ...interface{})
Skip(args ...interface{})
SkipNow()
Skipf(format string, args ...interface{})
Skipped() bool
}
// TLog changes the log package's output to log to t and returns a function
// to reset it back to stderr.
func TLog(t TB) func() {
// TODO(bradfitz): once we rely on Go 1.2, change this to take a testing.TB.
log.SetOutput(&twriter{t: t})
return func() {
log.SetOutput(os.Stderr)
}
}
type twriter struct {
t TB
buf bytes.Buffer
}
func (w *twriter) Write(p []byte) (n int, err error) {
n, err = w.buf.Write(p)
for {
i := bytes.IndexByte(w.buf.Bytes(), '\n')
if i < 0 {
return
}
if i > 0 {
w.t.Log(string(w.buf.Bytes()[:i]))
}
io.CopyN(ioutil.Discard, &w.buf, int64(i)+1)
}
}