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