From 0d9a1b201e5871e63d126e2ed0d69ce6c315658c Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Wed, 19 Jun 2019 13:39:27 -0700 Subject: [PATCH] Remove environment variable guards for command line desktop and web (#33867) --- .../lib/src/commands/daemon.dart | 5 +++++ packages/flutter_tools/lib/src/desktop.dart | 22 ++++++++++++++----- .../lib/src/linux/linux_workflow.dart | 4 ++-- .../lib/src/macos/macos_workflow.dart | 4 ++-- .../flutter_tools/lib/src/web/web_device.dart | 2 +- .../flutter_tools/lib/src/web/workflow.dart | 20 +++++++++++++---- .../lib/src/windows/windows_workflow.dart | 4 ++-- .../test/commands/devices_test.dart | 3 +++ .../test/linux/linux_device_test.dart | 1 - packages/flutter_tools/test/src/common.dart | 15 +++++++++++++ .../flutter_tools/test/web/workflow_test.dart | 14 +----------- 11 files changed, 64 insertions(+), 30 deletions(-) diff --git a/packages/flutter_tools/lib/src/commands/daemon.dart b/packages/flutter_tools/lib/src/commands/daemon.dart index 9fc2d83588..259cf7c4a1 100644 --- a/packages/flutter_tools/lib/src/commands/daemon.dart +++ b/packages/flutter_tools/lib/src/commands/daemon.dart @@ -28,6 +28,10 @@ import '../vmservice.dart'; const String protocolVersion = '0.5.2'; +/// Whether the tool started from the daemon, as opposed to the command line. +bool get isRunningFromDaemon => _isRunningFromDaemon; +bool _isRunningFromDaemon = false; + /// A server process command. This command will start up a long-lived server. /// It reads JSON-RPC based commands from stdin, executes them, and returns /// JSON-RPC based responses and events to stdout. @@ -49,6 +53,7 @@ class DaemonCommand extends FlutterCommand { @override Future runCommand() async { printStatus('Starting device daemon...'); + _isRunningFromDaemon = true; final NotifyingLogger notifyingLogger = NotifyingLogger(); diff --git a/packages/flutter_tools/lib/src/desktop.dart b/packages/flutter_tools/lib/src/desktop.dart index 287f33aaf0..37930afd3f 100644 --- a/packages/flutter_tools/lib/src/desktop.dart +++ b/packages/flutter_tools/lib/src/desktop.dart @@ -4,20 +4,32 @@ import 'dart:async'; +import 'package:meta/meta.dart'; + import 'base/io.dart'; import 'base/platform.dart'; import 'base/process_manager.dart'; +import 'commands/daemon.dart' show isRunningFromDaemon; import 'convert.dart'; import 'device.dart'; import 'version.dart'; -// Only launch or display desktop embedding devices if -// `ENABLE_FLUTTER_DESKTOP` environment variable is set to true. +@visibleForTesting +bool debugDisableDesktop = false; + +/// Only launch or display desktop embedding devices from the command line +/// or if `ENABLE_FLUTTER_DESKTOP` environment variable is set to true. bool get flutterDesktopEnabled { - _flutterDesktopEnabled ??= platform.environment['ENABLE_FLUTTER_DESKTOP']?.toLowerCase() == 'true'; - return _flutterDesktopEnabled && !FlutterVersion.instance.isStable; + if (debugDisableDesktop) { + return false; + } + if (isRunningFromDaemon) { + final bool platformEnabled = platform + .environment['ENABLE_FLUTTER_DESKTOP']?.toLowerCase() == 'true'; + return platformEnabled && !FlutterVersion.instance.isStable; + } + return !FlutterVersion.instance.isStable; } -bool _flutterDesktopEnabled; /// Kills a process on linux or macOS. Future killProcess(String executable) async { diff --git a/packages/flutter_tools/lib/src/linux/linux_workflow.dart b/packages/flutter_tools/lib/src/linux/linux_workflow.dart index bf7a04dccd..4959f72209 100644 --- a/packages/flutter_tools/lib/src/linux/linux_workflow.dart +++ b/packages/flutter_tools/lib/src/linux/linux_workflow.dart @@ -21,10 +21,10 @@ class LinuxWorkflow implements Workflow { bool get appliesToHostPlatform => platform.isLinux; @override - bool get canLaunchDevices => flutterDesktopEnabled; + bool get canLaunchDevices => platform.isLinux && flutterDesktopEnabled; @override - bool get canListDevices => flutterDesktopEnabled; + bool get canListDevices => platform.isLinux && flutterDesktopEnabled; @override bool get canListEmulators => false; diff --git a/packages/flutter_tools/lib/src/macos/macos_workflow.dart b/packages/flutter_tools/lib/src/macos/macos_workflow.dart index bf5900b850..e85e3d4054 100644 --- a/packages/flutter_tools/lib/src/macos/macos_workflow.dart +++ b/packages/flutter_tools/lib/src/macos/macos_workflow.dart @@ -21,10 +21,10 @@ class MacOSWorkflow implements Workflow { bool get appliesToHostPlatform => platform.isMacOS; @override - bool get canLaunchDevices => flutterDesktopEnabled; + bool get canLaunchDevices => platform.isMacOS && flutterDesktopEnabled; @override - bool get canListDevices => flutterDesktopEnabled; + bool get canListDevices => platform.isMacOS && flutterDesktopEnabled; @override bool get canListEmulators => false; diff --git a/packages/flutter_tools/lib/src/web/web_device.dart b/packages/flutter_tools/lib/src/web/web_device.dart index a98e91f0fd..d8b22328cf 100644 --- a/packages/flutter_tools/lib/src/web/web_device.dart +++ b/packages/flutter_tools/lib/src/web/web_device.dart @@ -12,8 +12,8 @@ import '../base/process_manager.dart'; import '../build_info.dart'; import '../device.dart'; import '../project.dart'; -import '../web/workflow.dart'; import 'chrome.dart'; +import 'workflow.dart'; class WebApplicationPackage extends ApplicationPackage { WebApplicationPackage(this.flutterProject) : super(id: flutterProject.manifest.appName); diff --git a/packages/flutter_tools/lib/src/web/workflow.dart b/packages/flutter_tools/lib/src/web/workflow.dart index cfa0a6d963..5ecfd1e6a1 100644 --- a/packages/flutter_tools/lib/src/web/workflow.dart +++ b/packages/flutter_tools/lib/src/web/workflow.dart @@ -2,20 +2,32 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +import 'package:meta/meta.dart'; + import '../base/context.dart'; import '../base/platform.dart'; import '../base/process_manager.dart'; +import '../commands/daemon.dart' show isRunningFromDaemon; import '../doctor.dart'; import '../version.dart'; import 'chrome.dart'; +@visibleForTesting +bool debugDisableWeb = false; + /// Only launch or display web devices if `FLUTTER_WEB` -/// environment variable is set to true. +/// environment variable is set to true from the daemon. bool get flutterWebEnabled { - _flutterWebEnabled = platform.environment['FLUTTER_WEB']?.toLowerCase() == 'true'; - return _flutterWebEnabled && !FlutterVersion.instance.isStable; + if (debugDisableWeb) { + return false; + } + if (isRunningFromDaemon) { + final bool platformEnabled = platform + .environment['FLUTTER_WEB']?.toLowerCase() == 'true'; + return platformEnabled && !FlutterVersion.instance.isStable; + } + return !FlutterVersion.instance.isStable; } -bool _flutterWebEnabled; /// The web workflow instance. WebWorkflow get webWorkflow => context.get(); diff --git a/packages/flutter_tools/lib/src/windows/windows_workflow.dart b/packages/flutter_tools/lib/src/windows/windows_workflow.dart index 5dae4269be..8d8e34e0fe 100644 --- a/packages/flutter_tools/lib/src/windows/windows_workflow.dart +++ b/packages/flutter_tools/lib/src/windows/windows_workflow.dart @@ -21,10 +21,10 @@ class WindowsWorkflow implements Workflow { bool get appliesToHostPlatform => platform.isWindows; @override - bool get canLaunchDevices => flutterDesktopEnabled; + bool get canLaunchDevices => platform.isWindows && flutterDesktopEnabled; @override - bool get canListDevices => flutterDesktopEnabled; + bool get canListDevices => platform.isWindows && flutterDesktopEnabled; @override bool get canListEmulators => false; diff --git a/packages/flutter_tools/test/commands/devices_test.dart b/packages/flutter_tools/test/commands/devices_test.dart index a63f19bdaf..86618a4d38 100644 --- a/packages/flutter_tools/test/commands/devices_test.dart +++ b/packages/flutter_tools/test/commands/devices_test.dart @@ -20,6 +20,9 @@ void main() { group('devices', () { setUpAll(() { Cache.disableLocking(); + // TODO(jonahwilliams): adjust the individual tests so they do not + // depend on the host environment. + debugDisableWebAndDesktop = true; }); testUsingContext('returns 0 when called', () async { diff --git a/packages/flutter_tools/test/linux/linux_device_test.dart b/packages/flutter_tools/test/linux/linux_device_test.dart index 97e7425600..323fa7ceaa 100644 --- a/packages/flutter_tools/test/linux/linux_device_test.dart +++ b/packages/flutter_tools/test/linux/linux_device_test.dart @@ -24,7 +24,6 @@ void main() { final MockProcessManager mockProcessManager = MockProcessManager(); when(notLinux.isLinux).thenReturn(false); - when(notLinux.environment).thenReturn(const {}); when(mockProcessManager.run([ 'ps', 'aux', ])).thenAnswer((Invocation invocation) async { diff --git a/packages/flutter_tools/test/src/common.dart b/packages/flutter_tools/test/src/common.dart index 44f65560f1..dea43fde5f 100644 --- a/packages/flutter_tools/test/src/common.dart +++ b/packages/flutter_tools/test/src/common.dart @@ -5,6 +5,8 @@ import 'dart:async'; import 'package:args/command_runner.dart'; +import 'package:flutter_tools/src/desktop.dart'; +import 'package:flutter_tools/src/web/workflow.dart'; import 'package:test_api/test_api.dart' hide TypeMatcher, isInstanceOf; import 'package:test_api/test_api.dart' as test_package show TypeMatcher; @@ -18,6 +20,19 @@ import 'package:flutter_tools/src/runner/flutter_command_runner.dart'; export 'package:test_api/test_api.dart' hide TypeMatcher, isInstanceOf; // Defines a 'package:test' shim. +/// Disable both web and desktop to make testing easier. For example, prevent +/// them from showing up in the devices list if the host happens to be setup +/// properly. +set debugDisableWebAndDesktop(bool value) { + if (value) { + debugDisableDesktop = true; + debugDisableWeb = true; + } else { + debugDisableDesktop = false; + debugDisableWeb = false; + } +} + /// A matcher that compares the type of the actual value to the type argument T. // TODO(ianh): Remove this once https://github.com/dart-lang/matcher/issues/98 is fixed Matcher isInstanceOf() => test_package.TypeMatcher(); diff --git a/packages/flutter_tools/test/web/workflow_test.dart b/packages/flutter_tools/test/web/workflow_test.dart index 31726c798a..6551104e47 100644 --- a/packages/flutter_tools/test/web/workflow_test.dart +++ b/packages/flutter_tools/test/web/workflow_test.dart @@ -17,7 +17,6 @@ import '../src/testbed.dart'; void main() { group('WebWorkflow', () { Testbed testbed; - MockPlatform noEnvironment; MockPlatform notSupported; MockPlatform windows; MockPlatform linux; @@ -30,7 +29,6 @@ void main() { setUpAll(() { unstable = MockFlutterVersion(false); stable = MockFlutterVersion(true); - noEnvironment = MockPlatform(environment: const {}); notSupported = MockPlatform(linux: false, windows: false, macos: false); windows = MockPlatform(windows: true); linux = MockPlatform(linux: true); @@ -46,15 +44,6 @@ void main() { }); }); - test('does not apply if FLUTTER_WEB is not true', ()=> testbed.run(() { - expect(workflow.appliesToHostPlatform, false); - expect(workflow.canLaunchDevices, false); - expect(workflow.canListDevices, false); - expect(workflow.canListEmulators, false); - }, overrides: { - Platform: () => noEnvironment, - })); - test('Applies on Linux', () => testbed.run(() { expect(workflow.appliesToHostPlatform, true); expect(workflow.canLaunchDevices, true); @@ -92,7 +81,7 @@ void main() { Platform: () => notSupported, })); - test('does not apply on stable brnach', () => testbed.run(() { + test('does not apply on stable branch', () => testbed.run(() { expect(workflow.appliesToHostPlatform, false); expect(workflow.canLaunchDevices, false); expect(workflow.canListDevices, false); @@ -119,7 +108,6 @@ class MockPlatform extends Mock implements Platform { this.macos = false, this.linux = false, this.environment = const { - 'FLUTTER_WEB': 'true', kChromeEnvironment: 'chrome', }});