Unverified Commit 1c1ef133 authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

[flutter_tools] remove globals from device manager, flutter tester (#61139)

Remove global variables from device manager, flutter tester devices and update tests
parent fbca40ad
......@@ -4,7 +4,9 @@
import 'package:meta/meta.dart';
import 'base/config.dart';
import 'base/context.dart';
import 'base/file_system.dart';
import 'base/logger.dart';
import 'base/utils.dart';
import 'build_system/targets/icon_tree_shaker.dart';
......@@ -612,15 +614,20 @@ HostPlatform getCurrentHostPlatform() {
}
/// Returns the top-level build output directory.
String getBuildDirectory() {
String getBuildDirectory([Config config, FileSystem fileSystem]) {
// TODO(johnmccutchan): Stop calling this function as part of setting
// up command line argument processing.
if (context == null || globals.config == null) {
if (context == null) {
return 'build';
}
final Config localConfig = config ?? globals.config;
final FileSystem localFilesystem = fileSystem ?? globals.fs;
if (localConfig == null) {
return 'build';
}
final String buildDir = globals.config.getValue('build-dir') as String ?? 'build';
if (globals.fs.path.isAbsolute(buildDir)) {
final String buildDir = localConfig.getValue('build-dir') as String ?? 'build';
if (localFilesystem.path.isAbsolute(buildDir)) {
throw Exception(
'build-dir config setting in ${globals.config.configPath} must be relative');
}
......
......@@ -30,7 +30,7 @@ import 'emulator.dart';
import 'features.dart';
import 'fuchsia/fuchsia_device.dart' show FuchsiaDeviceTools;
import 'fuchsia/fuchsia_sdk.dart' show FuchsiaSdk, FuchsiaArtifacts;
import 'fuchsia/fuchsia_workflow.dart' show FuchsiaWorkflow;
import 'fuchsia/fuchsia_workflow.dart' show FuchsiaWorkflow, fuchsiaWorkflow;
import 'globals.dart' as globals;
import 'ios/ios_workflow.dart';
import 'ios/simulators.dart';
......@@ -124,7 +124,22 @@ Future<T> runInContext<T>(
client: globals.httpClientFactory?.call() ?? HttpClient(),
),
DevFSConfig: () => DevFSConfig(),
DeviceManager: () => FlutterDeviceManager(),
DeviceManager: () => FlutterDeviceManager(
logger: globals.logger,
processManager: globals.processManager,
platform: globals.platform,
androidSdk: globals.androidSdk,
iosSimulatorUtils: globals.iosSimulatorUtils,
featureFlags: featureFlags,
fileSystem: globals.fs,
iosWorkflow: globals.iosWorkflow,
artifacts: globals.artifacts,
flutterVersion: globals.flutterVersion,
androidWorkflow: androidWorkflow,
config: globals.config,
fuchsiaWorkflow: fuchsiaWorkflow,
xcDevice: globals.xcdevice,
),
Doctor: () => Doctor(logger: globals.logger),
DoctorValidatorsProvider: () => DoctorValidatorsProvider.defaultInstance,
EmulatorManager: () => EmulatorManager(
......
......@@ -6,15 +6,20 @@ import 'dart:async';
import 'dart:math' as math;
import 'package:meta/meta.dart';
import 'package:process/process.dart';
import 'package:vm_service/vm_service.dart' as vm_service;
import 'android/android_device_discovery.dart';
import 'android/android_sdk.dart';
import 'android/android_workflow.dart';
import 'application_package.dart';
import 'artifacts.dart';
import 'base/config.dart';
import 'base/context.dart';
import 'base/file_system.dart';
import 'base/io.dart';
import 'base/logger.dart';
import 'base/platform.dart';
import 'base/user_messages.dart';
import 'base/utils.dart';
import 'build_info.dart';
......@@ -24,11 +29,14 @@ import 'fuchsia/fuchsia_sdk.dart';
import 'fuchsia/fuchsia_workflow.dart';
import 'globals.dart' as globals;
import 'ios/devices.dart';
import 'ios/ios_workflow.dart';
import 'ios/simulators.dart';
import 'linux/linux_device.dart';
import 'macos/macos_device.dart';
import 'macos/xcode.dart';
import 'project.dart';
import 'tester/flutter_tester.dart';
import 'version.dart';
import 'web/web_device.dart';
import 'windows/windows_device.dart';
......@@ -269,42 +277,68 @@ abstract class DeviceManager {
}
class FlutterDeviceManager extends DeviceManager {
@override
final List<DeviceDiscovery> deviceDiscoverers = <DeviceDiscovery>[
FlutterDeviceManager({
@required Logger logger,
@required Platform platform,
@required ProcessManager processManager,
@required FileSystem fileSystem,
@required AndroidSdk androidSdk,
@required FeatureFlags featureFlags,
@required IOSSimulatorUtils iosSimulatorUtils,
@required XCDevice xcDevice,
@required AndroidWorkflow androidWorkflow,
@required IOSWorkflow iosWorkflow,
@required FuchsiaWorkflow fuchsiaWorkflow,
@required FlutterVersion flutterVersion,
@required Config config,
@required Artifacts artifacts,
}) : deviceDiscoverers = <DeviceDiscovery>[
AndroidDevices(
logger: globals.logger,
androidSdk: globals.androidSdk,
logger: logger,
androidSdk: androidSdk,
androidWorkflow: androidWorkflow,
processManager: globals.processManager,
processManager: processManager,
),
IOSDevices(
platform: globals.platform,
xcdevice: globals.xcdevice,
iosWorkflow: globals.iosWorkflow,
logger: globals.logger,
platform: platform,
xcdevice: xcDevice,
iosWorkflow: iosWorkflow,
logger: logger,
),
IOSSimulators(
iosSimulatorUtils: iosSimulatorUtils,
),
IOSSimulators(iosSimulatorUtils: globals.iosSimulatorUtils),
FuchsiaDevices(
fuchsiaSdk: fuchsiaSdk,
logger: globals.logger,
logger: logger,
fuchsiaWorkflow: fuchsiaWorkflow,
platform: globals.platform,
platform: platform,
),
FlutterTesterDevices(
fileSystem: fileSystem,
flutterVersion: flutterVersion,
processManager: processManager,
config: config,
logger: logger,
artifacts: artifacts,
),
FlutterTesterDevices(),
MacOSDevices(),
LinuxDevices(
platform: globals.platform,
platform: platform,
featureFlags: featureFlags,
),
WindowsDevices(),
WebDevices(
featureFlags: featureFlags,
fileSystem: globals.fs,
platform: globals.platform,
processManager: globals.processManager,
logger: globals.logger,
fileSystem: fileSystem,
platform: platform,
processManager: processManager,
logger: logger,
),
];
@override
final List<DeviceDiscovery> deviceDiscoverers;
}
/// An abstract class to discover and enumerate a specific type of devices.
......
......@@ -9,6 +9,7 @@ import 'package:process/process.dart';
import '../application_package.dart';
import '../artifacts.dart';
import '../base/config.dart';
import '../base/file_system.dart';
import '../base/io.dart';
import '../base/logger.dart';
......@@ -47,18 +48,18 @@ class FlutterTesterApp extends ApplicationPackage {
// TODO(scheglov): This device does not currently work with full restarts.
class FlutterTesterDevice extends Device {
FlutterTesterDevice(String deviceId, {
@required ProcessManager processManager,
@required FlutterVersion flutterVersion,
@required Logger logger,
@required String buildDirectory,
@required FileSystem fileSystem,
@required Artifacts artifacts,
}) : _processManager = processManager,
_flutterVersion = flutterVersion,
_logger = logger,
_buildDirectory = buildDirectory,
_fileSystem = fileSystem,
_artifacts = artifacts,
ProcessManager processManager,
FlutterVersion flutterVersion,
Logger logger,
String buildDirectory,
FileSystem fileSystem,
Artifacts artifacts,
}) : _processManager = processManager ?? globals.processManager, // TODO(jonahwilliams): remove after google3 roll.
_flutterVersion = flutterVersion ?? globals.flutterVersion,
_logger = logger ?? globals.logger,
_buildDirectory = buildDirectory ?? getBuildDirectory(),
_fileSystem = fileSystem ?? globals.fs,
_artifacts = artifacts ?? globals.artifacts,
super(
deviceId,
platformType: null,
......@@ -253,21 +254,29 @@ class FlutterTesterDevice extends Device {
}
class FlutterTesterDevices extends PollingDeviceDiscovery {
FlutterTesterDevices() : super('Flutter tester');
FlutterTesterDevices({
FileSystem fileSystem,
Artifacts artifacts,
ProcessManager processManager,
Logger logger,
FlutterVersion flutterVersion,
Config config,
}) : _testerDevice = FlutterTesterDevice( // TODO(jonahwilliams): remove after google3 roll.
kTesterDeviceId,
fileSystem: fileSystem ?? globals.fs,
artifacts: artifacts ?? globals.artifacts,
processManager: processManager ?? globals.processManager,
buildDirectory: getBuildDirectory(config ?? globals.config, fileSystem ?? globals.fs),
logger: logger ?? globals.logger,
flutterVersion: flutterVersion ?? globals.flutterVersion,
),
super('Flutter tester');
static const String kTesterDeviceId = 'flutter-tester';
static bool showFlutterTesterDevice = false;
final FlutterTesterDevice _testerDevice = FlutterTesterDevice(
kTesterDeviceId,
fileSystem: globals.fs,
artifacts: globals.artifacts,
processManager: globals.processManager,
buildDirectory: getBuildDirectory(),
logger: globals.logger,
flutterVersion: globals.flutterVersion,
);
final FlutterTesterDevice _testerDevice;
@override
bool get canListAnything => true;
......
......@@ -7,12 +7,14 @@ import 'dart:async';
import 'package:file/file.dart';
import 'package:file/memory.dart';
import 'package:flutter_tools/src/artifacts.dart';
import 'package:flutter_tools/src/base/config.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/base/platform.dart';
import 'package:flutter_tools/src/build_info.dart';
import 'package:flutter_tools/src/build_system/build_system.dart';
import 'package:flutter_tools/src/device.dart';
import 'package:flutter_tools/src/tester/flutter_tester.dart';
import 'package:flutter_tools/src/version.dart';
import 'package:mockito/mockito.dart';
import 'package:process/process.dart';
......@@ -43,16 +45,16 @@ void main() {
FlutterTesterDevices.showFlutterTesterDevice = false;
});
testUsingContext('no device', () async {
final FlutterTesterDevices discoverer = FlutterTesterDevices();
testWithoutContext('no device', () async {
final FlutterTesterDevices discoverer = setUpFlutterTesterDevices();
final List<Device> devices = await discoverer.devices;
expect(devices, isEmpty);
});
testUsingContext('has device', () async {
testWithoutContext('has device', () async {
FlutterTesterDevices.showFlutterTesterDevice = true;
final FlutterTesterDevices discoverer = FlutterTesterDevices();
final FlutterTesterDevices discoverer = setUpFlutterTesterDevices();
final List<Device> devices = await discoverer.devices;
expect(devices, hasLength(1));
......@@ -62,14 +64,15 @@ void main() {
expect(device.id, 'flutter-tester');
});
testUsingContext('discoverDevices', () async {
testWithoutContext('discoverDevices', () async {
FlutterTesterDevices.showFlutterTesterDevice = true;
final FlutterTesterDevices discoverer = FlutterTesterDevices();
final FlutterTesterDevices discoverer = setUpFlutterTesterDevices();
// Timeout ignored.
final List<Device> devices = await discoverer.discoverDevices(timeout: const Duration(seconds: 10));
expect(devices, hasLength(1));
});
});
group('startApp', () {
FlutterTesterDevice device;
......@@ -170,4 +173,22 @@ Hello!
});
}
FlutterTesterDevices setUpFlutterTesterDevices() {
final FileSystem fileSystem = MemoryFileSystem.test();
final Logger logger = BufferLogger.test();
return FlutterTesterDevices(
logger: logger,
artifacts: Artifacts.test(),
processManager: FakeProcessManager.any(),
fileSystem: MemoryFileSystem.test(),
config: Config.test(
'test',
directory: fileSystem.currentDirectory,
logger: logger,
),
flutterVersion: MockFlutterVersion(),
);
}
class MockBuildSystem extends Mock implements BuildSystem {}
class MockFlutterVersion extends Mock implements FlutterVersion {}
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