Unverified Commit b0d5d2d9 authored by Ian Hickson's avatar Ian Hickson Committed by GitHub

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
parent 2908997b
...@@ -3,11 +3,12 @@ ...@@ -3,11 +3,12 @@
// found in the LICENSE file. // found in the LICENSE file.
import 'dart:async'; import 'dart:async';
import 'dart:io' show Platform;
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
import 'platform_helper.dart';
dynamic getAssertionErrorWithMessage() { dynamic getAssertionErrorWithMessage() {
try { try {
assert(false, 'Message goes here.'); assert(false, 'Message goes here.');
...@@ -44,7 +45,7 @@ Future<Null> main() async { ...@@ -44,7 +45,7 @@ Future<Null> main() async {
final StackTrace sampleStack = await getSampleStack(); final StackTrace sampleStack = await getSampleStack();
final String divider = Platform.pathSeparator; final String dividerRegExp = pathSeparatorForRegExp;
test('Error reporting - pretest', () async { test('Error reporting - pretest', () async {
expect(debugPrint, equals(debugPrintThrottled)); expect(debugPrint, equals(debugPrintThrottled));
...@@ -65,11 +66,11 @@ Future<Null> main() async { ...@@ -65,11 +66,11 @@ Future<Null> main() async {
information.writeln('line 2 of extra information\n'); // the double trailing newlines here are intentional 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' '^══╡ EXCEPTION CAUGHT BY ERROR HANDLING TEST ╞═══════════════════════════════════════════════════════\n'
'The following assertion was thrown testing the error handling logic:\n' 'The following assertion was thrown testing the error handling logic:\n'
'Message goes here\\.\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' '\n'
'Either the assertion indicates an error in the framework itself, or we should provide substantially ' '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' 'more information in this error message to help you determine and fix the underlying cause\\.\n'
...@@ -77,17 +78,17 @@ Future<Null> main() async { ...@@ -77,17 +78,17 @@ Future<Null> main() async {
' https://github\\.com/flutter/flutter/issues/new\n' ' https://github\\.com/flutter/flutter/issues/new\n'
'\n' '\n'
'When the exception was thrown, this was the stack:\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' '#0 getSampleStack\\.<anonymous closure> \\([^)]+flutter${dividerRegExp}test${dividerRegExp}foundation${dividerRegExp}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' '#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 '<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() '(.+\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' '\\(elided [0-9]+ frames from package dart:async\\)\n'
'\n' '\n'
'line 1 of extra information\n' 'line 1 of extra information\n'
'line 2 of extra information\n' 'line 2 of extra information\n'
'════════════════════════════════════════════════════════════════════════════════════════════════════\$', '════════════════════════════════════════════════════════════════════════════════════════════════════\$',
))); ));
console.clear(); console.clear();
FlutterError.dumpErrorToConsole(new FlutterErrorDetails( FlutterError.dumpErrorToConsole(new FlutterErrorDetails(
exception: getAssertionErrorWithMessage(), exception: getAssertionErrorWithMessage(),
...@@ -102,7 +103,7 @@ Future<Null> main() async { ...@@ -102,7 +103,7 @@ Future<Null> main() async {
FlutterError.dumpErrorToConsole(new FlutterErrorDetails( FlutterError.dumpErrorToConsole(new FlutterErrorDetails(
exception: getAssertionErrorWithLongMessage(), exception: getAssertionErrorWithLongMessage(),
)); ));
expect(console.join('\n'), matches(new RegExp( expect(console.join('\n'), matches(
'^══╡ EXCEPTION CAUGHT BY FLUTTER FRAMEWORK ╞═════════════════════════════════════════════════════════\n' '^══╡ EXCEPTION CAUGHT BY FLUTTER FRAMEWORK ╞═════════════════════════════════════════════════════════\n'
'The following assertion was thrown:\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 ' '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 { ...@@ -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 ' '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' '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' '\n'
'Either the assertion indicates an error in the framework itself, or we should provide substantially ' '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' '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' 'In either case, please report this assertion by filing a bug on GitHub:\n'
' https://github\\.com/flutter/flutter/issues/new\n' ' https://github\\.com/flutter/flutter/issues/new\n'
'════════════════════════════════════════════════════════════════════════════════════════════════════\$', '════════════════════════════════════════════════════════════════════════════════════════════════════\$',
))); ));
console.clear(); console.clear();
FlutterError.dumpErrorToConsole(new FlutterErrorDetails( FlutterError.dumpErrorToConsole(new FlutterErrorDetails(
exception: getAssertionErrorWithLongMessage(), exception: getAssertionErrorWithLongMessage(),
...@@ -147,10 +148,10 @@ Future<Null> main() async { ...@@ -147,10 +148,10 @@ Future<Null> main() async {
information.writeln('line 2 of extra information\n'); // the double trailing newlines here are intentional 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' '^══╡ EXCEPTION CAUGHT BY ERROR HANDLING TEST ╞═══════════════════════════════════════════════════════\n'
'The following assertion was thrown testing the error handling logic:\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' '\n'
'Either the assertion indicates an error in the framework itself, or we should provide substantially ' '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' 'more information in this error message to help you determine and fix the underlying cause\\.\n'
...@@ -158,22 +159,22 @@ Future<Null> main() async { ...@@ -158,22 +159,22 @@ Future<Null> main() async {
' https://github\\.com/flutter/flutter/issues/new\n' ' https://github\\.com/flutter/flutter/issues/new\n'
'\n' '\n'
'When the exception was thrown, this was the stack:\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' '#0 getSampleStack\\.<anonymous closure> \\([^)]+flutter${dividerRegExp}test${dividerRegExp}foundation${dividerRegExp}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' '#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 '<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() '(.+\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' '\\(elided [0-9]+ frames from package dart:async\\)\n'
'\n' '\n'
'line 1 of extra information\n' 'line 1 of extra information\n'
'line 2 of extra information\n' 'line 2 of extra information\n'
'════════════════════════════════════════════════════════════════════════════════════════════════════\$', '════════════════════════════════════════════════════════════════════════════════════════════════════\$',
))); ));
console.clear(); console.clear();
FlutterError.dumpErrorToConsole(new FlutterErrorDetails( FlutterError.dumpErrorToConsole(new FlutterErrorDetails(
exception: getAssertionErrorWithoutMessage(), 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(); console.clear();
FlutterError.resetErrorCount(); FlutterError.resetErrorCount();
}); });
...@@ -184,13 +185,13 @@ Future<Null> main() async { ...@@ -184,13 +185,13 @@ Future<Null> main() async {
FlutterError.dumpErrorToConsole(new FlutterErrorDetails( FlutterError.dumpErrorToConsole(new FlutterErrorDetails(
exception: exception, exception: exception,
)); ));
expect(console.join('\n'), matches(new RegExp( expect(console.join('\n'), matches(
'^══╡ EXCEPTION CAUGHT BY FLUTTER FRAMEWORK ╞═════════════════════════════════════════════════════════\n' '^══╡ EXCEPTION CAUGHT BY FLUTTER FRAMEWORK ╞═════════════════════════════════════════════════════════\n'
'The following NoSuchMethodError was thrown:\n' 'The following NoSuchMethodError was thrown:\n'
'Receiver: 5\n' 'Receiver: 5\n'
'Tried calling: foo = 2, 4\n' 'Tried calling: foo = 2, 4\n'
'════════════════════════════════════════════════════════════════════════════════════════════════════\$', '════════════════════════════════════════════════════════════════════════════════════════════════════\$',
))); ));
console.clear(); console.clear();
FlutterError.dumpErrorToConsole(new FlutterErrorDetails( FlutterError.dumpErrorToConsole(new FlutterErrorDetails(
exception: exception, exception: exception,
...@@ -205,12 +206,12 @@ Future<Null> main() async { ...@@ -205,12 +206,12 @@ Future<Null> main() async {
FlutterError.dumpErrorToConsole(const FlutterErrorDetails( FlutterError.dumpErrorToConsole(const FlutterErrorDetails(
exception: 'hello', exception: 'hello',
)); ));
expect(console.join('\n'), matches(new RegExp( expect(console.join('\n'), matches(
'^══╡ EXCEPTION CAUGHT BY FLUTTER FRAMEWORK ╞═════════════════════════════════════════════════════════\n' '^══╡ EXCEPTION CAUGHT BY FLUTTER FRAMEWORK ╞═════════════════════════════════════════════════════════\n'
'The following message was thrown:\n' 'The following message was thrown:\n'
'hello\n' 'hello\n'
'════════════════════════════════════════════════════════════════════════════════════════════════════\$', '════════════════════════════════════════════════════════════════════════════════════════════════════\$',
))); ));
console.clear(); console.clear();
FlutterError.dumpErrorToConsole(const FlutterErrorDetails( FlutterError.dumpErrorToConsole(const FlutterErrorDetails(
exception: 'hello again', exception: 'hello again',
......
// 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.';
}
}
...@@ -2,21 +2,21 @@ ...@@ -2,21 +2,21 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
import 'dart:io';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
import 'platform_helper.dart';
void main() { void main() {
// TODO(8128): These tests and the filtering mechanism should be revisited to account for causal async stack traces. // 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', () { test('FlutterError.defaultStackFilter', () {
final List<String> filtered = FlutterError.defaultStackFilter(StackTrace.current.toString().trimRight().split('\n')).toList(); final List<String> filtered = FlutterError.defaultStackFilter(StackTrace.current.toString().trimRight().split('\n')).toList();
expect(filtered.length, greaterThanOrEqualTo(4)); expect(filtered.length, greaterThanOrEqualTo(4));
expect(filtered[0], matches(r'^#0 +main\.<anonymous closure> \(.*stack_trace_test\.dart:[0-9]+:[0-9]+\)$')); 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[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\)$')); expect(filtered.last, matches(r'^\(elided [1-9][0-9]+ frames from package dart:async, package dart:async-patch, and package stack_trace\)$'));
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment