2019-11-27 23:04:02 +00:00
|
|
|
// Copyright 2014 The Flutter Authors. All rights reserved.
|
2019-07-31 20:51:19 +00:00
|
|
|
// 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:file/memory.dart';
|
|
|
|
import 'package:flutter_tools/runner.dart' as runner;
|
|
|
|
import 'package:flutter_tools/src/base/file_system.dart';
|
|
|
|
import 'package:flutter_tools/src/base/io.dart' as io;
|
|
|
|
import 'package:flutter_tools/src/base/common.dart';
|
2020-04-28 17:34:03 +00:00
|
|
|
import 'package:flutter_tools/src/base/user_messages.dart';
|
2019-07-31 20:51:19 +00:00
|
|
|
import 'package:flutter_tools/src/cache.dart';
|
2020-03-06 20:14:29 +00:00
|
|
|
import 'package:flutter_tools/src/globals.dart' as globals;
|
2019-07-31 20:51:19 +00:00
|
|
|
import 'package:flutter_tools/src/reporting/reporting.dart';
|
|
|
|
import 'package:flutter_tools/src/runner/flutter_command.dart';
|
2019-11-26 22:06:31 +00:00
|
|
|
import 'package:mockito/mockito.dart';
|
2019-07-31 20:51:19 +00:00
|
|
|
import 'package:platform/platform.dart';
|
|
|
|
|
|
|
|
import '../../src/common.dart';
|
|
|
|
import '../../src/context.dart';
|
|
|
|
|
2020-04-28 17:34:03 +00:00
|
|
|
const String kCustomBugInstructions = 'These are instructions to report with a custom bug tracker.';
|
|
|
|
|
2019-07-31 20:51:19 +00:00
|
|
|
void main() {
|
|
|
|
group('runner', () {
|
|
|
|
setUp(() {
|
|
|
|
// Instead of exiting with dart:io exit(), this causes an exception to
|
|
|
|
// be thrown, which we catch with the onError callback in the zone below.
|
|
|
|
io.setExitFunctionForTests((int _) { throw 'test exit';});
|
|
|
|
Cache.disableLocking();
|
|
|
|
});
|
|
|
|
|
|
|
|
tearDown(() {
|
|
|
|
io.restoreExitFunction();
|
|
|
|
Cache.enableLocking();
|
|
|
|
});
|
|
|
|
|
2019-11-26 22:06:31 +00:00
|
|
|
testUsingContext('error handling crash report', () async {
|
2019-07-31 20:51:19 +00:00
|
|
|
final Completer<void> completer = Completer<void>();
|
|
|
|
// runner.run() asynchronously calls the exit function set above, so we
|
|
|
|
// catch it in a zone.
|
2019-09-24 19:03:37 +00:00
|
|
|
unawaited(runZoned<Future<void>>(
|
|
|
|
() {
|
2019-07-31 20:51:19 +00:00
|
|
|
unawaited(runner.run(
|
|
|
|
<String>['test'],
|
|
|
|
<FlutterCommand>[
|
|
|
|
CrashingFlutterCommand(),
|
|
|
|
],
|
|
|
|
// This flutterVersion disables crash reporting.
|
|
|
|
flutterVersion: '[user-branch]/',
|
|
|
|
reportCrashes: true,
|
|
|
|
));
|
|
|
|
return null;
|
|
|
|
},
|
2020-03-24 15:34:17 +00:00
|
|
|
onError: (Object error, StackTrace stack) { // ignore: deprecated_member_use
|
2019-07-31 20:51:19 +00:00
|
|
|
expect(error, 'test exit');
|
|
|
|
completer.complete();
|
2019-09-24 19:03:37 +00:00
|
|
|
},
|
|
|
|
));
|
2019-07-31 20:51:19 +00:00
|
|
|
await completer.future;
|
|
|
|
|
|
|
|
// This is the main check of this test.
|
|
|
|
//
|
|
|
|
// We are checking that, even though crash reporting failed with an
|
|
|
|
// exception on the first attempt, the second attempt tries to report the
|
|
|
|
// *original* crash, and not the crash from the first crash report
|
|
|
|
// attempt.
|
2020-03-06 20:14:29 +00:00
|
|
|
final CrashingUsage crashingUsage = globals.flutterUsage as CrashingUsage;
|
2019-11-26 22:06:31 +00:00
|
|
|
expect(crashingUsage.sentException, 'an exception % --');
|
2019-07-31 20:51:19 +00:00
|
|
|
}, overrides: <Type, Generator>{
|
|
|
|
Platform: () => FakePlatform(environment: <String, String>{
|
|
|
|
'FLUTTER_ANALYTICS_LOG_FILE': 'test',
|
|
|
|
'FLUTTER_ROOT': '/',
|
|
|
|
}),
|
|
|
|
FileSystem: () => MemoryFileSystem(),
|
2019-10-23 05:05:00 +00:00
|
|
|
ProcessManager: () => FakeProcessManager.any(),
|
2019-07-31 20:51:19 +00:00
|
|
|
Usage: () => CrashingUsage(),
|
|
|
|
});
|
2019-11-26 22:06:31 +00:00
|
|
|
|
2020-04-28 17:34:03 +00:00
|
|
|
testUsingContext('create local report', () async {
|
2019-11-26 22:06:31 +00:00
|
|
|
final Completer<void> completer = Completer<void>();
|
|
|
|
// runner.run() asynchronously calls the exit function set above, so we
|
|
|
|
// catch it in a zone.
|
|
|
|
unawaited(runZoned<Future<void>>(
|
|
|
|
() {
|
|
|
|
unawaited(runner.run(
|
|
|
|
<String>['test'],
|
|
|
|
<FlutterCommand>[
|
|
|
|
CrashingFlutterCommand(),
|
|
|
|
],
|
|
|
|
// This flutterVersion disables crash reporting.
|
|
|
|
flutterVersion: '[user-branch]/',
|
|
|
|
reportCrashes: true,
|
|
|
|
));
|
|
|
|
return null;
|
|
|
|
},
|
2020-03-24 15:34:17 +00:00
|
|
|
onError: (Object error, StackTrace stack) { // ignore: deprecated_member_use
|
2019-11-26 22:06:31 +00:00
|
|
|
expect(error, 'test exit');
|
|
|
|
completer.complete();
|
|
|
|
},
|
|
|
|
));
|
|
|
|
await completer.future;
|
|
|
|
|
|
|
|
final String errorText = testLogger.errorText;
|
|
|
|
expect(errorText, contains('Oops; flutter has exited unexpectedly: "an exception % --".\n'));
|
|
|
|
|
2020-04-28 17:34:03 +00:00
|
|
|
final File log = globals.fs.file('/flutter_01.log');
|
|
|
|
final String logContents = log.readAsStringSync();
|
|
|
|
expect(logContents, contains(kCustomBugInstructions));
|
|
|
|
expect(logContents, contains('flutter test'));
|
|
|
|
expect(logContents, contains('String: an exception % --'));
|
|
|
|
expect(logContents, contains('CrashingFlutterCommand.runCommand'));
|
|
|
|
expect(logContents, contains('[✓] Flutter'));
|
|
|
|
|
|
|
|
final VerificationResult argVerification = verify(globals.crashReporter.informUser(captureAny, any));
|
|
|
|
final CrashDetails sentDetails = argVerification.captured.first as CrashDetails;
|
|
|
|
expect(sentDetails.command, 'flutter test');
|
|
|
|
expect(sentDetails.error, 'an exception % --');
|
|
|
|
expect(sentDetails.stackTrace.toString(), contains('CrashingFlutterCommand.runCommand'));
|
|
|
|
expect(sentDetails.doctorText, contains('[✓] Flutter'));
|
2019-11-26 22:06:31 +00:00
|
|
|
}, overrides: <Type, Generator>{
|
|
|
|
Platform: () => FakePlatform(
|
|
|
|
environment: <String, String>{
|
|
|
|
'FLUTTER_ANALYTICS_LOG_FILE': 'test',
|
|
|
|
'FLUTTER_ROOT': '/',
|
|
|
|
},
|
|
|
|
operatingSystem: 'linux'
|
|
|
|
),
|
|
|
|
FileSystem: () => MemoryFileSystem(),
|
|
|
|
ProcessManager: () => FakeProcessManager.any(),
|
2020-04-28 17:34:03 +00:00
|
|
|
UserMessages: () => CustomBugInstructions(),
|
2019-11-26 22:06:31 +00:00
|
|
|
});
|
2019-07-31 20:51:19 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
class CrashingFlutterCommand extends FlutterCommand {
|
|
|
|
@override
|
|
|
|
String get description => null;
|
|
|
|
|
|
|
|
@override
|
|
|
|
String get name => 'test';
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<FlutterCommandResult> runCommand() async {
|
2019-11-26 22:06:31 +00:00
|
|
|
throw 'an exception % --'; // Test URL encoding.
|
2019-07-31 20:51:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class CrashingUsage implements Usage {
|
2020-02-12 18:58:02 +00:00
|
|
|
CrashingUsage() : _impl = Usage(
|
|
|
|
versionOverride: '[user-branch]',
|
|
|
|
runningOnBot: true,
|
|
|
|
);
|
2019-07-31 20:51:19 +00:00
|
|
|
|
|
|
|
final Usage _impl;
|
|
|
|
|
|
|
|
dynamic get sentException => _sentException;
|
|
|
|
dynamic _sentException;
|
|
|
|
|
|
|
|
bool _firstAttempt = true;
|
|
|
|
|
|
|
|
// Crash while crashing.
|
|
|
|
@override
|
|
|
|
void sendException(dynamic exception) {
|
|
|
|
if (_firstAttempt) {
|
|
|
|
_firstAttempt = false;
|
|
|
|
throw 'sendException';
|
|
|
|
}
|
|
|
|
_sentException = exception;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool get isFirstRun => _impl.isFirstRun;
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool get suppressAnalytics => _impl.suppressAnalytics;
|
|
|
|
|
|
|
|
@override
|
|
|
|
set suppressAnalytics(bool value) {
|
|
|
|
_impl.suppressAnalytics = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool get enabled => _impl.enabled;
|
|
|
|
|
|
|
|
@override
|
|
|
|
set enabled(bool value) {
|
|
|
|
_impl.enabled = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
String get clientId => _impl.clientId;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void sendCommand(String command, {Map<String, String> parameters}) =>
|
|
|
|
_impl.sendCommand(command, parameters: parameters);
|
|
|
|
|
|
|
|
@override
|
2019-09-24 06:06:09 +00:00
|
|
|
void sendEvent(
|
|
|
|
String category,
|
|
|
|
String parameter, {
|
2019-10-03 18:08:42 +00:00
|
|
|
String label,
|
2019-10-07 22:04:55 +00:00
|
|
|
int value,
|
2019-09-24 06:06:09 +00:00
|
|
|
Map<String, String> parameters,
|
2019-10-07 22:04:55 +00:00
|
|
|
}) => _impl.sendEvent(
|
|
|
|
category,
|
|
|
|
parameter,
|
|
|
|
label: label,
|
|
|
|
value: value,
|
|
|
|
parameters: parameters,
|
|
|
|
);
|
2019-07-31 20:51:19 +00:00
|
|
|
|
|
|
|
@override
|
2019-09-24 06:06:09 +00:00
|
|
|
void sendTiming(
|
|
|
|
String category,
|
|
|
|
String variableName,
|
|
|
|
Duration duration, {
|
|
|
|
String label,
|
2019-07-31 20:51:19 +00:00
|
|
|
}) => _impl.sendTiming(category, variableName, duration, label: label);
|
|
|
|
|
|
|
|
@override
|
|
|
|
Stream<Map<String, dynamic>> get onSend => _impl.onSend;
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> ensureAnalyticsSent() => _impl.ensureAnalyticsSent();
|
|
|
|
|
|
|
|
@override
|
|
|
|
void printWelcome() => _impl.printWelcome();
|
|
|
|
}
|
2020-04-28 17:34:03 +00:00
|
|
|
|
|
|
|
class CustomBugInstructions extends UserMessages {
|
|
|
|
@override
|
|
|
|
String get flutterToolBugInstructions => kCustomBugInstructions;
|
|
|
|
}
|