Add support for executing custom tools in place of Flutter for DAP (#94475)

This commit is contained in:
Danny Tuppeny
2021-12-15 21:29:05 +00:00
committed by GitHub
parent 79bc1bfa89
commit 7e1e98c356
7 changed files with 356 additions and 24 deletions

View File

@@ -0,0 +1,83 @@
// Copyright 2014 The Flutter 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 'dart:async';
import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/debug_adapters/flutter_adapter_args.dart';
import 'package:flutter_tools/src/globals.dart' as globals;
import 'package:test/test.dart';
import 'mocks.dart';
void main() {
group('flutter adapter', () {
setUpAll(() {
Cache.flutterRoot = '/fake/flutter';
});
test('includes toolArgs', () async {
final MockFlutterDebugAdapter adapter = MockFlutterDebugAdapter(fileSystem: globals.fs, platform: globals.platform);
final Completer<void> responseCompleter = Completer<void>();
final FlutterLaunchRequestArguments args = FlutterLaunchRequestArguments(
cwd: '/project',
program: 'foo.dart',
toolArgs: <String>['tool_arg'],
noDebug: true,
);
await adapter.configurationDoneRequest(MockRequest(), null, () {});
await adapter.launchRequest(MockRequest(), args, responseCompleter.complete);
await responseCompleter.future;
expect(adapter.executable, equals('/fake/flutter/bin/flutter'));
expect(adapter.processArgs, contains('tool_arg'));
});
group('includes customTool', () {
test('with no args replaced', () async {
final MockFlutterDebugAdapter adapter = MockFlutterDebugAdapter(fileSystem: globals.fs, platform: globals.platform);
final FlutterLaunchRequestArguments args = FlutterLaunchRequestArguments(
cwd: '/project',
program: 'foo.dart',
customTool: '/custom/flutter',
noDebug: true,
);
await adapter.configurationDoneRequest(MockRequest(), null, () {});
final Completer<void> responseCompleter = Completer<void>();
await adapter.launchRequest(MockRequest(), args, responseCompleter.complete);
await responseCompleter.future;
expect(adapter.executable, equals('/custom/flutter'));
// args should be in-tact
expect(adapter.processArgs, contains('--machine'));
});
test('with all args replaced', () async {
final MockFlutterDebugAdapter adapter = MockFlutterDebugAdapter(fileSystem: globals.fs, platform: globals.platform);
final FlutterLaunchRequestArguments args = FlutterLaunchRequestArguments(
cwd: '/project',
program: 'foo.dart',
customTool: '/custom/flutter',
customToolReplacesArgs: 9999, // replaces all built-in args
noDebug: true,
toolArgs: <String>['tool_args'], // should still be in args
);
await adapter.configurationDoneRequest(MockRequest(), null, () {});
final Completer<void> responseCompleter = Completer<void>();
await adapter.launchRequest(MockRequest(), args, responseCompleter.complete);
await responseCompleter.future;
expect(adapter.executable, equals('/custom/flutter'));
// normal built-in args are replaced by customToolReplacesArgs, but
// user-provided toolArgs are not.
expect(adapter.processArgs, isNot(contains('--machine')));
expect(adapter.processArgs, contains('tool_args'));
});
});
});
}

View File

@@ -0,0 +1,90 @@
// Copyright 2014 The Flutter 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 'dart:async';
import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/debug_adapters/flutter_adapter_args.dart';
import 'package:flutter_tools/src/globals.dart' as globals;
import 'package:test/test.dart';
import 'mocks.dart';
void main() {
group('flutter test adapter', () {
setUpAll(() {
Cache.flutterRoot = '/fake/flutter';
});
test('includes toolArgs', () async {
final MockFlutterTestDebugAdapter adapter = MockFlutterTestDebugAdapter(
fileSystem: globals.fs,
platform: globals.platform,
);
final Completer<void> responseCompleter = Completer<void>();
final MockRequest request = MockRequest();
final FlutterLaunchRequestArguments args = FlutterLaunchRequestArguments(
cwd: '/project',
program: 'foo.dart',
toolArgs: <String>['tool_arg'],
noDebug: true,
);
await adapter.configurationDoneRequest(request, null, () {});
await adapter.launchRequest(request, args, responseCompleter.complete);
await responseCompleter.future;
expect(adapter.executable, equals('/fake/flutter/bin/flutter'));
expect(adapter.processArgs, contains('tool_arg'));
});
group('includes customTool', () {
test('with no args replaced', () async {
final MockFlutterTestDebugAdapter adapter = MockFlutterTestDebugAdapter(fileSystem: globals.fs,
platform: globals.platform,);
final Completer<void> responseCompleter = Completer<void>();
final MockRequest request = MockRequest();
final FlutterLaunchRequestArguments args = FlutterLaunchRequestArguments(
cwd: '/project',
program: 'foo.dart',
customTool: '/custom/flutter',
noDebug: true,
);
await adapter.configurationDoneRequest(request, null, () {});
await adapter.launchRequest(request, args, responseCompleter.complete);
await responseCompleter.future;
expect(adapter.executable, equals('/custom/flutter'));
// args should be in-tact
expect(adapter.processArgs, contains('--machine'));
});
test('with all args replaced', () async {
final MockFlutterTestDebugAdapter adapter = MockFlutterTestDebugAdapter(fileSystem: globals.fs,
platform: globals.platform,);
final Completer<void> responseCompleter = Completer<void>();
final MockRequest request = MockRequest();
final FlutterLaunchRequestArguments args = FlutterLaunchRequestArguments(
cwd: '/project',
program: 'foo.dart',
customTool: '/custom/flutter',
customToolReplacesArgs: 9999, // replaces all built-in args
noDebug: true,
toolArgs: <String>['tool_args'], // should still be in args
);
await adapter.configurationDoneRequest(request, null, () {});
await adapter.launchRequest(request, args, responseCompleter.complete);
await responseCompleter.future;
expect(adapter.executable, equals('/custom/flutter'));
// normal built-in args are replaced by customToolReplacesArgs, but
// user-provided toolArgs are not.
expect(adapter.processArgs, isNot(contains('--machine')));
expect(adapter.processArgs, contains('tool_args'));
});
});
});
}

View File

@@ -0,0 +1,106 @@
// Copyright 2014 The Flutter 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 'dart:async';
import 'package:dds/dap.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/platform.dart';
import 'package:flutter_tools/src/debug_adapters/flutter_adapter.dart';
import 'package:flutter_tools/src/debug_adapters/flutter_test_adapter.dart';
/// A [FlutterDebugAdapter] that captures what process/args will be launched.
class MockFlutterDebugAdapter extends FlutterDebugAdapter {
factory MockFlutterDebugAdapter({
required FileSystem fileSystem,
required Platform platform,
}) {
final StreamController<List<int>> stdinController = StreamController<List<int>>();
final StreamController<List<int>> stdoutController = StreamController<List<int>>();
final ByteStreamServerChannel channel = ByteStreamServerChannel(stdinController.stream, stdoutController.sink, null);
return MockFlutterDebugAdapter._(
stdinController.sink,
stdoutController.stream,
channel,
fileSystem: fileSystem,
platform: platform,
);
}
MockFlutterDebugAdapter._(
this.stdin,
this.stdout,
ByteStreamServerChannel channel, {
required FileSystem fileSystem,
required Platform platform,
}) : super(channel, fileSystem: fileSystem, platform: platform);
final StreamSink<List<int>> stdin;
final Stream<List<int>> stdout;
late String executable;
late List<String> processArgs;
@override
Future<void> launchAsProcess(String executable, List<String> processArgs) async {
this.executable = executable;
this.processArgs = processArgs;
// Pretend we launched the app and got the app.started event so that
// launchRequest will complete.
appStartedCompleter.complete();
}
}
/// A [FlutterTestDebugAdapter] that captures what process/args will be launched.
class MockFlutterTestDebugAdapter extends FlutterTestDebugAdapter {
factory MockFlutterTestDebugAdapter({
required FileSystem fileSystem,
required Platform platform,
}) {
final StreamController<List<int>> stdinController = StreamController<List<int>>();
final StreamController<List<int>> stdoutController = StreamController<List<int>>();
final ByteStreamServerChannel channel = ByteStreamServerChannel(stdinController.stream, stdoutController.sink, null);
return MockFlutterTestDebugAdapter._(
stdinController.sink,
stdoutController.stream,
channel,
fileSystem: fileSystem,
platform: platform,
);
}
MockFlutterTestDebugAdapter._(
this.stdin,
this.stdout,
ByteStreamServerChannel channel, {
required FileSystem fileSystem,
required Platform platform,
}) : super(channel, fileSystem: fileSystem, platform: platform);
final StreamSink<List<int>> stdin;
final Stream<List<int>> stdout;
late String executable;
late List<String> processArgs;
@override
Future<void> launchAsProcess(String executable, List<String> processArgs,) async {
this.executable = executable;
this.processArgs = processArgs;
}
}
class MockRequest extends Request {
MockRequest()
: super.fromMap(<String, Object?>{
'command': 'mock_command',
'type': 'mock_type',
'seq': _requestId++,
});
static int _requestId = 1;
}