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