From b0d5d2d92830e8ae0838f6d81ec6bc42fa9b038c Mon Sep 17 00:00:00 2001 From: Ian Hickson Date: Wed, 17 Jan 2018 09:09:00 -0800 Subject: [PATCH] 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 --- .../test/foundation/error_reporting_test.dart | 45 ++++++++++--------- .../test/foundation/platform_helper.dart | 18 ++++++++ .../test/foundation/stack_trace_test.dart | 10 ++--- 3 files changed, 46 insertions(+), 27 deletions(-) create mode 100644 packages/flutter/test/foundation/platform_helper.dart diff --git a/packages/flutter/test/foundation/error_reporting_test.dart b/packages/flutter/test/foundation/error_reporting_test.dart index 3bc199cd631..6406738e6ca 100644 --- a/packages/flutter/test/foundation/error_reporting_test.dart +++ b/packages/flutter/test/foundation/error_reporting_test.dart @@ -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 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 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 main() async { ' https://github\\.com/flutter/flutter/issues/new\n' '\n' 'When the exception was thrown, this was the stack:\n' - '#0 getSampleStack\\. \\([^)]+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\\. \\([^)]+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' '\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 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 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 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 main() async { ' https://github\\.com/flutter/flutter/issues/new\n' '\n' 'When the exception was thrown, this was the stack:\n' - '#0 getSampleStack\\. \\([^)]+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\\. \\([^)]+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' '\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 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 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', diff --git a/packages/flutter/test/foundation/platform_helper.dart b/packages/flutter/test/foundation/platform_helper.dart new file mode 100644 index 00000000000..1fc78f59552 --- /dev/null +++ b/packages/flutter/test/foundation/platform_helper.dart @@ -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.'; + } +} diff --git a/packages/flutter/test/foundation/stack_trace_test.dart b/packages/flutter/test/foundation/stack_trace_test.dart index f21c2ce7f81..5e2e4ff96f3 100644 --- a/packages/flutter/test/foundation/stack_trace_test.dart +++ b/packages/flutter/test/foundation/stack_trace_test.dart @@ -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 filtered = FlutterError.defaultStackFilter(StackTrace.current.toString().trimRight().split('\n')).toList(); expect(filtered.length, greaterThanOrEqualTo(4)); expect(filtered[0], matches(r'^#0 +main\. \(.*stack_trace_test\.dart:[0-9]+:[0-9]+\)$')); - expect(filtered[1], matches(r'^#1 +Declarer\.test\.. \(package:test' + divider + r'.+:[0-9]+:[0-9]+\)$')); + expect(filtered[1], matches(r'^#1 +Declarer\.test\.. \(package:test' + dividerRegExp + r'.+:[0-9]+:[0-9]+\)$')); expect(filtered[2], equals('')); expect(filtered.last, matches(r'^\(elided [1-9][0-9]+ frames from package dart:async, package dart:async-patch, and package stack_trace\)$')); });