-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtest_runner.go
163 lines (133 loc) · 2.86 KB
/
test_runner.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package terraform_module_test_helper
import (
"fmt"
terratest "github.com/gruntwork-io/terratest/modules/testing"
"github.com/stretchr/testify/require"
"runtime"
"strings"
"sync"
"testing"
)
type testingT interface {
terratest.TestingT
T() *testing.T
Failed() bool
ErrorMessage() string
}
var _ testingT = &T{}
type T struct {
failed bool
t *testing.T
errorMessages []string
}
func (t *T) T() *testing.T {
return t.t
}
func newT(t *testing.T) *T {
return &T{t: t}
}
func (t *T) error(message string) {
t.errorMessages = append(t.errorMessages, message)
}
func (t *T) Failed() bool {
return t.failed
}
func (t *T) Fail() {
t.failed = true
t.error("Fail")
t.t.Fail()
}
func (t *T) FailNow() {
t.failed = true
t.error("FailNow")
t.t.FailNow()
}
func (t *T) Fatal(args ...interface{}) {
t.failed = true
t.error("Fatal:" + fmt.Sprintln(args...))
t.t.Fatal(args...)
}
func (t *T) Fatalf(format string, args ...interface{}) {
t.failed = true
t.error("Fatal:" + fmt.Sprintf(format, args...))
t.t.Fatalf(format, args...)
}
func (t *T) Error(args ...interface{}) {
t.error("Error:" + fmt.Sprintln(args...))
t.t.Error(args...)
}
func (t *T) Errorf(format string, args ...interface{}) {
t.error("Error:" + fmt.Sprintf(format, args...))
t.t.Errorf(format, args...)
}
func (t *T) Name() string {
return t.t.Name()
}
func (t *T) ErrorMessage() string {
sb := strings.Builder{}
for i := 0; i < len(t.errorMessages); i++ {
sb.WriteString(t.errorMessages[i])
if i < len(t.errorMessages)-1 {
sb.WriteString("\n")
}
}
return sb.String()
}
var _ testingT = &mockT{}
type mockT struct {
t *T
}
func (m *mockT) Name() string {
return m.t.Name()
}
func (m *mockT) T() *testing.T {
return m.t.T()
}
func (m *mockT) Failed() bool {
return m.t.Failed()
}
func (m *mockT) ErrorMessage() string {
return m.t.ErrorMessage()
}
func (m *mockT) Fail() {
m.t.failed = true
m.t.error("Fail")
}
func (m *mockT) FailNow() {
m.t.failed = true
m.t.error("Fail")
runtime.Goexit()
}
func (m *mockT) Fatal(args ...interface{}) {
m.t.failed = true
m.t.error("Fatal:" + fmt.Sprintln(args...))
runtime.Goexit()
}
func (m *mockT) Fatalf(format string, args ...interface{}) {
m.t.failed = true
m.t.error("Fatal:" + fmt.Sprintf(format, args...))
runtime.Goexit()
}
func (m *mockT) Error(args ...interface{}) {
m.t.error("Error:" + fmt.Sprintln(args...))
}
func (m *mockT) Errorf(format string, args ...interface{}) {
m.t.error("Error:" + fmt.Sprintf(format, args...))
}
func expectFailure(t *testing.T, f func(tt testingT)) *mockT {
m := &mockT{t: newT(t)}
var wg sync.WaitGroup
// setup the barrier
wg.Add(1)
// start a co-routine to execute the test function f
// and release the barrier at its end
go func() {
defer wg.Done()
f(m)
}()
// wait for the barrier.
wg.Wait()
// verify fail now is invoked
require.True(t, m.t.failed)
return m
}