Add --machine support for flutter attach (#19077)

* Extract some of startApp into a reusable method

* Get basic attach --machine working

* Attach --machine tweaks

Move validation to validate method and create daemon early so we get the startup event before trying to get a connection.

* Bump daemon version so we know whether it's valid to flutter attach

* Tweak output text

* Swap package imports for relative

* Review tweaks (naming, formatting, typedefs)

* Separate arguments from process spawning

This will make calling attach easier

* Add a basic test for flutter attach --machine

* Fix crash if port unforward modifies the list of forwarded ports

* Add a no-op port forwarder for flutter-tester

* Switch to using BasicProject instead of our own inline code

* Fix expectation in test now we have a portForwarder

* Remove stale TODO (this is done)

* Tweak formatting

* Change some Completers to void to fix Dart 2 issues
This commit is contained in:
Danny Tuppeny
2018-07-19 13:38:29 +01:00
committed by GitHub
parent 442fc3cf06
commit ed9afbbc72
12 changed files with 204 additions and 67 deletions

View File

@@ -0,0 +1,43 @@
// Copyright 2018 The Chromium 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 'package:file/file.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:test/test.dart';
import '../src/context.dart';
import 'test_data/basic_project.dart';
import 'test_driver.dart';
FlutterTestDriver _flutterRun, _flutterAttach;
BasicProject _project = new BasicProject();
void main() {
setUp(() async {
final Directory tempDir = await fs.systemTempDirectory.createTemp('test_app');
await _project.setUpIn(tempDir);
_flutterRun = new FlutterTestDriver(tempDir);
_flutterAttach = new FlutterTestDriver(tempDir);
});
tearDown(() async {
try {
await _flutterRun.stop();
await _flutterAttach.stop();
_project.cleanup();
} catch (e) {
// Don't fail tests if we failed to clean up temp folder.
}
});
group('attached process', () {
testUsingContext('can hot reload', () async {
await _flutterRun.run(withDebugger: true);
await _flutterAttach.attach(_flutterRun.vmServicePort);
await _flutterAttach.hotReload();
});
}, timeout: const Timeout.factor(3));
}

View File

@@ -23,7 +23,7 @@ const Duration appStartTimeout = const Duration(seconds: 60);
const Duration quitTimeout = const Duration(seconds: 5);
class FlutterTestDriver {
Directory _projectFolder;
final Directory _projectFolder;
Process _proc;
int _procPid;
final StreamController<String> _stdout = new StreamController<String>.broadcast();
@@ -32,11 +32,14 @@ class FlutterTestDriver {
final StringBuffer _errorBuffer = new StringBuffer();
String _lastResponse;
String _currentRunningAppId;
Uri _vmServiceWsUri;
int _vmServicePort;
FlutterTestDriver(this._projectFolder);
VMServiceClient vmService;
String get lastErrorInfo => _errorBuffer.toString();
int get vmServicePort => _vmServicePort;
String _debugPrint(String msg) {
const int maxLength = 500;
@@ -47,13 +50,44 @@ class FlutterTestDriver {
print(truncatedMsg);
}
return msg;
}
}
// TODO(dantup): Is there a better way than spawning a proc? This breaks debugging..
// However, there's a lot of logic inside RunCommand that wouldn't be good
// to duplicate here.
Future<void> run({bool withDebugger = false}) async {
_proc = await _runFlutter(_projectFolder);
await _setupProcess(<String>[
'run',
'--machine',
'-d',
'flutter-tester',
], withDebugger: withDebugger);
}
Future<void> attach(int port, {bool withDebugger = false}) async {
await _setupProcess(<String>[
'attach',
'--machine',
'-d',
'flutter-tester',
'--debug-port',
'$port',
], withDebugger: withDebugger);
}
Future<void> _setupProcess(List<String> args, {bool withDebugger = false}) async {
final String flutterBin = fs.path.join(getFlutterRoot(), 'bin', 'flutter');
_debugPrint('Spawning flutter $args in ${_projectFolder.path}');
const ProcessManager _processManager = const LocalProcessManager();
_proc = await _processManager.start(
<String>[flutterBin]
.followedBy(args)
.followedBy(withDebugger ? <String>['--start-paused'] : <String>[])
.toList(),
workingDirectory: _projectFolder.path,
environment: <String, String>{'FLUTTER_TEST': 'true'});
_transformToLines(_proc.stdout).listen((String line) => _stdout.add(line));
_transformToLines(_proc.stderr).listen((String line) => _stderr.add(line));
@@ -76,14 +110,13 @@ class FlutterTestDriver {
timeout: appStartTimeout);
if (withDebugger) {
final Future<Map<String, dynamic>> debugPort = _waitFor(event: 'app.debugPort',
final Map<String, dynamic> debugPort = await _waitFor(event: 'app.debugPort',
timeout: appStartTimeout);
final String wsUriString = (await debugPort)['params']['wsUri'];
// Ensure the app is started before we try to connect to it.
await started;
final Uri uri = Uri.parse(wsUriString);
final String wsUriString = debugPort['params']['wsUri'];
_vmServiceWsUri = Uri.parse(wsUriString);
_vmServicePort = debugPort['params']['port'];
// Proxy the stream/sink for the VM Client so we can debugPrint it.
final StreamChannel<String> channel = new IOWebSocketChannel.connect(uri)
final StreamChannel<String> channel = new IOWebSocketChannel.connect(_vmServiceWsUri)
.cast<String>()
.changeStream((Stream<String> stream) => stream.map(_debugPrint))
.changeSink((StreamSink<String> sink) =>
@@ -155,26 +188,6 @@ class FlutterTestDriver {
return _proc.exitCode;
}
Future<Process> _runFlutter(Directory projectDir) async {
final String flutterBin = fs.path.join(getFlutterRoot(), 'bin', 'flutter');
final List<String> command = <String>[
flutterBin,
'run',
'--machine',
'-d',
'flutter-tester',
'--start-paused',
];
_debugPrint('Spawning $command in ${projectDir.path}');
const ProcessManager _processManager = const LocalProcessManager();
return _processManager.start(
command,
workingDirectory: projectDir.path,
environment: <String, String>{'FLUTTER_TEST': 'true'}
);
}
Future<void> addBreakpoint(String path, int line) async {
final VM vm = await vmService.getVM();
final VMIsolate isolate = await vm.isolates.first.load();