John McCutchan | 9f60559 | 2015-09-17 14:09:47 -0700 | [diff] [blame] | 1 | // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 | // for details. All rights reserved. Use of this source code is governed by a |
| 3 | // BSD-style license that can be found in the LICENSE file. |
| 4 | |
| 5 | library trace_test; |
| 6 | |
| 7 | import 'package:path/path.dart' as path; |
| 8 | import 'package:stack_trace/stack_trace.dart'; |
| 9 | import 'package:test/test.dart'; |
| 10 | |
| 11 | String getStackTraceString() { |
| 12 | try { |
| 13 | throw ''; |
| 14 | } catch (_, stackTrace) { |
| 15 | return stackTrace.toString(); |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | StackTrace getStackTraceObject() { |
| 20 | try { |
| 21 | throw ''; |
| 22 | } catch (_, stackTrace) { |
| 23 | return stackTrace; |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | Trace getCurrentTrace([int level]) => new Trace.current(level); |
| 28 | |
| 29 | Trace nestedGetCurrentTrace(int level) => getCurrentTrace(level); |
| 30 | |
| 31 | void main() { |
| 32 | // This just shouldn't crash. |
| 33 | test('a native stack trace is parseable', () => new Trace.current()); |
| 34 | |
| 35 | group('.parse', () { |
| 36 | test('.parse parses a VM stack trace correctly', () { |
| 37 | var trace = new Trace.parse( |
| 38 | '#0 Foo._bar (file:///home/nweiz/code/stuff.dart:42:21)\n' |
| 39 | '#1 zip.<anonymous closure>.zap (dart:async/future.dart:0:2)\n' |
| 40 | '#2 zip.<anonymous closure>.zap (http://pub.dartlang.org/thing.' |
| 41 | 'dart:1:100)'); |
| 42 | |
| 43 | expect(trace.frames[0].uri, |
| 44 | equals(Uri.parse("file:///home/nweiz/code/stuff.dart"))); |
| 45 | expect(trace.frames[1].uri, equals(Uri.parse("dart:async/future.dart"))); |
| 46 | expect(trace.frames[2].uri, |
| 47 | equals(Uri.parse("http://pub.dartlang.org/thing.dart"))); |
| 48 | }); |
| 49 | |
| 50 | test('parses a V8 stack trace correctly', () { |
| 51 | var trace = new Trace.parse( |
| 52 | 'Error\n' |
| 53 | ' at Foo._bar (http://pub.dartlang.org/stuff.js:42:21)\n' |
| 54 | ' at http://pub.dartlang.org/stuff.js:0:2\n' |
| 55 | ' at zip.<anonymous>.zap ' |
| 56 | '(http://pub.dartlang.org/thing.js:1:100)'); |
| 57 | |
| 58 | expect(trace.frames[0].uri, |
| 59 | equals(Uri.parse("http://pub.dartlang.org/stuff.js"))); |
| 60 | expect(trace.frames[1].uri, |
| 61 | equals(Uri.parse("http://pub.dartlang.org/stuff.js"))); |
| 62 | expect(trace.frames[2].uri, |
| 63 | equals(Uri.parse("http://pub.dartlang.org/thing.js"))); |
| 64 | |
| 65 | trace = new Trace.parse( |
| 66 | "Exception: foo\n" |
| 67 | ' at Foo._bar (http://pub.dartlang.org/stuff.js:42:21)\n' |
| 68 | ' at http://pub.dartlang.org/stuff.js:0:2\n' |
| 69 | ' at zip.<anonymous>.zap ' |
| 70 | '(http://pub.dartlang.org/thing.js:1:100)'); |
| 71 | |
| 72 | expect(trace.frames[0].uri, |
| 73 | equals(Uri.parse("http://pub.dartlang.org/stuff.js"))); |
| 74 | expect(trace.frames[1].uri, |
| 75 | equals(Uri.parse("http://pub.dartlang.org/stuff.js"))); |
| 76 | expect(trace.frames[2].uri, |
| 77 | equals(Uri.parse("http://pub.dartlang.org/thing.js"))); |
| 78 | |
| 79 | trace = new Trace.parse( |
| 80 | 'Exception: foo\n' |
| 81 | ' bar\n' |
| 82 | ' at Foo._bar (http://pub.dartlang.org/stuff.js:42:21)\n' |
| 83 | ' at http://pub.dartlang.org/stuff.js:0:2\n' |
| 84 | ' at zip.<anonymous>.zap ' |
| 85 | '(http://pub.dartlang.org/thing.js:1:100)'); |
| 86 | |
| 87 | expect(trace.frames[0].uri, |
| 88 | equals(Uri.parse("http://pub.dartlang.org/stuff.js"))); |
| 89 | expect(trace.frames[1].uri, |
| 90 | equals(Uri.parse("http://pub.dartlang.org/stuff.js"))); |
| 91 | expect(trace.frames[2].uri, |
| 92 | equals(Uri.parse("http://pub.dartlang.org/thing.js"))); |
| 93 | }); |
| 94 | |
| 95 | // JavaScriptCore traces are just like V8, except that it doesn't have a |
| 96 | // header and it starts with a tab rather than spaces. |
| 97 | test('parses a JavaScriptCore stack trace correctly', () { |
| 98 | var trace = new Trace.parse( |
| 99 | '\tat Foo._bar (http://pub.dartlang.org/stuff.js:42:21)\n' |
| 100 | '\tat http://pub.dartlang.org/stuff.js:0:2\n' |
| 101 | '\tat zip.<anonymous>.zap ' |
| 102 | '(http://pub.dartlang.org/thing.js:1:100)'); |
| 103 | |
| 104 | expect(trace.frames[0].uri, |
| 105 | equals(Uri.parse("http://pub.dartlang.org/stuff.js"))); |
| 106 | expect(trace.frames[1].uri, |
| 107 | equals(Uri.parse("http://pub.dartlang.org/stuff.js"))); |
| 108 | expect(trace.frames[2].uri, |
| 109 | equals(Uri.parse("http://pub.dartlang.org/thing.js"))); |
| 110 | |
| 111 | trace = new Trace.parse( |
| 112 | '\tat Foo._bar (http://pub.dartlang.org/stuff.js:42:21)\n' |
| 113 | '\tat \n' |
| 114 | '\tat zip.<anonymous>.zap ' |
| 115 | '(http://pub.dartlang.org/thing.js:1:100)'); |
| 116 | |
| 117 | expect(trace.frames[0].uri, |
| 118 | equals(Uri.parse("http://pub.dartlang.org/stuff.js"))); |
| 119 | expect(trace.frames[1].uri, |
| 120 | equals(Uri.parse("http://pub.dartlang.org/thing.js"))); |
| 121 | }); |
| 122 | |
| 123 | test('parses a Firefox/Safari stack trace correctly', () { |
| 124 | var trace = new Trace.parse( |
| 125 | 'Foo._bar@http://pub.dartlang.org/stuff.js:42\n' |
| 126 | 'zip/<@http://pub.dartlang.org/stuff.js:0\n' |
| 127 | 'zip.zap(12, "@)()/<")@http://pub.dartlang.org/thing.js:1'); |
| 128 | |
| 129 | expect(trace.frames[0].uri, |
| 130 | equals(Uri.parse("http://pub.dartlang.org/stuff.js"))); |
| 131 | expect(trace.frames[1].uri, |
| 132 | equals(Uri.parse("http://pub.dartlang.org/stuff.js"))); |
| 133 | expect(trace.frames[2].uri, |
| 134 | equals(Uri.parse("http://pub.dartlang.org/thing.js"))); |
| 135 | |
| 136 | trace = new Trace.parse( |
| 137 | 'zip/<@http://pub.dartlang.org/stuff.js:0\n' |
| 138 | 'Foo._bar@http://pub.dartlang.org/stuff.js:42\n' |
| 139 | 'zip.zap(12, "@)()/<")@http://pub.dartlang.org/thing.js:1'); |
| 140 | |
| 141 | expect(trace.frames[0].uri, |
| 142 | equals(Uri.parse("http://pub.dartlang.org/stuff.js"))); |
| 143 | expect(trace.frames[1].uri, |
| 144 | equals(Uri.parse("http://pub.dartlang.org/stuff.js"))); |
| 145 | expect(trace.frames[2].uri, |
| 146 | equals(Uri.parse("http://pub.dartlang.org/thing.js"))); |
| 147 | |
| 148 | trace = new Trace.parse( |
| 149 | 'zip.zap(12, "@)()/<")@http://pub.dartlang.org/thing.js:1\n' |
| 150 | 'zip/<@http://pub.dartlang.org/stuff.js:0\n' |
| 151 | 'Foo._bar@http://pub.dartlang.org/stuff.js:42'); |
| 152 | |
| 153 | expect(trace.frames[0].uri, |
| 154 | equals(Uri.parse("http://pub.dartlang.org/thing.js"))); |
| 155 | expect(trace.frames[1].uri, |
| 156 | equals(Uri.parse("http://pub.dartlang.org/stuff.js"))); |
| 157 | expect(trace.frames[2].uri, |
| 158 | equals(Uri.parse("http://pub.dartlang.org/stuff.js"))); |
| 159 | }); |
| 160 | |
| 161 | test('parses a Firefox/Safari stack trace containing native code correctly', |
| 162 | () { |
| 163 | var trace = new Trace.parse( |
| 164 | 'Foo._bar@http://pub.dartlang.org/stuff.js:42\n' |
| 165 | 'zip/<@http://pub.dartlang.org/stuff.js:0\n' |
| 166 | 'zip.zap(12, "@)()/<")@http://pub.dartlang.org/thing.js:1\n' |
| 167 | '[native code]'); |
| 168 | |
| 169 | expect(trace.frames[0].uri, |
| 170 | equals(Uri.parse("http://pub.dartlang.org/stuff.js"))); |
| 171 | expect(trace.frames[1].uri, |
| 172 | equals(Uri.parse("http://pub.dartlang.org/stuff.js"))); |
| 173 | expect(trace.frames[2].uri, |
| 174 | equals(Uri.parse("http://pub.dartlang.org/thing.js"))); |
| 175 | expect(trace.frames.length, equals(3)); |
| 176 | }); |
| 177 | |
| 178 | test('parses a Firefox/Safari stack trace without a method name correctly', |
| 179 | () { |
| 180 | var trace = new Trace.parse( |
| 181 | 'http://pub.dartlang.org/stuff.js:42\n' |
| 182 | 'zip/<@http://pub.dartlang.org/stuff.js:0\n' |
| 183 | 'zip.zap(12, "@)()/<")@http://pub.dartlang.org/thing.js:1'); |
| 184 | |
| 185 | expect(trace.frames[0].uri, |
| 186 | equals(Uri.parse("http://pub.dartlang.org/stuff.js"))); |
| 187 | expect(trace.frames[0].member, equals('<fn>')); |
| 188 | expect(trace.frames[1].uri, |
| 189 | equals(Uri.parse("http://pub.dartlang.org/stuff.js"))); |
| 190 | expect(trace.frames[2].uri, |
| 191 | equals(Uri.parse("http://pub.dartlang.org/thing.js"))); |
| 192 | }); |
| 193 | |
| 194 | test('parses a Firefox/Safari stack trace with an empty line correctly', |
| 195 | () { |
| 196 | var trace = new Trace.parse( |
| 197 | 'Foo._bar@http://pub.dartlang.org/stuff.js:42\n' |
| 198 | '\n' |
| 199 | 'zip/<@http://pub.dartlang.org/stuff.js:0\n' |
| 200 | 'zip.zap(12, "@)()/<")@http://pub.dartlang.org/thing.js:1'); |
| 201 | |
| 202 | expect(trace.frames[0].uri, |
| 203 | equals(Uri.parse("http://pub.dartlang.org/stuff.js"))); |
| 204 | expect(trace.frames[1].uri, |
| 205 | equals(Uri.parse("http://pub.dartlang.org/stuff.js"))); |
| 206 | expect(trace.frames[2].uri, |
| 207 | equals(Uri.parse("http://pub.dartlang.org/thing.js"))); |
| 208 | }); |
| 209 | |
| 210 | test('parses a Firefox/Safari stack trace with a column number correctly', |
| 211 | () { |
| 212 | var trace = new Trace.parse( |
| 213 | 'Foo._bar@http://pub.dartlang.org/stuff.js:42:2\n' |
| 214 | 'zip/<@http://pub.dartlang.org/stuff.js:0\n' |
| 215 | 'zip.zap(12, "@)()/<")@http://pub.dartlang.org/thing.js:1'); |
| 216 | |
| 217 | expect(trace.frames[0].uri, |
| 218 | equals(Uri.parse("http://pub.dartlang.org/stuff.js"))); |
| 219 | expect(trace.frames[0].line, equals(42)); |
| 220 | expect(trace.frames[0].column, equals(2)); |
| 221 | expect(trace.frames[1].uri, |
| 222 | equals(Uri.parse("http://pub.dartlang.org/stuff.js"))); |
| 223 | expect(trace.frames[2].uri, |
| 224 | equals(Uri.parse("http://pub.dartlang.org/thing.js"))); |
| 225 | }); |
| 226 | |
| 227 | test('parses a package:stack_trace stack trace correctly', () { |
| 228 | var trace = new Trace.parse( |
| 229 | 'http://dartlang.org/foo/bar.dart 10:11 Foo.<fn>.bar\n' |
| 230 | 'http://dartlang.org/foo/baz.dart Foo.<fn>.bar'); |
| 231 | |
| 232 | expect(trace.frames[0].uri, |
| 233 | equals(Uri.parse("http://dartlang.org/foo/bar.dart"))); |
| 234 | expect(trace.frames[1].uri, |
| 235 | equals(Uri.parse("http://dartlang.org/foo/baz.dart"))); |
| 236 | }); |
| 237 | |
| 238 | test('parses a package:stack_trace stack chain correctly', () { |
| 239 | var trace = new Trace.parse( |
| 240 | 'http://dartlang.org/foo/bar.dart 10:11 Foo.<fn>.bar\n' |
| 241 | 'http://dartlang.org/foo/baz.dart Foo.<fn>.bar\n' |
| 242 | '===== asynchronous gap ===========================\n' |
| 243 | 'http://dartlang.org/foo/bang.dart 10:11 Foo.<fn>.bar\n' |
| 244 | 'http://dartlang.org/foo/quux.dart Foo.<fn>.bar'); |
| 245 | |
| 246 | expect(trace.frames[0].uri, |
| 247 | equals(Uri.parse("http://dartlang.org/foo/bar.dart"))); |
| 248 | expect(trace.frames[1].uri, |
| 249 | equals(Uri.parse("http://dartlang.org/foo/baz.dart"))); |
| 250 | expect(trace.frames[2].uri, |
| 251 | equals(Uri.parse("http://dartlang.org/foo/bang.dart"))); |
| 252 | expect(trace.frames[3].uri, |
| 253 | equals(Uri.parse("http://dartlang.org/foo/quux.dart"))); |
| 254 | }); |
| 255 | |
| 256 | test('parses a real package:stack_trace stack trace correctly', () { |
| 257 | var traceString = new Trace.current().toString(); |
| 258 | expect(new Trace.parse(traceString).toString(), equals(traceString)); |
| 259 | }); |
| 260 | |
| 261 | test('parses an empty string correctly', () { |
| 262 | var trace = new Trace.parse(''); |
| 263 | expect(trace.frames, isEmpty); |
| 264 | expect(trace.toString(), equals('')); |
| 265 | }); |
| 266 | }); |
| 267 | |
| 268 | test('.toString() nicely formats the stack trace', () { |
| 269 | var trace = new Trace.parse(''' |
| 270 | #0 Foo._bar (foo/bar.dart:42:21) |
| 271 | #1 zip.<anonymous closure>.zap (dart:async/future.dart:0:2) |
| 272 | #2 zip.<anonymous closure>.zap (http://pub.dartlang.org/thing.dart:1:100) |
| 273 | '''); |
| 274 | |
| 275 | expect(trace.toString(), equals(''' |
| 276 | ${path.join('foo', 'bar.dart')} 42:21 Foo._bar |
| 277 | dart:async/future.dart 0:2 zip.<fn>.zap |
| 278 | http://pub.dartlang.org/thing.dart 1:100 zip.<fn>.zap |
| 279 | ''')); |
| 280 | }); |
| 281 | |
| 282 | test('.vmTrace returns a native-style trace', () { |
| 283 | var uri = path.toUri(path.absolute('foo')); |
| 284 | var trace = new Trace([ |
| 285 | new Frame(uri, 10, 20, 'Foo.<fn>'), |
| 286 | new Frame(Uri.parse('http://dartlang.org/foo.dart'), null, null, 'bar'), |
| 287 | new Frame(Uri.parse('dart:async'), 15, null, 'baz'), |
| 288 | ]); |
| 289 | |
| 290 | expect(trace.vmTrace.toString(), equals( |
| 291 | '#1 Foo.<anonymous closure> ($uri:10:20)\n' |
| 292 | '#2 bar (http://dartlang.org/foo.dart:0:0)\n' |
| 293 | '#3 baz (dart:async:15:0)\n')); |
| 294 | }); |
| 295 | |
| 296 | group("folding", () { |
| 297 | test('.terse folds core frames together bottom-up', () { |
| 298 | var trace = new Trace.parse(''' |
| 299 | #1 top (dart:async/future.dart:0:2) |
| 300 | #2 bottom (dart:core/uri.dart:1:100) |
| 301 | #0 notCore (foo.dart:42:21) |
| 302 | #3 top (dart:io:5:10) |
| 303 | #4 bottom (dart:async-patch/future.dart:9:11) |
| 304 | #5 alsoNotCore (bar.dart:10:20) |
| 305 | '''); |
| 306 | |
| 307 | expect(trace.terse.toString(), equals(''' |
| 308 | dart:core bottom |
| 309 | foo.dart 42:21 notCore |
| 310 | dart:async bottom |
| 311 | bar.dart 10:20 alsoNotCore |
| 312 | ''')); |
| 313 | }); |
| 314 | |
| 315 | test('.terse folds empty async frames', () { |
| 316 | var trace = new Trace.parse(''' |
| 317 | #0 top (dart:async/future.dart:0:2) |
| 318 | #1 empty.<<anonymous closure>_async_body> (bar.dart) |
| 319 | #2 bottom (dart:async-patch/future.dart:9:11) |
| 320 | #3 notCore (foo.dart:42:21) |
| 321 | '''); |
| 322 | |
| 323 | expect(trace.terse.toString(), equals(''' |
| 324 | dart:async bottom |
| 325 | foo.dart 42:21 notCore |
| 326 | ''')); |
| 327 | }); |
| 328 | |
| 329 | test('.terse removes the bottom-most async frame', () { |
| 330 | var trace = new Trace.parse(''' |
| 331 | #0 notCore (foo.dart:42:21) |
| 332 | #1 top (dart:async/future.dart:0:2) |
| 333 | #2 bottom (dart:core/uri.dart:1:100) |
| 334 | #3 top (dart:io:5:10) |
| 335 | #4 bottom (dart:async-patch/future.dart:9:11) |
| 336 | '''); |
| 337 | |
| 338 | expect(trace.terse.toString(), equals(''' |
| 339 | foo.dart 42:21 notCore |
| 340 | ''')); |
| 341 | }); |
| 342 | |
| 343 | test(".terse won't make a trace empty", () { |
| 344 | var trace = new Trace.parse(''' |
| 345 | #1 top (dart:async/future.dart:0:2) |
| 346 | #2 bottom (dart:core/uri.dart:1:100) |
| 347 | '''); |
| 348 | |
| 349 | expect(trace.terse.toString(), equals(''' |
| 350 | dart:core bottom |
| 351 | ''')); |
| 352 | }); |
| 353 | |
| 354 | test(".terse won't panic on an empty trace", () { |
| 355 | expect(new Trace.parse("").terse.toString(), equals("")); |
| 356 | }); |
| 357 | |
| 358 | test('.foldFrames folds frames together bottom-up', () { |
| 359 | var trace = new Trace.parse(''' |
| 360 | #0 notFoo (foo.dart:42:21) |
| 361 | #1 fooTop (bar.dart:0:2) |
| 362 | #2 fooBottom (foo.dart:1:100) |
| 363 | #3 alsoNotFoo (bar.dart:10:20) |
| 364 | #4 fooTop (dart:io/socket.dart:5:10) |
| 365 | #5 fooBottom (dart:async-patch/future.dart:9:11) |
| 366 | '''); |
| 367 | |
| 368 | var folded = trace.foldFrames((frame) => frame.member.startsWith('foo')); |
| 369 | expect(folded.toString(), equals(''' |
| 370 | foo.dart 42:21 notFoo |
| 371 | foo.dart 1:100 fooBottom |
| 372 | bar.dart 10:20 alsoNotFoo |
| 373 | dart:async-patch/future.dart 9:11 fooBottom |
| 374 | ''')); |
| 375 | }); |
| 376 | |
| 377 | test('.foldFrames with terse: true folds core frames as well', () { |
| 378 | var trace = new Trace.parse(''' |
| 379 | #0 notFoo (foo.dart:42:21) |
| 380 | #1 fooTop (bar.dart:0:2) |
| 381 | #2 coreBottom (dart:async/future.dart:0:2) |
| 382 | #3 alsoNotFoo (bar.dart:10:20) |
| 383 | #4 fooTop (foo.dart:9:11) |
| 384 | #5 coreBottom (dart:async-patch/future.dart:9:11) |
| 385 | '''); |
| 386 | |
| 387 | var folded = trace.foldFrames((frame) => frame.member.startsWith('foo'), |
| 388 | terse: true); |
| 389 | expect(folded.toString(), equals(''' |
| 390 | foo.dart 42:21 notFoo |
| 391 | dart:async coreBottom |
| 392 | bar.dart 10:20 alsoNotFoo |
| 393 | ''')); |
| 394 | }); |
| 395 | |
| 396 | test('.foldFrames with terse: true shortens folded frames', () { |
| 397 | var trace = new Trace.parse(''' |
| 398 | #0 notFoo (foo.dart:42:21) |
| 399 | #1 fooTop (bar.dart:0:2) |
| 400 | #2 fooBottom (package:foo/bar.dart:0:2) |
| 401 | #3 alsoNotFoo (bar.dart:10:20) |
| 402 | #4 fooTop (foo.dart:9:11) |
| 403 | #5 fooBottom (foo/bar.dart:9:11) |
| 404 | '''); |
| 405 | |
| 406 | var folded = trace.foldFrames((frame) => frame.member.startsWith('foo'), |
| 407 | terse: true); |
| 408 | expect(folded.toString(), equals(''' |
| 409 | foo.dart 42:21 notFoo |
| 410 | package:foo fooBottom |
| 411 | bar.dart 10:20 alsoNotFoo |
| 412 | foo fooBottom |
| 413 | ''')); |
| 414 | }); |
| 415 | |
| 416 | test('.foldFrames will never fold unparsed frames', () { |
| 417 | var trace = new Trace.parse(r''' |
| 418 | .g"cs$#:b";a#>sw{*{ul$"$xqwr`p |
| 419 | %+j-?uppx<([j@#nu{{>*+$%x-={`{ |
| 420 | !e($b{nj)zs?cgr%!;bmw.+$j+pfj~ |
| 421 | '''); |
| 422 | |
| 423 | expect(trace.foldFrames((frame) => true).toString(), equals(r''' |
| 424 | .g"cs$#:b";a#>sw{*{ul$"$xqwr`p |
| 425 | %+j-?uppx<([j@#nu{{>*+$%x-={`{ |
| 426 | !e($b{nj)zs?cgr%!;bmw.+$j+pfj~ |
| 427 | ''')); |
| 428 | }); |
| 429 | }); |
| 430 | } |