Unverified Commit 2c15e3ca authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

[flutter_tools] update test/src to null safety (#106064)

parent f104be7a
...@@ -2,8 +2,6 @@ ...@@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// @dart = 2.8
import 'dart:async'; import 'dart:async';
import 'package:flutter_tools/src/android/android_workflow.dart'; import 'package:flutter_tools/src/android/android_workflow.dart';
...@@ -36,6 +34,7 @@ import 'package:flutter_tools/src/reporting/crash_reporting.dart'; ...@@ -36,6 +34,7 @@ import 'package:flutter_tools/src/reporting/crash_reporting.dart';
import 'package:flutter_tools/src/reporting/reporting.dart'; import 'package:flutter_tools/src/reporting/reporting.dart';
import 'package:flutter_tools/src/version.dart'; import 'package:flutter_tools/src/version.dart';
import 'package:meta/meta.dart'; import 'package:meta/meta.dart';
import 'package:test/fake.dart';
import 'common.dart'; import 'common.dart';
import 'fake_http_client.dart'; import 'fake_http_client.dart';
...@@ -48,9 +47,9 @@ export 'package:flutter_tools/src/base/context.dart' show Generator; ...@@ -48,9 +47,9 @@ export 'package:flutter_tools/src/base/context.dart' show Generator;
export 'fake_process_manager.dart' show ProcessManager, FakeProcessManager, FakeCommand; export 'fake_process_manager.dart' show ProcessManager, FakeProcessManager, FakeCommand;
/// Return the test logger. This assumes that the current Logger is a BufferLogger. /// Return the test logger. This assumes that the current Logger is a BufferLogger.
BufferLogger get testLogger => context.get<Logger>() as BufferLogger; BufferLogger get testLogger => context.get<Logger>()! as BufferLogger;
FakeDeviceManager get testDeviceManager => context.get<DeviceManager>() as FakeDeviceManager; FakeDeviceManager get testDeviceManager => context.get<DeviceManager>()! as FakeDeviceManager;
@isTest @isTest
void testUsingContext( void testUsingContext(
...@@ -58,8 +57,8 @@ void testUsingContext( ...@@ -58,8 +57,8 @@ void testUsingContext(
dynamic Function() testMethod, { dynamic Function() testMethod, {
Map<Type, Generator> overrides = const <Type, Generator>{}, Map<Type, Generator> overrides = const <Type, Generator>{},
bool initializeFlutterRoot = true, bool initializeFlutterRoot = true,
String testOn, String? testOn,
bool skip, // should default to `false`, but https://github.com/dart-lang/test/issues/545 doesn't allow this bool? skip, // should default to `false`, but https://github.com/dart-lang/test/issues/545 doesn't allow this
}) { }) {
if (overrides[FileSystem] != null && overrides[ProcessManager] == null) { if (overrides[FileSystem] != null && overrides[ProcessManager] == null) {
throw StateError( throw StateError(
...@@ -74,10 +73,10 @@ void testUsingContext( ...@@ -74,10 +73,10 @@ void testUsingContext(
// Ensure we don't rely on the default [Config] constructor which will // Ensure we don't rely on the default [Config] constructor which will
// leak a sticky $HOME/.flutter_settings behind! // leak a sticky $HOME/.flutter_settings behind!
Directory configDir; Directory? configDir;
tearDown(() { tearDown(() {
if (configDir != null) { if (configDir != null) {
tryToDelete(configDir); tryToDelete(configDir!);
configDir = null; configDir = null;
} }
}); });
...@@ -92,7 +91,7 @@ void testUsingContext( ...@@ -92,7 +91,7 @@ void testUsingContext(
PersistentToolState buildPersistentToolState(FileSystem fs) { PersistentToolState buildPersistentToolState(FileSystem fs) {
configDir ??= globals.fs.systemTempDirectory.createTempSync('flutter_config_dir_test.'); configDir ??= globals.fs.systemTempDirectory.createTempSync('flutter_config_dir_test.');
return PersistentToolState.test( return PersistentToolState.test(
directory: configDir, directory: configDir!,
logger: globals.logger, logger: globals.logger,
); );
} }
...@@ -172,7 +171,7 @@ void testUsingContext( ...@@ -172,7 +171,7 @@ void testUsingContext(
void _printBufferedErrors(AppContext testContext) { void _printBufferedErrors(AppContext testContext) {
if (testContext.get<Logger>() is BufferLogger) { if (testContext.get<Logger>() is BufferLogger) {
final BufferLogger bufferLogger = testContext.get<Logger>() as BufferLogger; final BufferLogger bufferLogger = testContext.get<Logger>()! as BufferLogger;
if (bufferLogger.errorText.isNotEmpty) { if (bufferLogger.errorText.isNotEmpty) {
// This is where the logger outputting errors is implemented, so it has // This is where the logger outputting errors is implemented, so it has
// to use `print`. // to use `print`.
...@@ -185,10 +184,10 @@ void _printBufferedErrors(AppContext testContext) { ...@@ -185,10 +184,10 @@ void _printBufferedErrors(AppContext testContext) {
class FakeDeviceManager implements DeviceManager { class FakeDeviceManager implements DeviceManager {
List<Device> devices = <Device>[]; List<Device> devices = <Device>[];
String _specifiedDeviceId; String? _specifiedDeviceId;
@override @override
String get specifiedDeviceId { String? get specifiedDeviceId {
if (_specifiedDeviceId == null || _specifiedDeviceId == 'all') { if (_specifiedDeviceId == null || _specifiedDeviceId == 'all') {
return null; return null;
} }
...@@ -196,7 +195,7 @@ class FakeDeviceManager implements DeviceManager { ...@@ -196,7 +195,7 @@ class FakeDeviceManager implements DeviceManager {
} }
@override @override
set specifiedDeviceId(String id) { set specifiedDeviceId(String? id) {
_specifiedDeviceId = id; _specifiedDeviceId = id;
} }
...@@ -212,7 +211,7 @@ class FakeDeviceManager implements DeviceManager { ...@@ -212,7 +211,7 @@ class FakeDeviceManager implements DeviceManager {
Future<List<Device>> getAllConnectedDevices() async => devices; Future<List<Device>> getAllConnectedDevices() async => devices;
@override @override
Future<List<Device>> refreshAllConnectedDevices({ Duration timeout }) async => devices; Future<List<Device>> refreshAllConnectedDevices({ Duration? timeout }) async => devices;
@override @override
Future<List<Device>> getDevicesById(String deviceId) async { Future<List<Device>> getDevicesById(String deviceId) async {
...@@ -222,7 +221,7 @@ class FakeDeviceManager implements DeviceManager { ...@@ -222,7 +221,7 @@ class FakeDeviceManager implements DeviceManager {
@override @override
Future<List<Device>> getDevices() { Future<List<Device>> getDevices() {
return hasSpecifiedDeviceId return hasSpecifiedDeviceId
? getDevicesById(specifiedDeviceId) ? getDevicesById(specifiedDeviceId!)
: getAllConnectedDevices(); : getAllConnectedDevices();
} }
...@@ -238,17 +237,17 @@ class FakeDeviceManager implements DeviceManager { ...@@ -238,17 +237,17 @@ class FakeDeviceManager implements DeviceManager {
List<DeviceDiscovery> get deviceDiscoverers => <DeviceDiscovery>[]; List<DeviceDiscovery> get deviceDiscoverers => <DeviceDiscovery>[];
@override @override
bool isDeviceSupportedForProject(Device device, FlutterProject flutterProject) { bool isDeviceSupportedForProject(Device device, FlutterProject? flutterProject) {
return device.isSupportedForProject(flutterProject); return device.isSupportedForProject(flutterProject!);
} }
@override @override
Future<List<Device>> findTargetDevices(FlutterProject flutterProject, { Duration timeout }) async { Future<List<Device>> findTargetDevices(FlutterProject? flutterProject, { Duration? timeout }) async {
return devices; return devices;
} }
} }
class FakeAndroidLicenseValidator extends AndroidLicenseValidator { class FakeAndroidLicenseValidator extends Fake implements AndroidLicenseValidator {
@override @override
Future<LicensesAccepted> get licensesAccepted async => LicensesAccepted.all; Future<LicensesAccepted> get licensesAccepted async => LicensesAccepted.all;
} }
...@@ -302,7 +301,7 @@ class FakeXcodeProjectInterpreter implements XcodeProjectInterpreter { ...@@ -302,7 +301,7 @@ class FakeXcodeProjectInterpreter implements XcodeProjectInterpreter {
@override @override
Future<Map<String, String>> getBuildSettings( Future<Map<String, String>> getBuildSettings(
String projectPath, { String projectPath, {
XcodeProjectBuildContext buildContext, XcodeProjectBuildContext? buildContext,
Duration timeout = const Duration(minutes: 1), Duration timeout = const Duration(minutes: 1),
}) async { }) async {
return <String, String>{}; return <String, String>{};
...@@ -313,14 +312,14 @@ class FakeXcodeProjectInterpreter implements XcodeProjectInterpreter { ...@@ -313,14 +312,14 @@ class FakeXcodeProjectInterpreter implements XcodeProjectInterpreter {
Directory podXcodeProject, { Directory podXcodeProject, {
Duration timeout = const Duration(minutes: 1), Duration timeout = const Duration(minutes: 1),
}) async { }) async {
return null; return '';
} }
@override @override
Future<void> cleanWorkspace(String workspacePath, String scheme, { bool verbose = false }) async { } Future<void> cleanWorkspace(String workspacePath, String scheme, { bool verbose = false }) async { }
@override @override
Future<XcodeProjectInfo> getInfo(String projectPath, {String projectFilename}) async { Future<XcodeProjectInfo> getInfo(String projectPath, {String? projectFilename}) async {
return XcodeProjectInfo( return XcodeProjectInfo(
<String>['Runner'], <String>['Runner'],
<String>['Debug', 'Release'], <String>['Debug', 'Release'],
...@@ -358,7 +357,7 @@ class LocalFileSystemBlockingSetCurrentDirectory extends LocalFileSystem { ...@@ -358,7 +357,7 @@ class LocalFileSystemBlockingSetCurrentDirectory extends LocalFileSystem {
class FakeSignals implements Signals { class FakeSignals implements Signals {
@override @override
Object addHandler(ProcessSignal signal, SignalHandler handler) { Object addHandler(ProcessSignal signal, SignalHandler handler) {
return null; return Object();
} }
@override @override
......
...@@ -2,8 +2,6 @@ ...@@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// @dart = 2.8
import 'dart:async'; import 'dart:async';
import 'dart:io'; import 'dart:io';
...@@ -81,18 +79,18 @@ class Testbed { ...@@ -81,18 +79,18 @@ class Testbed {
/// `overrides` provides more overrides in addition to the test defaults. /// `overrides` provides more overrides in addition to the test defaults.
/// `setup` may be provided to apply mocks within the tool managed zone, /// `setup` may be provided to apply mocks within the tool managed zone,
/// including any specified overrides. /// including any specified overrides.
Testbed({FutureOr<void> Function() setup, Map<Type, Generator> overrides}) Testbed({FutureOr<void> Function()? setup, Map<Type, Generator>? overrides})
: _setup = setup, : _setup = setup,
_overrides = overrides; _overrides = overrides;
final FutureOr<void> Function() _setup; final FutureOr<void> Function()? _setup;
final Map<Type, Generator> _overrides; final Map<Type, Generator>? _overrides;
/// Runs `test` within a tool zone. /// Runs `test` within a tool zone.
/// ///
/// `overrides` may be used to provide new context values for the single test /// `overrides` may be used to provide new context values for the single test
/// case or override any context values from the setup. /// case or override any context values from the setup.
Future<T> run<T>(FutureOr<T> Function() test, {Map<Type, Generator> overrides}) { Future<T?> run<T>(FutureOr<T> Function() test, {Map<Type, Generator>? overrides}) {
final Map<Type, Generator> testOverrides = <Type, Generator>{ final Map<Type, Generator> testOverrides = <Type, Generator>{
..._testbedDefaults, ..._testbedDefaults,
// Add the initial setUp overrides // Add the initial setUp overrides
...@@ -104,13 +102,13 @@ class Testbed { ...@@ -104,13 +102,13 @@ class Testbed {
throw StateError('Do not inject ProcessUtils for testing, use ProcessManager instead.'); throw StateError('Do not inject ProcessUtils for testing, use ProcessManager instead.');
} }
// Cache the original flutter root to restore after the test case. // Cache the original flutter root to restore after the test case.
final String originalFlutterRoot = Cache.flutterRoot; final String? originalFlutterRoot = Cache.flutterRoot;
// Track pending timers to verify that they were correctly cleaned up. // Track pending timers to verify that they were correctly cleaned up.
final Map<Timer, StackTrace> timers = <Timer, StackTrace>{}; final Map<Timer, StackTrace> timers = <Timer, StackTrace>{};
return HttpOverrides.runZoned(() { return HttpOverrides.runZoned(() {
return runInContext<T>(() { return runInContext<T?>(() {
return context.run<T>( return context.run<T?>(
name: 'testbed', name: 'testbed',
overrides: testOverrides, overrides: testOverrides,
zoneSpecification: ZoneSpecification( zoneSpecification: ZoneSpecification(
...@@ -128,7 +126,7 @@ class Testbed { ...@@ -128,7 +126,7 @@ class Testbed {
body: () async { body: () async {
Cache.flutterRoot = ''; Cache.flutterRoot = '';
if (_setup != null) { if (_setup != null) {
await _setup(); await _setup?.call();
} }
await test(); await test();
Cache.flutterRoot = originalFlutterRoot; Cache.flutterRoot = originalFlutterRoot;
...@@ -140,6 +138,6 @@ class Testbed { ...@@ -140,6 +138,6 @@ class Testbed {
return null; return null;
}); });
}); });
}, createHttpClient: (SecurityContext c) => FakeHttpClient.any()); }, createHttpClient: (SecurityContext? c) => FakeHttpClient.any());
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment