diff --git a/packages/flutter_tools/lib/src/base/io.dart b/packages/flutter_tools/lib/src/base/io.dart index d9d8c14d08..42eb915055 100644 --- a/packages/flutter_tools/lib/src/base/io.dart +++ b/packages/flutter_tools/lib/src/base/io.dart @@ -26,7 +26,7 @@ /// increase the API surface that we have to test in Flutter tools, and the APIs /// in `dart:io` can sometimes be hard to use in tests. import 'dart:async'; -import 'dart:io' as io show exit, IOSink, Process, ProcessInfo, ProcessSignal, +import 'dart:io' as io show exit, IOSink, Platform, Process, ProcessInfo, ProcessSignal, stderr, stdin, Stdin, StdinException, Stdout, stdout; import 'package:meta/meta.dart'; @@ -92,12 +92,26 @@ ExitFunction _exitFunction = _defaultExitFunction; /// Exits the process. /// +/// Throws [StateError] if assertions are enabled and the dart:io exit +/// is still active when called. This may indicate exit was called in +/// a test without being configured correctly. This behavior can be +/// removed by setting the `FLUTTER_TEST` environment +/// variable to a non-null string. +/// /// This is analogous to the `exit` function in `dart:io`, except that this /// function may be set to a testing-friendly value by calling /// [setExitFunctionForTests] (and then restored to its default implementation /// with [restoreExitFunction]). The default implementation delegates to /// `dart:io`. -ExitFunction get exit => _exitFunction; +ExitFunction get exit { + assert( + _exitFunction != io.exit || io.Platform.environment['FLUTTER_TEST'] != null, + 'io.exit was called with assertions active. If this is an integration test, ' + 'ensure that the environment variable FLUTTER_TEST is set ' + 'to a non-null String.', + ); + return _exitFunction; +} /// Sets the [exit] function to a function that throws an exception rather /// than exiting the process; this is intended for testing purposes. diff --git a/packages/flutter_tools/test/general.shard/base/io_test.dart b/packages/flutter_tools/test/general.shard/base/io_test.dart index c8493281f5..0d6d7d1979 100644 --- a/packages/flutter_tools/test/general.shard/base/io_test.dart +++ b/packages/flutter_tools/test/general.shard/base/io_test.dart @@ -68,6 +68,16 @@ void main() { testUsingContext('ProcessSignal toString() works', () async { expect(io.ProcessSignal.sigint.toString(), ProcessSignal.SIGINT.toString()); }); + + test('exit throws a StateError if called without being overriden', () { + expect(() => exit(0), throwsA(isInstanceOf())); + }); + + test('exit does not throw a StateError if overriden', () { + setExitFunctionForTests((int value) {}); + + expect(() => exit(0), returnsNormally); + }); } class MockIoProcessSignal extends Mock implements io.ProcessSignal {} diff --git a/packages/flutter_tools/test/integration.shard/daemon_mode_test.dart b/packages/flutter_tools/test/integration.shard/daemon_mode_test.dart index 571da8a354..aab518f619 100644 --- a/packages/flutter_tools/test/integration.shard/daemon_mode_test.dart +++ b/packages/flutter_tools/test/integration.shard/daemon_mode_test.dart @@ -29,6 +29,9 @@ void main() { final Process process = await processManager.start( [flutterBin, '--show-test-device', 'daemon'], workingDirectory: tempDir.path, + environment: { + 'FLUTTER_TEST': 'true', + } ); final StreamController stdout = StreamController.broadcast(); diff --git a/packages/flutter_tools/test/integration.shard/test_utils.dart b/packages/flutter_tools/test/integration.shard/test_utils.dart index 1833a2dc5c..08563a984b 100644 --- a/packages/flutter_tools/test/integration.shard/test_utils.dart +++ b/packages/flutter_tools/test/integration.shard/test_utils.dart @@ -46,7 +46,12 @@ Future getPackages(String folder) async { 'pub', 'get', ]; - final ProcessResult result = await processManager.run(command, workingDirectory: folder); + final ProcessResult result = await processManager.run(command, + workingDirectory: folder, + environment: { + 'FLUTTER_TEST': 'true' + }, + ); if (result.exitCode != 0) { throw Exception('flutter pub get failed: ${result.stderr}\n${result.stdout}'); }