[flutter_tools] fix null check in crash reporter (#80382)

This commit is contained in:
Jonah Williams
2021-04-13 14:04:25 -07:00
committed by GitHub
parent ce3c3d2a21
commit b7214a9a43
5 changed files with 52 additions and 4 deletions

View File

@@ -28,9 +28,6 @@ class BotDetector {
final PersistentToolState _persistentToolState;
Future<bool> get isRunningOnBot async {
if (_persistentToolState.isRunningOnBot != null) {
return _persistentToolState.isRunningOnBot!;
}
if (
// Explicitly stated to not be a bot.
_platform.environment['BOT'] == 'false'
@@ -43,6 +40,10 @@ class BotDetector {
return _persistentToolState.runningOnBot = false;
}
if (_persistentToolState.isRunningOnBot != null) {
return _persistentToolState.isRunningOnBot!;
}
return _persistentToolState.runningOnBot = _platform.environment['BOT'] == 'true'
// https://docs.travis-ci.com/user/environment-variables/#Default-Environment-Variables

View File

@@ -125,6 +125,12 @@ class UpdatePackagesCommand extends FlutterCommand {
help: 'Use cached packages instead of accessing the network.',
defaultsTo: false,
negatable: false,
)
..addFlag(
'crash',
help: 'For Flutter CLI testing only, forces this command to throw an unhandled exception.',
defaultsTo: false,
negatable: false,
);
}
@@ -179,6 +185,11 @@ class UpdatePackagesCommand extends FlutterCommand {
final bool isVerifyOnly = boolArg('verify-only');
final bool isConsumerOnly = boolArg('consumer-only');
final bool offline = boolArg('offline');
final bool crash = boolArg('crash');
if (crash) {
throw StateError('test crash please ignore.');
}
if (upgrade && offline) {
throwToolExit(

View File

@@ -29,7 +29,7 @@ import 'version.dart';
Cache get cache => context.get<Cache>()!;
Config get config => context.get<Config>()!;
HttpClientFactory get httpClientFactory => context.get<HttpClientFactory>()!;
HttpClientFactory? get httpClientFactory => context.get<HttpClientFactory>();
Logger get logger => context.get<Logger>()!;
OperatingSystemUtils get os => context.get<OperatingSystemUtils>()!;
Signals get signals => context.get<Signals>() ?? LocalSignals.instance;

View File

@@ -45,6 +45,24 @@ void main() {
expect(persistentToolState.isRunningOnBot, isFalse);
});
testWithoutContext('does not cache BOT environment variable', () async {
fakePlatform.environment['BOT'] = 'true';
final BotDetector botDetector = BotDetector(
platform: fakePlatform,
httpClientFactory: () => FakeHttpClient.any(),
persistentToolState: persistentToolState,
);
expect(await botDetector.isRunningOnBot, isTrue);
expect(persistentToolState.isRunningOnBot, isTrue);
fakePlatform.environment['BOT'] = 'false';
expect(await botDetector.isRunningOnBot, isFalse);
expect(persistentToolState.isRunningOnBot, isFalse);
});
testWithoutContext('returns false unconditionally if FLUTTER_HOST is set', () async {
fakePlatform.environment['FLUTTER_HOST'] = 'foo';
fakePlatform.environment['TRAVIS'] = 'true';

View File

@@ -221,4 +221,22 @@ void main() {
expect(result.exitCode, isNot(0));
expect(result.stderr, contains('Could not find an option named "release"'));
});
testWithoutContext('flutter can report crashes', () async {
final String flutterBin = fileSystem.path.join(getFlutterRoot(), 'bin', 'flutter');
final ProcessResult result = await processManager.run(<String>[
flutterBin,
...getLocalEngineArguments(),
'update-packages',
'--crash',
], environment: <String, String>{
'BOT': 'false',
});
expect(result.exitCode, isNot(0));
expect(result.stderr, contains(
'Oops; flutter has exited unexpectedly: "Bad state: test crash please ignore.".\n'
'A crash report has been written to',
));
});
}