Skip to content

Commit

Permalink
[flutter_tools] update test/src to null safety (#106064)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonahwilliams authored Jun 16, 2022
1 parent f104be7 commit 2c15e3c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 34 deletions.
45 changes: 22 additions & 23 deletions packages/flutter_tools/test/src/context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// @dart = 2.8

import 'dart:async';

import 'package:flutter_tools/src/android/android_workflow.dart';
Expand Down Expand Up @@ -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/version.dart';
import 'package:meta/meta.dart';
import 'package:test/fake.dart';

import 'common.dart';
import 'fake_http_client.dart';
Expand All @@ -48,18 +47,18 @@ export 'package:flutter_tools/src/base/context.dart' show Generator;
export 'fake_process_manager.dart' show ProcessManager, FakeProcessManager, FakeCommand;

/// 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
void testUsingContext(
String description,
dynamic Function() testMethod, {
Map<Type, Generator> overrides = const <Type, Generator>{},
bool initializeFlutterRoot = true,
String testOn,
bool skip, // should default to `false`, but https://github.com/dart-lang/test/issues/545 doesn't allow this
String? testOn,
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) {
throw StateError(
Expand All @@ -74,10 +73,10 @@ void testUsingContext(

// Ensure we don't rely on the default [Config] constructor which will
// leak a sticky $HOME/.flutter_settings behind!
Directory configDir;
Directory? configDir;
tearDown(() {
if (configDir != null) {
tryToDelete(configDir);
tryToDelete(configDir!);
configDir = null;
}
});
Expand All @@ -92,7 +91,7 @@ void testUsingContext(
PersistentToolState buildPersistentToolState(FileSystem fs) {
configDir ??= globals.fs.systemTempDirectory.createTempSync('flutter_config_dir_test.');
return PersistentToolState.test(
directory: configDir,
directory: configDir!,
logger: globals.logger,
);
}
Expand Down Expand Up @@ -172,7 +171,7 @@ void testUsingContext(

void _printBufferedErrors(AppContext testContext) {
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) {
// This is where the logger outputting errors is implemented, so it has
// to use `print`.
Expand All @@ -185,18 +184,18 @@ void _printBufferedErrors(AppContext testContext) {
class FakeDeviceManager implements DeviceManager {
List<Device> devices = <Device>[];

String _specifiedDeviceId;
String? _specifiedDeviceId;

@override
String get specifiedDeviceId {
String? get specifiedDeviceId {
if (_specifiedDeviceId == null || _specifiedDeviceId == 'all') {
return null;
}
return _specifiedDeviceId;
}

@override
set specifiedDeviceId(String id) {
set specifiedDeviceId(String? id) {
_specifiedDeviceId = id;
}

Expand All @@ -212,7 +211,7 @@ class FakeDeviceManager implements DeviceManager {
Future<List<Device>> getAllConnectedDevices() async => devices;

@override
Future<List<Device>> refreshAllConnectedDevices({ Duration timeout }) async => devices;
Future<List<Device>> refreshAllConnectedDevices({ Duration? timeout }) async => devices;

@override
Future<List<Device>> getDevicesById(String deviceId) async {
Expand All @@ -222,7 +221,7 @@ class FakeDeviceManager implements DeviceManager {
@override
Future<List<Device>> getDevices() {
return hasSpecifiedDeviceId
? getDevicesById(specifiedDeviceId)
? getDevicesById(specifiedDeviceId!)
: getAllConnectedDevices();
}

Expand All @@ -238,17 +237,17 @@ class FakeDeviceManager implements DeviceManager {
List<DeviceDiscovery> get deviceDiscoverers => <DeviceDiscovery>[];

@override
bool isDeviceSupportedForProject(Device device, FlutterProject flutterProject) {
return device.isSupportedForProject(flutterProject);
bool isDeviceSupportedForProject(Device device, FlutterProject? flutterProject) {
return device.isSupportedForProject(flutterProject!);
}

@override
Future<List<Device>> findTargetDevices(FlutterProject flutterProject, { Duration timeout }) async {
Future<List<Device>> findTargetDevices(FlutterProject? flutterProject, { Duration? timeout }) async {
return devices;
}
}

class FakeAndroidLicenseValidator extends AndroidLicenseValidator {
class FakeAndroidLicenseValidator extends Fake implements AndroidLicenseValidator {
@override
Future<LicensesAccepted> get licensesAccepted async => LicensesAccepted.all;
}
Expand Down Expand Up @@ -302,7 +301,7 @@ class FakeXcodeProjectInterpreter implements XcodeProjectInterpreter {
@override
Future<Map<String, String>> getBuildSettings(
String projectPath, {
XcodeProjectBuildContext buildContext,
XcodeProjectBuildContext? buildContext,
Duration timeout = const Duration(minutes: 1),
}) async {
return <String, String>{};
Expand All @@ -313,14 +312,14 @@ class FakeXcodeProjectInterpreter implements XcodeProjectInterpreter {
Directory podXcodeProject, {
Duration timeout = const Duration(minutes: 1),
}) async {
return null;
return '';
}

@override
Future<void> cleanWorkspace(String workspacePath, String scheme, { bool verbose = false }) async { }

@override
Future<XcodeProjectInfo> getInfo(String projectPath, {String projectFilename}) async {
Future<XcodeProjectInfo> getInfo(String projectPath, {String? projectFilename}) async {
return XcodeProjectInfo(
<String>['Runner'],
<String>['Debug', 'Release'],
Expand Down Expand Up @@ -358,7 +357,7 @@ class LocalFileSystemBlockingSetCurrentDirectory extends LocalFileSystem {
class FakeSignals implements Signals {
@override
Object addHandler(ProcessSignal signal, SignalHandler handler) {
return null;
return Object();
}

@override
Expand Down
20 changes: 9 additions & 11 deletions packages/flutter_tools/test/src/testbed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// @dart = 2.8

import 'dart:async';
import 'dart:io';

Expand Down Expand Up @@ -81,18 +79,18 @@ class Testbed {
/// `overrides` provides more overrides in addition to the test defaults.
/// `setup` may be provided to apply mocks within the tool managed zone,
/// including any specified overrides.
Testbed({FutureOr<void> Function() setup, Map<Type, Generator> overrides})
Testbed({FutureOr<void> Function()? setup, Map<Type, Generator>? overrides})
: _setup = setup,
_overrides = overrides;

final FutureOr<void> Function() _setup;
final Map<Type, Generator> _overrides;
final FutureOr<void> Function()? _setup;
final Map<Type, Generator>? _overrides;

/// Runs `test` within a tool zone.
///
/// `overrides` may be used to provide new context values for the single test
/// 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>{
..._testbedDefaults,
// Add the initial setUp overrides
Expand All @@ -104,13 +102,13 @@ class Testbed {
throw StateError('Do not inject ProcessUtils for testing, use ProcessManager instead.');
}
// 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.
final Map<Timer, StackTrace> timers = <Timer, StackTrace>{};

return HttpOverrides.runZoned(() {
return runInContext<T>(() {
return context.run<T>(
return runInContext<T?>(() {
return context.run<T?>(
name: 'testbed',
overrides: testOverrides,
zoneSpecification: ZoneSpecification(
Expand All @@ -128,7 +126,7 @@ class Testbed {
body: () async {
Cache.flutterRoot = '';
if (_setup != null) {
await _setup();
await _setup?.call();
}
await test();
Cache.flutterRoot = originalFlutterRoot;
Expand All @@ -140,6 +138,6 @@ class Testbed {
return null;
});
});
}, createHttpClient: (SecurityContext c) => FakeHttpClient.any());
}, createHttpClient: (SecurityContext? c) => FakeHttpClient.any());
}
}

0 comments on commit 2c15e3c

Please sign in to comment.