...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package testutil
16
17 import (
18 "io/ioutil"
19 "log"
20 "os"
21 )
22
23
24
25 type TB interface {
26 Cleanup(func())
27 Error(args ...interface{})
28 Errorf(format string, args ...interface{})
29 Fail()
30 FailNow()
31 Failed() bool
32 Fatal(args ...interface{})
33 Fatalf(format string, args ...interface{})
34 Logf(format string, args ...interface{})
35 Name() string
36 TempDir() string
37 Helper()
38 Skip(args ...interface{})
39 }
40
41
42
43
44
45
46
47
48 func NewTestingTBProthesis(name string) (tb TB, closef func()) {
49 testtb := &testingTBProthesis{name: name}
50 return testtb, testtb.close
51 }
52
53 type testingTBProthesis struct {
54 name string
55 failed bool
56 cleanups []func()
57 }
58
59 func (t *testingTBProthesis) Helper() {
60
61 }
62
63 func (t *testingTBProthesis) Skip(args ...interface{}) {
64 t.Log(append([]interface{}{"Skipping due to: "}, args...))
65 }
66
67 func (t *testingTBProthesis) Cleanup(f func()) {
68 t.cleanups = append(t.cleanups, f)
69 }
70
71 func (t *testingTBProthesis) Error(args ...interface{}) {
72 log.Println(args...)
73 t.Fail()
74 }
75
76 func (t *testingTBProthesis) Errorf(format string, args ...interface{}) {
77 log.Printf(format, args...)
78 t.Fail()
79 }
80
81 func (t *testingTBProthesis) Fail() {
82 t.failed = true
83 }
84
85 func (t *testingTBProthesis) FailNow() {
86 t.failed = true
87 panic("FailNow() called")
88 }
89
90 func (t *testingTBProthesis) Failed() bool {
91 return t.failed
92 }
93
94 func (t *testingTBProthesis) Fatal(args ...interface{}) {
95 log.Fatalln(args...)
96 }
97
98 func (t *testingTBProthesis) Fatalf(format string, args ...interface{}) {
99 log.Fatalf(format, args...)
100 }
101
102 func (t *testingTBProthesis) Logf(format string, args ...interface{}) {
103 log.Printf(format, args...)
104 }
105
106 func (t *testingTBProthesis) Log(args ...interface{}) {
107 log.Println(args...)
108 }
109
110 func (t *testingTBProthesis) Name() string {
111 return t.name
112 }
113
114 func (t *testingTBProthesis) TempDir() string {
115 dir, err := ioutil.TempDir("", t.name)
116 if err != nil {
117 t.Fatal(err)
118 }
119 t.cleanups = append([]func(){func() {
120 t.Logf("Cleaning UP: %v", dir)
121 os.RemoveAll(dir)
122 }}, t.cleanups...)
123 return dir
124 }
125
126 func (t *testingTBProthesis) close() {
127 for i := len(t.cleanups) - 1; i >= 0; i-- {
128 t.cleanups[i]()
129 }
130 }
131
View as plain text