Line data Source code
1 : // Copyright (c) 2017, 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 : import 'package:stack_trace/stack_trace.dart';
6 :
7 : import '../backend/invoker.dart';
8 : import '../util/stack_trace_mapper.dart';
9 :
10 : /// Converts [trace] into a Dart stack trace
11 : StackTraceMapper _mapper;
12 :
13 : /// The list of packages to fold when producing [Chain]s.
14 : Set<String> _exceptPackages = new Set.from(['test', 'stream_channel']);
15 :
16 : /// If non-empty, all packages not in this list will be folded when producing
17 : /// [Chain]s.
18 : Set<String> _onlyPackages = new Set();
19 :
20 : /// Configure the resources used for test chaining.
21 : ///
22 : /// [mapper] is used to convert traces into Dart stack traces.
23 : /// [exceptPackages] is the list of packages to fold when producing a [Chain].
24 : /// [onlyPackages] is the list of packages to keep in a [Chain]. If non-empty,
25 : /// all packages not in this will be folded.
26 : void configureTestChaining(
27 : {StackTraceMapper mapper,
28 : Set<String> exceptPackages,
29 : Set<String> onlyPackages}) {
30 : if (mapper != null) _mapper = mapper;
31 : if (exceptPackages != null) _exceptPackages = exceptPackages;
32 : if (onlyPackages != null) _onlyPackages = onlyPackages;
33 : }
34 :
35 : /// Returns [stackTrace] converted to a [Chain] with all irrelevant frames
36 : /// folded together.
37 : ///
38 : /// If [verbose] is `true`, returns the chain for [stackTrace] unmodified.
39 : Chain terseChain(StackTrace stackTrace, {bool verbose: false}) {
40 0 : var testTrace = _mapper?.mapStackTrace(stackTrace) ?? stackTrace;
41 0 : if (verbose) return new Chain.forTrace(testTrace);
42 0 : return new Chain.forTrace(testTrace).foldFrames((frame) {
43 0 : if (_onlyPackages.isNotEmpty) {
44 0 : return !_onlyPackages.contains(frame.package);
45 : }
46 0 : return _exceptPackages.contains(frame.package);
47 : }, terse: true);
48 : }
49 :
50 : /// Converts [stackTrace] to a [Chain] following the test's configuration.
51 0 : Chain testChain(StackTrace stackTrace) => terseChain(stackTrace,
52 0 : verbose: Invoker.current?.liveTest?.test?.metadata?.verboseTrace ?? true);
|