diff --git a/packages/flutter_tools/lib/executable.dart b/packages/flutter_tools/lib/executable.dart index 5655d7b874..f3fa22bf10 100644 --- a/packages/flutter_tools/lib/executable.dart +++ b/packages/flutter_tools/lib/executable.dart @@ -146,8 +146,8 @@ Future run(List args, List subCommands, { await _exit(0); runCompleter.complete(0); }, onError: (dynamic error, Chain chain) { - flutterVersion ??= FlutterVersion.getVersionString(); - _handleToolError(error, chain, verbose, args, reportCrashes, flutterVersion) + String getVersion() => flutterVersion ?? FlutterVersion.getVersionString(); + _handleToolError(error, chain, verbose, args, reportCrashes, getVersion) .then(runCompleter.complete, onError: runCompleter.completeError); }); return runCompleter.future; @@ -160,7 +160,7 @@ Future _handleToolError( bool verbose, List args, bool reportCrashes, - String flutterVersion, + String getFlutterVersion(), ) async { if (error is UsageException) { stderr.writeln(error.message); @@ -208,7 +208,7 @@ Future _handleToolError( await CrashReportSender.instance.sendReport( error: error, stackTrace: chain, - flutterVersion: flutterVersion, + flutterVersion: getFlutterVersion(), ); try { final File file = await _createLocalCrashReport(args, error, chain); diff --git a/packages/flutter_tools/lib/src/base/logger.dart b/packages/flutter_tools/lib/src/base/logger.dart index b5821b3e02..94e7e97ff4 100644 --- a/packages/flutter_tools/lib/src/base/logger.dart +++ b/packages/flutter_tools/lib/src/base/logger.dart @@ -166,6 +166,13 @@ class BufferLogger extends Logger { printStatus(message); return new Status(); } + + /// Clears all buffers. + void clear() { + _error.clear(); + _status.clear(); + _trace.clear(); + } } class VerboseLogger extends Logger { diff --git a/packages/flutter_tools/test/replay/common.dart b/packages/flutter_tools/test/replay/common.dart new file mode 100644 index 0000000000..9ace629136 --- /dev/null +++ b/packages/flutter_tools/test/replay/common.dart @@ -0,0 +1,61 @@ +// Copyright 2015 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:async'; + +import 'package:flutter_tools/executable.dart' as tools; +import 'package:flutter_tools/src/base/io.dart'; +import 'package:test/test.dart'; + +import '../src/common.dart'; +import '../src/context.dart'; + +/// Runs the specified [testMethod] in a minimal `AppContext` that is set up +/// to redirect log output to a `BufferLogger` to avoid spamming `stdout`. +/// +/// Test methods will generally want to use [expectProcessExits] in their method +/// bodies. +void testReplay( + String description, + dynamic testMethod(), { + Timeout timeout, + Map overrides: const {}, + bool skip, +}) { + setUp(() { + setExitFunctionForTests(); + }); + + tearDown(() { + restoreExitFunction(); + }); + + testUsingContext( + description, + testMethod, + timeout: timeout, + overrides: overrides, + skip: skip, + initializeContext: (_) {}, + ); +} + +/// Expects that the specified [command] to Flutter tools exits with the +/// specified [exitCode] (defaults to zero). It is expected that callers will +/// be running in a test via [testReplay]. +/// +/// [command] should be the list of arguments that are passed to the `flutter` +/// command-line tool. For example: +/// +/// ``` +/// [ +/// 'run', +/// '--no-hot', +/// '--no-resident', +/// ] +/// ``` +void expectProcessExits(List command, {dynamic exitCode: 0}) { + final Future mainFuture = tools.main(command); + expect(mainFuture, throwsProcessExit(exitCode)); +} diff --git a/packages/flutter_tools/test/src/context.dart b/packages/flutter_tools/test/src/context.dart index 9223d56b4b..fd3b4001d3 100644 --- a/packages/flutter_tools/test/src/context.dart +++ b/packages/flutter_tools/test/src/context.dart @@ -74,7 +74,7 @@ void testUsingContext(String description, dynamic testMethod(), { final String flutterRoot = getFlutterRoot(); try { - return await testContext.runInZone(() { + return await testContext.runInZone(() async { // Apply the overrides to the test context in the zone since their // instantiation may reference items already stored on the context. overrides.forEach((Type type, dynamic value()) { @@ -83,23 +83,28 @@ void testUsingContext(String description, dynamic testMethod(), { // Provide a sane default for the flutterRoot directory. Individual // tests can override this. Cache.flutterRoot = flutterRoot; - return testMethod(); + return await testMethod(); + }, onError: (dynamic error, StackTrace stackTrace) { + _printBufferedErrors(testContext); + throw error; }); } catch (error) { - if (testContext[Logger] is BufferLogger) { - final BufferLogger bufferLogger = testContext[Logger]; - if (bufferLogger.errorText.isNotEmpty) - print(bufferLogger.errorText); - } - // Previously the following line read "throw error;". This is bad because - // it drops the error's actual stacktrace. Use 'rethrow' to preserve - // the stacktrace. + _printBufferedErrors(testContext); rethrow; } }, timeout: timeout, skip: skip); } +void _printBufferedErrors(AppContext testContext) { + if (testContext[Logger] is BufferLogger) { + final BufferLogger bufferLogger = testContext[Logger]; + if (bufferLogger.errorText.isNotEmpty) + print(bufferLogger.errorText); + bufferLogger.clear(); + } +} + String getFlutterRoot() { Error invalidScript() => new StateError('Invalid script: ${platform.script}');