Migrate device_port_forwarder to null safety (#78949)
This commit is contained in:
parent
3c24f16744
commit
8893e89d11
@ -21,6 +21,7 @@ import '../base/process.dart';
|
||||
import '../build_info.dart';
|
||||
import '../convert.dart';
|
||||
import '../device.dart';
|
||||
import '../device_port_forwader.dart';
|
||||
import '../project.dart';
|
||||
import '../protocol_discovery.dart';
|
||||
|
||||
|
@ -19,6 +19,7 @@ import '../build_info.dart';
|
||||
import '../commands/daemon.dart';
|
||||
import '../compile.dart';
|
||||
import '../device.dart';
|
||||
import '../device_port_forwader.dart';
|
||||
import '../fuchsia/fuchsia_device.dart';
|
||||
import '../globals.dart' as globals;
|
||||
import '../ios/devices.dart';
|
||||
|
@ -20,6 +20,7 @@ import '../base/utils.dart';
|
||||
import '../build_info.dart';
|
||||
import '../convert.dart';
|
||||
import '../device.dart';
|
||||
import '../device_port_forwader.dart';
|
||||
import '../emulator.dart';
|
||||
import '../features.dart';
|
||||
import '../globals.dart' as globals;
|
||||
|
@ -19,6 +19,7 @@ import 'build_info.dart';
|
||||
import 'convert.dart';
|
||||
import 'devfs.dart';
|
||||
import 'device.dart';
|
||||
import 'device_port_forwader.dart';
|
||||
import 'protocol_discovery.dart';
|
||||
|
||||
/// A partial implementation of Device for desktop-class devices to inherit
|
||||
|
@ -20,7 +20,6 @@ import 'base/config.dart';
|
||||
import 'base/context.dart';
|
||||
import 'base/dds.dart';
|
||||
import 'base/file_system.dart';
|
||||
import 'base/io.dart';
|
||||
import 'base/logger.dart';
|
||||
import 'base/os.dart';
|
||||
import 'base/platform.dart';
|
||||
@ -29,6 +28,7 @@ import 'base/user_messages.dart' hide userMessages;
|
||||
import 'base/utils.dart';
|
||||
import 'build_info.dart';
|
||||
import 'devfs.dart';
|
||||
import 'device_port_forwader.dart';
|
||||
import 'features.dart';
|
||||
import 'fuchsia/fuchsia_device.dart';
|
||||
import 'fuchsia/fuchsia_sdk.dart';
|
||||
@ -989,43 +989,6 @@ class LaunchResult {
|
||||
}
|
||||
}
|
||||
|
||||
class ForwardedPort {
|
||||
ForwardedPort(this.hostPort, this.devicePort) : context = null;
|
||||
ForwardedPort.withContext(this.hostPort, this.devicePort, this.context);
|
||||
|
||||
final int hostPort;
|
||||
final int devicePort;
|
||||
final Process context;
|
||||
|
||||
@override
|
||||
String toString() => 'ForwardedPort HOST:$hostPort to DEVICE:$devicePort';
|
||||
|
||||
/// Kill subprocess (if present) used in forwarding.
|
||||
void dispose() {
|
||||
if (context != null) {
|
||||
context.kill();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Forward ports from the host machine to the device.
|
||||
abstract class DevicePortForwarder {
|
||||
/// Returns a Future that completes with the current list of forwarded
|
||||
/// ports for this device.
|
||||
List<ForwardedPort> get forwardedPorts;
|
||||
|
||||
/// Forward [hostPort] on the host to [devicePort] on the device.
|
||||
/// If [hostPort] is null or zero, will auto select a host port.
|
||||
/// Returns a Future that completes with the host port.
|
||||
Future<int> forward(int devicePort, { int hostPort });
|
||||
|
||||
/// Stops forwarding [forwardedPort].
|
||||
Future<void> unforward(ForwardedPort forwardedPort);
|
||||
|
||||
/// Cleanup allocated resources, like [forwardedPorts].
|
||||
Future<void> dispose();
|
||||
}
|
||||
|
||||
/// Read the log for a particular device.
|
||||
abstract class DeviceLogReader {
|
||||
String get name;
|
||||
@ -1074,23 +1037,6 @@ class NoOpDeviceLogReader implements DeviceLogReader {
|
||||
void dispose() { }
|
||||
}
|
||||
|
||||
// A port forwarder which does not support forwarding ports.
|
||||
class NoOpDevicePortForwarder implements DevicePortForwarder {
|
||||
const NoOpDevicePortForwarder();
|
||||
|
||||
@override
|
||||
Future<int> forward(int devicePort, { int hostPort }) async => devicePort;
|
||||
|
||||
@override
|
||||
List<ForwardedPort> get forwardedPorts => <ForwardedPort>[];
|
||||
|
||||
@override
|
||||
Future<void> unforward(ForwardedPort forwardedPort) async { }
|
||||
|
||||
@override
|
||||
Future<void> dispose() async { }
|
||||
}
|
||||
|
||||
/// Append --null_assertions to any existing Dart VM flags if
|
||||
/// [debuggingOptions.nullAssertions] is true.
|
||||
String computeDartVmFlags(DebuggingOptions debuggingOptions) {
|
||||
|
61
packages/flutter_tools/lib/src/device_port_forwader.dart
Normal file
61
packages/flutter_tools/lib/src/device_port_forwader.dart
Normal file
@ -0,0 +1,61 @@
|
||||
// Copyright 2014 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
import 'base/io.dart';
|
||||
|
||||
class ForwardedPort {
|
||||
ForwardedPort(this.hostPort, this.devicePort) : context = null;
|
||||
ForwardedPort.withContext(this.hostPort, this.devicePort, this.context);
|
||||
|
||||
final int hostPort;
|
||||
final int devicePort;
|
||||
final Process? context;
|
||||
|
||||
@override
|
||||
String toString() => 'ForwardedPort HOST:$hostPort to DEVICE:$devicePort';
|
||||
|
||||
/// Kill subprocess (if present) used in forwarding.
|
||||
void dispose() {
|
||||
if (context != null) {
|
||||
context!.kill();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Forward ports from the host machine to the device.
|
||||
abstract class DevicePortForwarder {
|
||||
/// Returns a Future that completes with the current list of forwarded
|
||||
/// ports for this device.
|
||||
List<ForwardedPort> get forwardedPorts;
|
||||
|
||||
/// Forward [hostPort] on the host to [devicePort] on the device.
|
||||
/// If [hostPort] is null or zero, will auto select a host port.
|
||||
/// Returns a Future that completes with the host port.
|
||||
Future<int> forward(int devicePort, { int? hostPort });
|
||||
|
||||
/// Stops forwarding [forwardedPort].
|
||||
Future<void> unforward(ForwardedPort forwardedPort);
|
||||
|
||||
/// Cleanup allocated resources, like [forwardedPorts].
|
||||
Future<void> dispose();
|
||||
}
|
||||
|
||||
// A port forwarder which does not support forwarding ports.
|
||||
class NoOpDevicePortForwarder implements DevicePortForwarder {
|
||||
const NoOpDevicePortForwarder();
|
||||
|
||||
@override
|
||||
Future<int> forward(int devicePort, { int? hostPort }) async => devicePort;
|
||||
|
||||
@override
|
||||
List<ForwardedPort> get forwardedPorts => <ForwardedPort>[];
|
||||
|
||||
@override
|
||||
Future<void> unforward(ForwardedPort forwardedPort) async { }
|
||||
|
||||
@override
|
||||
Future<void> dispose() async { }
|
||||
}
|
@ -21,6 +21,7 @@ import '../base/process.dart';
|
||||
import '../base/time.dart';
|
||||
import '../build_info.dart';
|
||||
import '../device.dart';
|
||||
import '../device_port_forwader.dart';
|
||||
import '../globals.dart' as globals;
|
||||
import '../project.dart';
|
||||
import '../vmservice.dart';
|
||||
|
@ -21,6 +21,7 @@ import '../base/utils.dart';
|
||||
import '../build_info.dart';
|
||||
import '../convert.dart';
|
||||
import '../device.dart';
|
||||
import '../device_port_forwader.dart';
|
||||
import '../globals.dart' as globals;
|
||||
import '../macos/xcode.dart';
|
||||
import '../project.dart';
|
||||
|
@ -21,6 +21,7 @@ import '../build_info.dart';
|
||||
import '../convert.dart';
|
||||
import '../devfs.dart';
|
||||
import '../device.dart';
|
||||
import '../device_port_forwader.dart';
|
||||
import '../globals.dart' as globals;
|
||||
import '../macos/xcode.dart';
|
||||
import '../project.dart';
|
||||
|
@ -11,6 +11,7 @@ import 'package:meta/meta.dart';
|
||||
import 'base/io.dart';
|
||||
import 'base/logger.dart';
|
||||
import 'device.dart';
|
||||
import 'device_port_forwader.dart';
|
||||
import 'globals.dart' as globals;
|
||||
|
||||
/// Discovers a specific service protocol on a device, and forwards the service
|
||||
|
@ -21,6 +21,7 @@ import '../bundle.dart';
|
||||
import '../desktop_device.dart';
|
||||
import '../devfs.dart';
|
||||
import '../device.dart';
|
||||
import '../device_port_forwader.dart';
|
||||
import '../project.dart';
|
||||
import '../protocol_discovery.dart';
|
||||
import '../version.dart';
|
||||
|
@ -16,6 +16,7 @@ import '../base/platform.dart';
|
||||
import '../base/version.dart';
|
||||
import '../build_info.dart';
|
||||
import '../device.dart';
|
||||
import '../device_port_forwader.dart';
|
||||
import '../features.dart';
|
||||
import '../project.dart';
|
||||
import 'chrome.dart';
|
||||
|
@ -17,6 +17,7 @@ import 'package:flutter_tools/src/base/terminal.dart';
|
||||
import 'package:flutter_tools/src/cache.dart';
|
||||
import 'package:flutter_tools/src/commands/attach.dart';
|
||||
import 'package:flutter_tools/src/device.dart';
|
||||
import 'package:flutter_tools/src/device_port_forwader.dart';
|
||||
import 'package:flutter_tools/src/globals.dart' as globals;
|
||||
import 'package:flutter_tools/src/ios/devices.dart';
|
||||
import 'package:flutter_tools/src/project.dart';
|
||||
|
@ -7,7 +7,7 @@
|
||||
import 'package:flutter_tools/src/android/android_device.dart';
|
||||
import 'package:flutter_tools/src/base/io.dart';
|
||||
import 'package:flutter_tools/src/base/logger.dart';
|
||||
import 'package:flutter_tools/src/device.dart';
|
||||
import 'package:flutter_tools/src/device_port_forwader.dart';
|
||||
|
||||
import '../../src/common.dart';
|
||||
import '../../src/context.dart';
|
||||
|
@ -15,6 +15,7 @@ import 'package:flutter_tools/src/build_info.dart';
|
||||
import 'package:flutter_tools/src/desktop_device.dart';
|
||||
import 'package:flutter_tools/src/devfs.dart';
|
||||
import 'package:flutter_tools/src/device.dart';
|
||||
import 'package:flutter_tools/src/device_port_forwader.dart';
|
||||
import 'package:flutter_tools/src/project.dart';
|
||||
|
||||
import 'package:meta/meta.dart';
|
||||
|
@ -0,0 +1,30 @@
|
||||
// Copyright 2014 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
// @dart = 2.8
|
||||
|
||||
import 'package:flutter_tools/src/base/io.dart';
|
||||
import 'package:flutter_tools/src/device_port_forwader.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
|
||||
import '../src/common.dart';
|
||||
import '../src/context.dart';
|
||||
|
||||
void main() {
|
||||
testUsingContext('dispose does not throw exception if no process is present', () {
|
||||
final ForwardedPort forwardedPort = ForwardedPort(123, 456);
|
||||
expect(forwardedPort.context, isNull);
|
||||
forwardedPort.dispose();
|
||||
});
|
||||
|
||||
testUsingContext('dispose kills process if process was available', () {
|
||||
final MockProcess mockProcess = MockProcess();
|
||||
final ForwardedPort forwardedPort = ForwardedPort.withContext(123, 456, mockProcess);
|
||||
forwardedPort.dispose();
|
||||
expect(forwardedPort.context, isNotNull);
|
||||
verify(mockProcess.kill());
|
||||
});
|
||||
}
|
||||
|
||||
class MockProcess extends Mock implements Process {}
|
@ -5,7 +5,6 @@
|
||||
// @dart = 2.8
|
||||
|
||||
import 'package:flutter_tools/src/base/common.dart';
|
||||
import 'package:flutter_tools/src/base/io.dart';
|
||||
import 'package:flutter_tools/src/base/logger.dart';
|
||||
import 'package:flutter_tools/src/base/terminal.dart';
|
||||
import 'package:flutter_tools/src/base/user_messages.dart';
|
||||
@ -481,24 +480,6 @@ void main() {
|
||||
});
|
||||
});
|
||||
|
||||
group('ForwardedPort', () {
|
||||
group('dispose()', () {
|
||||
testUsingContext('does not throw exception if no process is present', () {
|
||||
final ForwardedPort forwardedPort = ForwardedPort(123, 456);
|
||||
expect(forwardedPort.context, isNull);
|
||||
forwardedPort.dispose();
|
||||
});
|
||||
|
||||
testUsingContext('kills process if process was available', () {
|
||||
final MockProcess mockProcess = MockProcess();
|
||||
final ForwardedPort forwardedPort = ForwardedPort.withContext(123, 456, mockProcess);
|
||||
forwardedPort.dispose();
|
||||
expect(forwardedPort.context, isNotNull);
|
||||
verify(mockProcess.kill());
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
group('JSON encode devices', () {
|
||||
testUsingContext('Consistency of JSON representation', () async {
|
||||
expect(
|
||||
@ -555,4 +536,3 @@ class TestDeviceManager extends DeviceManager {
|
||||
class MockTerminal extends Mock implements AnsiTerminal {}
|
||||
class MockDeviceDiscovery extends Mock implements DeviceDiscovery {}
|
||||
class FakeFlutterProject extends Fake implements FlutterProject {}
|
||||
class MockProcess extends Mock implements Process {}
|
||||
|
@ -21,6 +21,7 @@ import 'package:flutter_tools/src/base/time.dart';
|
||||
import 'package:flutter_tools/src/build_info.dart';
|
||||
import 'package:flutter_tools/src/cache.dart';
|
||||
import 'package:flutter_tools/src/device.dart';
|
||||
import 'package:flutter_tools/src/device_port_forwader.dart';
|
||||
import 'package:flutter_tools/src/fuchsia/amber_ctl.dart';
|
||||
import 'package:flutter_tools/src/fuchsia/application_package.dart';
|
||||
import 'package:flutter_tools/src/fuchsia/fuchsia_dev_finder.dart';
|
||||
|
@ -18,6 +18,7 @@ import 'package:flutter_tools/src/base/platform.dart';
|
||||
import 'package:flutter_tools/src/build_info.dart';
|
||||
import 'package:flutter_tools/src/cache.dart';
|
||||
import 'package:flutter_tools/src/device.dart';
|
||||
import 'package:flutter_tools/src/device_port_forwader.dart';
|
||||
import 'package:flutter_tools/src/ios/devices.dart';
|
||||
import 'package:flutter_tools/src/ios/ios_deploy.dart';
|
||||
import 'package:flutter_tools/src/ios/ios_workflow.dart';
|
||||
|
@ -15,6 +15,7 @@ import 'package:flutter_tools/src/base/platform.dart';
|
||||
import 'package:flutter_tools/src/build_info.dart';
|
||||
import 'package:flutter_tools/src/cache.dart';
|
||||
import 'package:flutter_tools/src/device.dart';
|
||||
import 'package:flutter_tools/src/device_port_forwader.dart';
|
||||
import 'package:flutter_tools/src/ios/devices.dart';
|
||||
import 'package:flutter_tools/src/ios/ios_deploy.dart';
|
||||
import 'package:flutter_tools/src/ios/iproxy.dart';
|
||||
|
@ -13,6 +13,7 @@ import 'package:flutter_tools/src/base/platform.dart';
|
||||
import 'package:flutter_tools/src/build_info.dart';
|
||||
import 'package:flutter_tools/src/devfs.dart';
|
||||
import 'package:flutter_tools/src/device.dart';
|
||||
import 'package:flutter_tools/src/device_port_forwader.dart';
|
||||
import 'package:flutter_tools/src/globals.dart' as globals;
|
||||
import 'package:flutter_tools/src/ios/plist_parser.dart';
|
||||
import 'package:flutter_tools/src/ios/simulators.dart';
|
||||
|
@ -7,7 +7,7 @@
|
||||
import 'package:flutter_tools/src/base/io.dart';
|
||||
import 'package:flutter_tools/src/base/logger.dart';
|
||||
import 'package:flutter_tools/src/build_info.dart';
|
||||
import 'package:flutter_tools/src/device.dart';
|
||||
import 'package:flutter_tools/src/device_port_forwader.dart';
|
||||
import 'package:flutter_tools/src/ios/devices.dart';
|
||||
import 'package:flutter_tools/src/mdns_discovery.dart';
|
||||
import 'package:flutter_tools/src/project.dart';
|
||||
|
@ -4,7 +4,7 @@
|
||||
|
||||
// @dart = 2.8
|
||||
|
||||
import 'package:flutter_tools/src/device.dart';
|
||||
import 'package:flutter_tools/src/device_port_forwader.dart';
|
||||
import 'package:flutter_tools/src/protocol_discovery.dart';
|
||||
import 'package:fake_async/fake_async.dart';
|
||||
|
||||
|
@ -9,6 +9,7 @@ import 'dart:async';
|
||||
import 'package:flutter_tools/src/application_package.dart';
|
||||
import 'package:flutter_tools/src/base/dds.dart';
|
||||
import 'package:flutter_tools/src/base/platform.dart';
|
||||
import 'package:flutter_tools/src/device_port_forwader.dart';
|
||||
import 'package:flutter_tools/src/features.dart';
|
||||
import 'package:flutter_tools/src/resident_devtools_handler.dart';
|
||||
import 'package:flutter_tools/src/version.dart';
|
||||
|
Loading…
x
Reference in New Issue
Block a user