Unverified Commit aa394331 authored by Yegor's avatar Yegor Committed by GitHub

Fix dartDefines in daemon mode (#45317)

* de-null dartDefines in daemon mode
* remove daemonCommand; pipe through dartDefines into Daemon
* pass dartDefiles in attach; add test for --machine mode
parent a60bf8e2
......@@ -198,8 +198,13 @@ class AttachCommand extends FlutterCommand {
final int devicePort = await getDevicePort();
final Daemon daemon = boolArg('machine')
? Daemon(stdinCommandStream, stdoutCommandResponse,
notifyingLogger: NotifyingLogger(), logToStdout: true)
? Daemon(
stdinCommandStream,
stdoutCommandResponse,
notifyingLogger: NotifyingLogger(),
logToStdout: true,
dartDefines: dartDefines,
)
: null;
Stream<Uri> observatoryUri;
......
......@@ -35,7 +35,9 @@ const String protocolVersion = '0.5.3';
/// It can be shutdown with a `daemon.shutdown` command (or by killing the
/// process).
class DaemonCommand extends FlutterCommand {
DaemonCommand({ this.hidden = false });
DaemonCommand({ this.hidden = false }) {
usesDartDefines();
}
@override
final String name = 'daemon';
......@@ -58,8 +60,10 @@ class DaemonCommand extends FlutterCommand {
await context.run<void>(
body: () async {
final Daemon daemon = Daemon(
stdinCommandStream, stdoutCommandResponse,
daemonCommand: this, notifyingLogger: notifyingLogger);
stdinCommandStream, stdoutCommandResponse,
notifyingLogger: notifyingLogger,
dartDefines: dartDefines,
);
final int code = await daemon.onExit;
if (code != 0) {
......@@ -82,10 +86,17 @@ class Daemon {
Daemon(
Stream<Map<String, dynamic>> commandStream,
this.sendCommand, {
this.daemonCommand,
this.notifyingLogger,
this.logToStdout = false,
@required this.dartDefines,
}) {
if (dartDefines == null) {
throw Exception(
'dartDefines must not be null. This is a bug in Flutter.\n'
'Please file an issue at https://github.com/flutter/flutter/issues/new/choose',
);
}
// Set up domains.
_registerDomain(daemonDomain = DaemonDomain(this));
_registerDomain(appDomain = AppDomain(this));
......@@ -110,9 +121,9 @@ class Daemon {
StreamSubscription<Map<String, dynamic>> _commandSubscription;
final DispatchCommand sendCommand;
final DaemonCommand daemonCommand;
final NotifyingLogger notifyingLogger;
final bool logToStdout;
final List<String> dartDefines;
final Completer<int> _onExitCompleter = Completer<int>();
final Map<String, Domain> _domainMap = <String, Domain>{};
......@@ -184,8 +195,6 @@ abstract class Domain {
_handlers[name] = handler;
}
FlutterCommand get command => daemon.daemonCommand;
@override
String toString() => name;
......@@ -424,7 +433,7 @@ class AppDomain extends Domain {
viewFilter: isolateFilter,
target: target,
buildMode: options.buildInfo.mode,
dartDefines: command?.dartDefines,
dartDefines: daemon.dartDefines,
);
ResidentRunner runner;
......@@ -437,7 +446,7 @@ class AppDomain extends Domain {
debuggingOptions: options,
ipv6: ipv6,
stayResident: true,
dartDefines: command?.dartDefines,
dartDefines: daemon.dartDefines,
);
} else if (enableHotReload) {
runner = HotRunner(
......
......@@ -340,8 +340,13 @@ class RunCommand extends RunCommandBase {
if (devices.length > 1) {
throwToolExit('--machine does not support -d all.');
}
final Daemon daemon = Daemon(stdinCommandStream, stdoutCommandResponse,
notifyingLogger: NotifyingLogger(), logToStdout: true);
final Daemon daemon = Daemon(
stdinCommandStream,
stdoutCommandResponse,
notifyingLogger: NotifyingLogger(),
logToStdout: true,
dartDefines: dartDefines,
);
AppInstance app;
try {
final String applicationBinaryPath = stringArg('use-application-binary');
......
......@@ -39,6 +39,7 @@ void main() {
commands.stream,
responses.add,
notifyingLogger: notifyingLogger,
dartDefines: const <String>[],
);
commands.add(<String, dynamic>{'id': 0, 'method': 'daemon.version'});
final Map<String, dynamic> response = await responses.stream.firstWhere(_notEvent);
......@@ -56,6 +57,7 @@ void main() {
commands.stream,
responses.add,
notifyingLogger: notifyingLogger,
dartDefines: const <String>[],
);
printError('daemon.logMessage test');
final Map<String, dynamic> response = await responses.stream.firstWhere((Map<String, dynamic> map) {
......@@ -83,6 +85,7 @@ void main() {
responses.add,
notifyingLogger: notifyingLogger,
logToStdout: true,
dartDefines: const <String>[],
);
printStatus('daemon.logMessage test');
// Service the event loop.
......@@ -103,6 +106,7 @@ void main() {
commands.stream,
responses.add,
notifyingLogger: notifyingLogger,
dartDefines: const <String>[],
);
commands.add(<String, dynamic>{'id': 0, 'method': 'daemon.shutdown'});
return daemon.onExit.then<void>((int code) async {
......@@ -112,16 +116,13 @@ void main() {
});
testUsingContext('app.restart without an appId should report an error', () async {
final DaemonCommand command = DaemonCommand();
applyMocksToCommand(command);
final StreamController<Map<String, dynamic>> commands = StreamController<Map<String, dynamic>>();
final StreamController<Map<String, dynamic>> responses = StreamController<Map<String, dynamic>>();
daemon = Daemon(
commands.stream,
responses.add,
daemonCommand: command,
notifyingLogger: notifyingLogger,
dartDefines: const <String>[],
);
commands.add(<String, dynamic>{'id': 0, 'method': 'app.restart'});
......@@ -133,16 +134,13 @@ void main() {
});
testUsingContext('ext.flutter.debugPaint via service extension without an appId should report an error', () async {
final DaemonCommand command = DaemonCommand();
applyMocksToCommand(command);
final StreamController<Map<String, dynamic>> commands = StreamController<Map<String, dynamic>>();
final StreamController<Map<String, dynamic>> responses = StreamController<Map<String, dynamic>>();
daemon = Daemon(
commands.stream,
responses.add,
daemonCommand: command,
notifyingLogger: notifyingLogger,
dartDefines: const <String>[],
);
commands.add(<String, dynamic>{
......@@ -160,16 +158,13 @@ void main() {
});
testUsingContext('app.stop without appId should report an error', () async {
final DaemonCommand command = DaemonCommand();
applyMocksToCommand(command);
final StreamController<Map<String, dynamic>> commands = StreamController<Map<String, dynamic>>();
final StreamController<Map<String, dynamic>> responses = StreamController<Map<String, dynamic>>();
daemon = Daemon(
commands.stream,
responses.add,
daemonCommand: command,
notifyingLogger: notifyingLogger,
dartDefines: const <String>[],
);
commands.add(<String, dynamic>{'id': 0, 'method': 'app.stop'});
......@@ -187,6 +182,7 @@ void main() {
commands.stream,
responses.add,
notifyingLogger: notifyingLogger,
dartDefines: const <String>[],
);
commands.add(<String, dynamic>{'id': 0, 'method': 'device.getDevices'});
final Map<String, dynamic> response = await responses.stream.firstWhere(_notEvent);
......@@ -203,6 +199,7 @@ void main() {
commands.stream,
responses.add,
notifyingLogger: notifyingLogger,
dartDefines: const <String>[],
);
final MockPollingDeviceDiscovery discoverer = MockPollingDeviceDiscovery();
daemon.deviceDomain.addDeviceDiscoverer(discoverer);
......@@ -221,9 +218,10 @@ void main() {
final StreamController<Map<String, dynamic>> commands = StreamController<Map<String, dynamic>>();
final StreamController<Map<String, dynamic>> responses = StreamController<Map<String, dynamic>>();
daemon = Daemon(
commands.stream,
responses.add,
notifyingLogger: notifyingLogger,
commands.stream,
responses.add,
notifyingLogger: notifyingLogger,
dartDefines: const <String>[],
);
final MockPollingDeviceDiscovery discoverer = MockPollingDeviceDiscovery();
......@@ -247,16 +245,13 @@ void main() {
});
testUsingContext('emulator.launch without an emulatorId should report an error', () async {
final DaemonCommand command = DaemonCommand();
applyMocksToCommand(command);
final StreamController<Map<String, dynamic>> commands = StreamController<Map<String, dynamic>>();
final StreamController<Map<String, dynamic>> responses = StreamController<Map<String, dynamic>>();
daemon = Daemon(
commands.stream,
responses.add,
daemonCommand: command,
notifyingLogger: notifyingLogger,
dartDefines: const <String>[],
);
commands.add(<String, dynamic>{'id': 0, 'method': 'emulator.launch'});
......@@ -274,6 +269,7 @@ void main() {
commands.stream,
responses.add,
notifyingLogger: notifyingLogger,
dartDefines: const <String>[],
);
commands.add(<String, dynamic>{'id': 0, 'method': 'emulator.getEmulators'});
final Map<String, dynamic> response = await responses.stream.firstWhere(_notEvent);
......
......@@ -244,6 +244,37 @@ void main() {
FlutterVersion: () => mockStableFlutterVersion,
WebRunnerFactory: () => mockWebRunnerFactory,
});
testUsingContext('populates dartDefines in --machine mode', () async {
final Directory tempDir = fs.systemTempDirectory.createTempSync('flutter_run_test.');
fs.currentDirectory = tempDir;
final Directory libDir = tempDir.childDirectory('lib');
libDir.createSync();
final File mainFile = libDir.childFile('main.dart');
mainFile.writeAsStringSync('void main() {}');
final Directory webDir = tempDir.childDirectory('web');
webDir.createSync();
final File indexFile = libDir.childFile('index.html');
indexFile.writeAsStringSync('<h1>Hello</h1>');
when(mockDeviceManager.deviceDiscoverers).thenReturn(<DeviceDiscovery>[]);
args.add('--machine');
await createTestCommandRunner(command).run(args);
expect(mockWebRunnerFactory._dartDefines, <String>['FOO=bar']);
}, overrides: <Type, Generator>{
DeviceManager: () => mockDeviceManager,
FeatureFlags: () => TestFeatureFlags(
isWebEnabled: true,
),
FileSystem: () => fs,
ProcessManager: () => mockProcessManager,
DeviceManager: () => mockDeviceManager,
FlutterVersion: () => mockStableFlutterVersion,
WebRunnerFactory: () => mockWebRunnerFactory,
});
});
});
}
......@@ -276,6 +307,9 @@ class FakeDevice extends Fake implements Device {
static const int kFailure = -1;
TargetPlatform _targetPlatform = TargetPlatform.ios;
@override
String get id => 'fake_device';
void _throwToolExit(int code) => throwToolExit(null, exitCode: code);
@override
......@@ -350,6 +384,9 @@ class MockWebRunnerFactory extends Mock implements WebRunnerFactory {
}
class MockWebRunner extends Mock implements ResidentRunner {
@override
bool get debuggingEnabled => false;
@override
Future<int> run({
Completer<DebugConnectionInfo> connectionInfoCompleter,
......@@ -358,4 +395,7 @@ class MockWebRunner extends Mock implements ResidentRunner {
}) async {
return 0;
}
@override
Future<int> waitForAppToFinish() async => 0;
}
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