Escape path separator in regexps (#14122)

* Escape path separator in regexps

* Update platform_helper.dart

* Update stack_trace_test.dart

* Update stack_trace_test.dart
This commit is contained in:
Ian Hickson 2018-01-17 09:09:00 -08:00 committed by GitHub
parent 2908997b0e
commit b0d5d2d928
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 27 deletions

View file

@ -3,11 +3,12 @@
// found in the LICENSE file.
import 'dart:async';
import 'dart:io' show Platform;
import 'package:flutter/foundation.dart';
import 'package:test/test.dart';
import 'platform_helper.dart';
dynamic getAssertionErrorWithMessage() {
try {
assert(false, 'Message goes here.');
@ -44,7 +45,7 @@ Future<Null> main() async {
final StackTrace sampleStack = await getSampleStack();
final String divider = Platform.pathSeparator;
final String dividerRegExp = pathSeparatorForRegExp;
test('Error reporting - pretest', () async {
expect(debugPrint, equals(debugPrintThrottled));
@ -65,11 +66,11 @@ Future<Null> main() async {
information.writeln('line 2 of extra information\n'); // the double trailing newlines here are intentional
},
));
expect(console.join('\n'), matches(new RegExp(
expect(console.join('\n'), matches(
'^══╡ EXCEPTION CAUGHT BY ERROR HANDLING TEST ╞═══════════════════════════════════════════════════════\n'
'The following assertion was thrown testing the error handling logic:\n'
'Message goes here\\.\n'
'\'[^\']+flutter${divider}test${divider}foundation${divider}error_reporting_test\\.dart\': Failed assertion: line [0-9]+ pos [0-9]+: \'false\'\n'
'\'[^\']+flutter${dividerRegExp}test${dividerRegExp}foundation${dividerRegExp}error_reporting_test\\.dart\': Failed assertion: line [0-9]+ pos [0-9]+: \'false\'\n'
'\n'
'Either the assertion indicates an error in the framework itself, or we should provide substantially '
'more information in this error message to help you determine and fix the underlying cause\\.\n'
@ -77,17 +78,17 @@ Future<Null> main() async {
' https://github\\.com/flutter/flutter/issues/new\n'
'\n'
'When the exception was thrown, this was the stack:\n'
'#0 getSampleStack\\.<anonymous closure> \\([^)]+flutter${divider}test${divider}foundation${divider}error_reporting_test\\.dart:[0-9]+:[0-9]+\\)\n'
'#2 getSampleStack \\([^)]+flutter${divider}test${divider}foundation${divider}error_reporting_test\\.dart:[0-9]+:[0-9]+\\)\n'
'#0 getSampleStack\\.<anonymous closure> \\([^)]+flutter${dividerRegExp}test${dividerRegExp}foundation${dividerRegExp}error_reporting_test\\.dart:[0-9]+:[0-9]+\\)\n'
'#2 getSampleStack \\([^)]+flutter${dividerRegExp}test${dividerRegExp}foundation${dividerRegExp}error_reporting_test\\.dart:[0-9]+:[0-9]+\\)\n'
'<asynchronous suspension>\n' // TODO(ianh): https://github.com/flutter/flutter/issues/4021
'#3 main \\([^)]+flutter${divider}test${divider}foundation${divider}error_reporting_test\\.dart:[0-9]+:[0-9]+\\)\n'
'#3 main \\([^)]+flutter${dividerRegExp}test${dividerRegExp}foundation${dividerRegExp}error_reporting_test\\.dart:[0-9]+:[0-9]+\\)\n'
'(.+\n)+' // TODO(ianh): when fixing #4021, also filter out frames from the test infrastructure below the first call to our main()
'\\(elided [0-9]+ frames from package dart:async\\)\n'
'\n'
'line 1 of extra information\n'
'line 2 of extra information\n'
'════════════════════════════════════════════════════════════════════════════════════════════════════\$',
)));
));
console.clear();
FlutterError.dumpErrorToConsole(new FlutterErrorDetails(
exception: getAssertionErrorWithMessage(),
@ -102,7 +103,7 @@ Future<Null> main() async {
FlutterError.dumpErrorToConsole(new FlutterErrorDetails(
exception: getAssertionErrorWithLongMessage(),
));
expect(console.join('\n'), matches(new RegExp(
expect(console.join('\n'), matches(
'^══╡ EXCEPTION CAUGHT BY FLUTTER FRAMEWORK ╞═════════════════════════════════════════════════════════\n'
'The following assertion was thrown:\n'
'word word word word word word word word word word word word word word word word word word word word '
@ -110,14 +111,14 @@ Future<Null> main() async {
'word word word word word word word word word word word word word word word word word word word word '
'word word word word word word word word word word word word word word word word word word word word '
'word word word word word word word word word word word word word word word word word word word word\n'
'\'[^\']+flutter${divider}test${divider}foundation${divider}error_reporting_test\\.dart\': Failed assertion: line [0-9]+ pos [0-9]+: \'false\'\n'
'\'[^\']+flutter${dividerRegExp}test${dividerRegExp}foundation${dividerRegExp}error_reporting_test\\.dart\': Failed assertion: line [0-9]+ pos [0-9]+: \'false\'\n'
'\n'
'Either the assertion indicates an error in the framework itself, or we should provide substantially '
'more information in this error message to help you determine and fix the underlying cause\\.\n'
'In either case, please report this assertion by filing a bug on GitHub:\n'
' https://github\\.com/flutter/flutter/issues/new\n'
'════════════════════════════════════════════════════════════════════════════════════════════════════\$',
)));
));
console.clear();
FlutterError.dumpErrorToConsole(new FlutterErrorDetails(
exception: getAssertionErrorWithLongMessage(),
@ -147,10 +148,10 @@ Future<Null> main() async {
information.writeln('line 2 of extra information\n'); // the double trailing newlines here are intentional
},
));
expect(console.join('\n'), matches(new RegExp(
expect(console.join('\n'), matches(
'^══╡ EXCEPTION CAUGHT BY ERROR HANDLING TEST ╞═══════════════════════════════════════════════════════\n'
'The following assertion was thrown testing the error handling logic:\n'
'\'[^\']+flutter${divider}test${divider}foundation${divider}error_reporting_test\\.dart\': Failed assertion: line [0-9]+ pos [0-9]+: \'false\': is not true\\.\n'
'\'[^\']+flutter${dividerRegExp}test${dividerRegExp}foundation${dividerRegExp}error_reporting_test\\.dart\': Failed assertion: line [0-9]+ pos [0-9]+: \'false\': is not true\\.\n'
'\n'
'Either the assertion indicates an error in the framework itself, or we should provide substantially '
'more information in this error message to help you determine and fix the underlying cause\\.\n'
@ -158,22 +159,22 @@ Future<Null> main() async {
' https://github\\.com/flutter/flutter/issues/new\n'
'\n'
'When the exception was thrown, this was the stack:\n'
'#0 getSampleStack\\.<anonymous closure> \\([^)]+flutter${divider}test${divider}foundation${divider}error_reporting_test\\.dart:[0-9]+:[0-9]+\\)\n'
'#2 getSampleStack \\([^)]+flutter${divider}test${divider}foundation${divider}error_reporting_test\\.dart:[0-9]+:[0-9]+\\)\n'
'#0 getSampleStack\\.<anonymous closure> \\([^)]+flutter${dividerRegExp}test${dividerRegExp}foundation${dividerRegExp}error_reporting_test\\.dart:[0-9]+:[0-9]+\\)\n'
'#2 getSampleStack \\([^)]+flutter${dividerRegExp}test${dividerRegExp}foundation${dividerRegExp}error_reporting_test\\.dart:[0-9]+:[0-9]+\\)\n'
'<asynchronous suspension>\n' // TODO(ianh): https://github.com/flutter/flutter/issues/4021
'#3 main \\([^)]+flutter${divider}test${divider}foundation${divider}error_reporting_test\\.dart:[0-9]+:[0-9]+\\)\n'
'#3 main \\([^)]+flutter${dividerRegExp}test${dividerRegExp}foundation${dividerRegExp}error_reporting_test\\.dart:[0-9]+:[0-9]+\\)\n'
'(.+\n)+' // TODO(ianh): when fixing #4021, also filter out frames from the test infrastructure below the first call to our main()
'\\(elided [0-9]+ frames from package dart:async\\)\n'
'\n'
'line 1 of extra information\n'
'line 2 of extra information\n'
'════════════════════════════════════════════════════════════════════════════════════════════════════\$',
)));
));
console.clear();
FlutterError.dumpErrorToConsole(new FlutterErrorDetails(
exception: getAssertionErrorWithoutMessage(),
));
expect(console.join('\n'), matches('Another exception was thrown: \'[^\']+flutter${divider}test${divider}foundation${divider}error_reporting_test\\.dart\': Failed assertion: line [0-9]+ pos [0-9]+: \'false\': is not true\\.'));
expect(console.join('\n'), matches('Another exception was thrown: \'[^\']+flutter${dividerRegExp}test${dividerRegExp}foundation${dividerRegExp}error_reporting_test\\.dart\': Failed assertion: line [0-9]+ pos [0-9]+: \'false\': is not true\\.'));
console.clear();
FlutterError.resetErrorCount();
});
@ -184,13 +185,13 @@ Future<Null> main() async {
FlutterError.dumpErrorToConsole(new FlutterErrorDetails(
exception: exception,
));
expect(console.join('\n'), matches(new RegExp(
expect(console.join('\n'), matches(
'^══╡ EXCEPTION CAUGHT BY FLUTTER FRAMEWORK ╞═════════════════════════════════════════════════════════\n'
'The following NoSuchMethodError was thrown:\n'
'Receiver: 5\n'
'Tried calling: foo = 2, 4\n'
'════════════════════════════════════════════════════════════════════════════════════════════════════\$',
)));
));
console.clear();
FlutterError.dumpErrorToConsole(new FlutterErrorDetails(
exception: exception,
@ -205,12 +206,12 @@ Future<Null> main() async {
FlutterError.dumpErrorToConsole(const FlutterErrorDetails(
exception: 'hello',
));
expect(console.join('\n'), matches(new RegExp(
expect(console.join('\n'), matches(
'^══╡ EXCEPTION CAUGHT BY FLUTTER FRAMEWORK ╞═════════════════════════════════════════════════════════\n'
'The following message was thrown:\n'
'hello\n'
'════════════════════════════════════════════════════════════════════════════════════════════════════\$',
)));
));
console.clear();
FlutterError.dumpErrorToConsole(const FlutterErrorDetails(
exception: 'hello again',

View file

@ -0,0 +1,18 @@
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:io' show Platform;
/// Returns [Platform.pathSeparator], suitably escaped so as to be usable in a
/// regular expression.
String get pathSeparatorForRegExp {
switch (Platform.pathSeparator) {
case r'/':
return r'/';
case r'\':
return r'\\'; // because dividerRegExp gets inserted into regexps
default:
throw 'Unsupported platform.';
}
}

View file

@ -2,21 +2,21 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:test/test.dart';
import 'platform_helper.dart';
void main() {
// TODO(8128): These tests and the filtering mechanism should be revisited to account for causal async stack traces.
final String divider = Platform.pathSeparator;
final String dividerRegExp = pathSeparatorForRegExp;
test('FlutterError.defaultStackFilter', () {
final List<String> filtered = FlutterError.defaultStackFilter(StackTrace.current.toString().trimRight().split('\n')).toList();
expect(filtered.length, greaterThanOrEqualTo(4));
expect(filtered[0], matches(r'^#0 +main\.<anonymous closure> \(.*stack_trace_test\.dart:[0-9]+:[0-9]+\)$'));
expect(filtered[1], matches(r'^#1 +Declarer\.test\.<anonymous closure>.<anonymous closure> \(package:test' + divider + r'.+:[0-9]+:[0-9]+\)$'));
expect(filtered[1], matches(r'^#1 +Declarer\.test\.<anonymous closure>.<anonymous closure> \(package:test' + dividerRegExp + r'.+:[0-9]+:[0-9]+\)$'));
expect(filtered[2], equals('<asynchronous suspension>'));
expect(filtered.last, matches(r'^\(elided [1-9][0-9]+ frames from package dart:async, package dart:async-patch, and package stack_trace\)$'));
});