Debian Codegraph @

runner.go

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 package expect
2
3 import (
4 "flag"
5 "fmt"
6 "os"
7 "reflect"
8 "regexp"
9 "runtime"
10 "strconv"
11 "strings"
12 "testing"
13 "time"
14
15 "github.com/wsxiaoys/terminal/color"
16 )
17
18 var (
19 showStdout = flag.Bool("vv", false, "turn on stdout")
20 matchFlag = flag.String("m", "", "Regular expression selecting which tests to run")
21 notMatchFlag = flag.String("M", "", "Regular expression selecting which tests not to run")
22 summaryPath = flag.String("summary", "", "Path to write a summary file to")
23 pattern *regexp.Regexp
24 notPattern *regexp.Regexp
25 runner *Runner
26 stdout = os.Stdout
27 silentOut *os.File
28 beforeEach = make([]func(), 0, 2)
29 endTestErr = new(error)
30 summaryPattern = regexp.MustCompile(`(?s)(\d+) passed.*?(\d+) failed`)
31 )
32
33 func init() {
34 os.Setenv("GO_TEST", "true")
35
36 flag.Parse()
37 if len(*matchFlag) != 0 {
38 pattern = regexp.MustCompile("(?i)" + *matchFlag)
39 }
40 if len(*notMatchFlag) != 0 {
41 notPattern = regexp.MustCompile("(?i)" + *notMatchFlag)
42 }
43 if *showStdout == true {
44 silentOut = stdout
45 }
46 os.Stdout = silentOut
47 }
48
49 func Expectify(suite interface{}, t *testing.T) {
50 var name string
51 var res *result
52 defer func() {
53 if err := recover(); err != nil {
54 if err == endTestErr {
55 finish(t)
56 return
57 }
58 os.Stdout = stdout
59 if res != nil {
60 res.Report()
61 }
62 color.Printf("@R 💣 %-75s\n", name)
63 panic(err)
64 }
65 }()
66
67 tp := reflect.TypeOf(suite)
68 sv := reflect.ValueOf(suite)
69 count := tp.NumMethod()
70
71 runner = &Runner{
72 results: make([]*result, 0, 10),
73 }
74
75 each, _ := tp.MethodByName("Each")
76 if each.Func.IsValid() && each.Type.NumIn() != 2 {
77 each = reflect.Method{}
78 }
79
80 announced := false
81 for i := 0; i < count; i++ {
82 method := tp.Method(i)
83 // this method is not exported
84 if len(method.PkgPath) != 0 {
85 continue
86 }
87 name = method.Name
88 typeName := sv.Elem().Type().String()
89
90 if method.Type.NumIn() != 1 {
91 continue
92 }
93
94 if pattern != nil && !pattern.MatchString(name) && !pattern.MatchString(typeName) {
95 continue
96 }
97 if notPattern != nil && (notPattern.MatchString(name) || notPattern.MatchString(typeName)) {
98 continue
99 }
100
101 os.Stdout = stdout
102 res = runner.Start(name, typeName)
103 var f = func() {
104 method.Func.Call([]reflect.Value{sv})
105 if runner.End() == false || testing.Verbose() {
106 if announced == false {
107 color.Printf("\n@!%s@|\n", typeName)
108 announced = true
109 }
110 res.Report()
111 }
112 }
113 for i := 0; i < len(beforeEach); i++ {
114 beforeEach[i]()
115 }
116 if each.Func.IsValid() {
117 each.Func.Call([]reflect.Value{sv, reflect.ValueOf(f)})
118 } else {
119 f()
120 }
121 os.Stdout = silentOut
122 }
123 finish(t)
124 }
125
126 func finish(t *testing.T) {
127 passed := 0
128 for _, result := range runner.results {
129 if result.Passed() {
130 passed++
131 }
132 }
133 failed := len(runner.results) - passed
134 if failed != 0 {
135 os.Stdout = stdout
136 fmt.Println("\nFailure summary")
137 for _, r := range runner.results {
138 if r.Passed() == false {
139 r.Summary()
140 }
141 }
142 fmt.Println()
143 os.Stdout = silentOut
144 t.Fail()
145 }
146 if path := *summaryPath; len(path) != 0 {
147 updatePersistedSummary(path, passed)
148 }
149 }
150
151 func updatePersistedSummary(path string, passed int) {
152 file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600)
153 if err != nil {
154 panic(err)
155 }
156 file.Write([]byte(strconv.Itoa(passed)))
157 file.Write([]byte{'\n'})
158 file.Close()
159 }
160
161 func BeforeEach(f func()) {
162 beforeEach = append(beforeEach, f)
163 }
164
165 type Runner struct {
166 results []*result
167 current *result
168 }
169
170 func (r *Runner) Start(name string, typeName string) *result {
171 r.current = &result{
172 method: name,
173 typeName: typeName,
174 start: time.Now(),
175 failures: make([]*Failure, 0, 3),
176 }
177 r.results = append(r.results, r.current)
178 return r.current
179 }
180
181 func (r *Runner) End() bool {
182 r.current.end = time.Now()
183 passed := r.current.Passed()
184 r.current = nil
185 return passed
186 }
187
188 func (r *Runner) Skip(format string, args ...interface{}) {
189 if r.current != nil {
190 r.current.Skip(format, args...)
191 }
192 }
193
194 func (r *Runner) Errorf(format string, args ...interface{}) {
195 file := "???"
196 line := 1
197 ok := false
198 for i := 3; i < 10; i++ {
199 _, file, line, ok = runtime.Caller(i)
200 if ok == false || strings.HasSuffix(file, "_test.go") {
201 break
202 }
203 }
204
205 if ok {
206 if index := strings.LastIndex(file, "/"); index >= 0 {
207 file = file[index+1:]
208 } else if index = strings.LastIndex(file, "\\"); index >= 0 {
209 file = file[index+1:]
210 }
211 }
212
213 failure := &Failure{
214 message: fmt.Sprintf(format, args...),
215 location: fmt.Sprintf("%s:%d", file, line),
216 }
217 r.current.failures = append(r.current.failures, failure)
218 }
219
220 func (r *Runner) ErrorMessage(format string, args ...interface{}) {
221 if r.current != nil {
222 r.current.ErrorMessage(format, args...)
223 }
224 }
225
226 type result struct {
227 method string
228 failures []*Failure
229 typeName string
230 start time.Time
231 end time.Time
232 skipMessage string
233 skip bool
234 }
235
236 type Failure struct {
237 message string
238 location string
239 }
240
241 func (r *result) Skip(format string, args ...interface{}) {
242 r.skip = true
243 r.skipMessage = fmt.Sprintf(format, args...)
244 }
245
246 func (r *result) Passed() bool {
247 return r.skip || len(r.failures) == 0
248 }
249
250 func (r *result) ErrorMessage(format string, args ...interface{}) {
251 l := len(r.failures)
252 if l > 0 {
253 r.failures[l-1].message = fmt.Sprintf(format, args...)
254 }
255 }
256
257 func (r *result) Report() {
258 if r.end.IsZero() {
259 r.end = time.Now()
260 }
261 info := fmt.Sprintf(" %-70s%dms", r.method, r.end.Sub(r.start).Nanoseconds()/1000000)
262 if r.skip {
263 color.Println(" @y⸚", info)
264 color.Println(" @." + r.skipMessage)
265 } else if r.Passed() {
266 color.Println(" @g✓", info)
267 } else {
268 color.Println(" @r×", info)
269 for _, failure := range r.failures {
270 color.Printf(" @.%-40s%s\n", failure.location, failure.message)
271 }
272 }
273 }
274
275 func (r *result) Summary() {
276 info := fmt.Sprintf(" %s.%-40s", r.typeName, r.method)
277 if r.skip {
278 color.Println(" @y⸚", info)
279 } else if r.Passed() {
280 color.Println(" @g✓", info)
281 } else {
282 color.Print(" @r×", info)
283 color.Printf("%2d\n", len(r.failures))
284 }
285 }