From 147020366a55690390c4fae4ff3fc827163335df Mon Sep 17 00:00:00 2001 From: Danny Tuppeny Date: Thu, 26 May 2022 09:36:30 +0100 Subject: [PATCH] Fix issues running integration tests through DAP debug adapter (#104618) * Fix issues running integration tests through DAP These adapters were incorrectly trying to connect a DDS instance even when Flutter would create its own. This change disables DDS in the DAP layer and leaves it to Flutter (although it passes `--no-dds` on to Flutter if provided to the DAP process). Also fixes an issue where we would unnecessarily connect the VM Service for tests even in 'noDebug' mode because of a change/fix that now includes a 'vmServiceUri' in the `test.startedProcess` event. --- .../src/debug_adapters/flutter_adapter.dart | 15 ++- .../debug_adapters/flutter_test_adapter.dart | 37 +++++++- .../debug_adapter/test_adapter_test.dart | 94 +++++++++++-------- .../debug_adapter/test_server.dart | 1 + .../test_data/integration_tests_project.dart | 68 ++++++++++++++ 5 files changed, 170 insertions(+), 45 deletions(-) create mode 100644 packages/flutter_tools/test/integration.shard/test_data/integration_tests_project.dart diff --git a/packages/flutter_tools/lib/src/debug_adapters/flutter_adapter.dart b/packages/flutter_tools/lib/src/debug_adapters/flutter_adapter.dart index 6b0991686d..b4723326f1 100644 --- a/packages/flutter_tools/lib/src/debug_adapters/flutter_adapter.dart +++ b/packages/flutter_tools/lib/src/debug_adapters/flutter_adapter.dart @@ -25,15 +25,24 @@ class FlutterDebugAdapter extends DartDebugAdapter obj) parseLaunchArgs = FlutterLaunchRequestArguments.fromJson; @@ -136,6 +145,7 @@ class FlutterDebugAdapter extends DartDebugAdapter toolArgs = [ 'attach', '--machine', + if (!_enableDds) '--no-dds', if (vmServiceUri != null) ...['--debug-uri', vmServiceUri], ]; @@ -249,6 +259,7 @@ class FlutterDebugAdapter extends DartDebugAdapter toolArgs = [ 'run', '--machine', + if (!_enableDds) '--no-dds', if (enableDebugger) '--start-paused', // Structured errors are enabled by default, but since we don't connect // the VM Service for noDebug, we need to disable them so that error text diff --git a/packages/flutter_tools/lib/src/debug_adapters/flutter_test_adapter.dart b/packages/flutter_tools/lib/src/debug_adapters/flutter_test_adapter.dart index 2c8955ba63..1f0f77416e 100644 --- a/packages/flutter_tools/lib/src/debug_adapters/flutter_test_adapter.dart +++ b/packages/flutter_tools/lib/src/debug_adapters/flutter_test_adapter.dart @@ -25,15 +25,24 @@ class FlutterTestDebugAdapter extends DartDebugAdapter obj) parseLaunchArgs = FlutterLaunchRequestArguments.fromJson; @@ -78,6 +87,22 @@ class FlutterTestDebugAdapter extends DartDebugAdapter launchImpl() async { final FlutterLaunchRequestArguments args = this.args as FlutterLaunchRequestArguments; - final bool debug = !(args.noDebug ?? false); + final bool debug = enableDebugger; final String? program = args.program; final List toolArgs = [ 'test', '--machine', + if (!_enableDds) '--no-dds', if (debug) '--start-paused', ]; @@ -207,8 +233,9 @@ class FlutterTestDebugAdapter extends DartDebugAdapter params) { final String? vmServiceUriString = params['observatoryUri'] as String?; - // For no-debug mode, this event is still sent, but has a null URI. - if (vmServiceUriString == null) { + // For no-debug mode, this event may be still sent so ignore it if we know + // we're not debugging, or its URI is null. + if (!enableDebugger || vmServiceUriString == null) { return; } final Uri vmServiceUri = Uri.parse(vmServiceUriString); diff --git a/packages/flutter_tools/test/integration.shard/debug_adapter/test_adapter_test.dart b/packages/flutter_tools/test/integration.shard/debug_adapter/test_adapter_test.dart index b3c2fe67dd..cc16010c70 100644 --- a/packages/flutter_tools/test/integration.shard/debug_adapter/test_adapter_test.dart +++ b/packages/flutter_tools/test/integration.shard/debug_adapter/test_adapter_test.dart @@ -7,6 +7,7 @@ import 'package:file/file.dart'; import 'package:flutter_tools/src/cache.dart'; import '../../src/common.dart'; +import '../test_data/integration_tests_project.dart'; import '../test_data/tests_project.dart'; import '../test_utils.dart'; import 'test_client.dart'; @@ -15,6 +16,8 @@ import 'test_support.dart'; void main() { late Directory tempDir; late DapTestSession dap; + late DapTestClient client; + late TestsProject project; setUpAll(() { Cache.flutterRoot = getFlutterRoot(); @@ -23,6 +26,7 @@ void main() { setUp(() async { tempDir = createResolvedTempDirectorySync('flutter_test_adapter_test.'); dap = await DapTestSession.setUp(additionalArgs: ['--test']); + client = dap.client; }); tearDown(() async { @@ -30,45 +34,40 @@ void main() { tryToDelete(tempDir); }); - test('can run in debug mode', () async { - final DapTestClient client = dap.client; - final TestsProject project = TestsProject(); - await project.setUpIn(tempDir); + void standardTests({List? toolArgs}) { + test('can run in debug mode', () async { + // Collect output and test events while running the script. + final TestEvents outputEvents = await client.collectTestOutput( + launch: () => client.launch( + program: project.testFilePath, + cwd: project.dir.path, + toolArgs: toolArgs, + ), + ); - // Collect output and test events while running the script. - final TestEvents outputEvents = await client.collectTestOutput( - launch: () => client.launch( - program: project.testFilePath, - cwd: project.dir.path, - ), - ); + // Check the printed output shows that the run finished, and it's exit + // code (which is 1 due to the failing test). + final String output = outputEvents.output.map((OutputEventBody e) => e.output).join(); + expectLines( + output, + [ + startsWith('Connecting to VM Service at'), + ..._testsProjectExpectedOutput, + ], + allowExtras: true, // Allow for printed call stack etc. + ); - // Check the printed output shows that the run finished, and it's exit - // code (which is 1 due to the failing test). - final String output = outputEvents.output.map((OutputEventBody e) => e.output).join(); - expectLines( - output, - [ - startsWith('Connecting to VM Service at'), - ..._testsProjectExpectedOutput, - ], - allowExtras: true, // Allow for printed call stack etc. - ); - - _expectStandardTestsProjectResults(outputEvents); - }); + _expectStandardTestsProjectResults(outputEvents); + }); test('can run in noDebug mode', () async { - final DapTestClient client = dap.client; - final TestsProject project = TestsProject(); - await project.setUpIn(tempDir); - // Collect output and test events while running the script. final TestEvents outputEvents = await client.collectTestOutput( launch: () => client.launch( program: project.testFilePath, noDebug: true, cwd: project.dir.path, + toolArgs: toolArgs, ), ); @@ -85,21 +84,18 @@ void main() { }); test('can run a single test', () async { - final DapTestClient client = dap.client; - final TestsProject project = TestsProject(); - await project.setUpIn(tempDir); - // Collect output and test events while running the script. final TestEvents outputEvents = await client.collectTestOutput( launch: () => client.launch( program: project.testFilePath, noDebug: true, cwd: project.dir.path, - // It's up to the calling IDE to pass the correct args for 'dart test' - // if it wants to run a subset of tests. - args: [ + // It's up to the calling IDE to pass the correct args for + // 'flutter test' if it wants to run a subset of tests. + toolArgs: [ '--plain-name', 'can pass', + ...?toolArgs, ], ), ); @@ -112,6 +108,27 @@ void main() { expect(testsNames, contains('Flutter tests can pass')); expect(testsNames, isNot(contains('Flutter tests can fail'))); }); + } + + group('widget tests', () { + setUp(() async { + project = TestsProject(); + await project.setUpIn(tempDir); + }); + + standardTests(); + }); + + group('integration tests', () { + const List toolArgs = ['-d', 'flutter-tester']; + + setUp(() async { + project = IntegrationTestsProject(); + await project.setUpIn(tempDir); + }); + + standardTests(toolArgs: toolArgs); + }); } /// Matchers for the expected console output of [TestsProject]. @@ -137,8 +154,9 @@ final List _testsProjectExpectedOutput = [ void _expectStandardTestsProjectResults(TestEvents events) { // Check we received all expected test events passed through from // package:test. - final List eventNames = - events.testNotifications.map((Map e) => e['type']!).toList(); + final List eventNames = events.testNotifications + .map((Map e) => e['type']!) + .toList(); // start/done should always be first/last. expect(eventNames.first, equals('start')); diff --git a/packages/flutter_tools/test/integration.shard/debug_adapter/test_server.dart b/packages/flutter_tools/test/integration.shard/debug_adapter/test_server.dart index 39a3034bf5..43be68bf42 100644 --- a/packages/flutter_tools/test/integration.shard/debug_adapter/test_server.dart +++ b/packages/flutter_tools/test/integration.shard/debug_adapter/test_server.dart @@ -36,6 +36,7 @@ class InProcessDapTestServer extends DapTestServer { // Simulate flags based on the args to aid testing. enableDds: !args.contains('--no-dds'), ipv6: args.contains('--ipv6'), + test: args.contains('--test'), ); } diff --git a/packages/flutter_tools/test/integration.shard/test_data/integration_tests_project.dart b/packages/flutter_tools/test/integration.shard/test_data/integration_tests_project.dart new file mode 100644 index 0000000000..c1cb36516d --- /dev/null +++ b/packages/flutter_tools/test/integration.shard/test_data/integration_tests_project.dart @@ -0,0 +1,68 @@ +// 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 'package:file/file.dart'; +import 'package:flutter_tools/src/base/file_system.dart'; + +import '../test_utils.dart'; +import 'project.dart'; +import 'tests_project.dart'; + +class IntegrationTestsProject extends Project implements TestsProject { + @override + final String pubspec = ''' + name: test + environment: + sdk: '>=2.12.0-0 <3.0.0' + + dependencies: + flutter: + sdk: flutter + + dev_dependencies: + integration_test: + sdk: flutter + flutter_test: + sdk: flutter + '''; + + @override + String get main => '// Unused'; + + @override + final String testContent = r''' + import 'package:flutter_test/flutter_test.dart'; + import 'package:integration_test/integration_test.dart'; + + void main() { + group('Flutter tests', () { + testWidgets('can pass', (WidgetTester tester) async { + expect(true, isTrue); // BREAKPOINT + }); + testWidgets('can fail', (WidgetTester tester) async { + expect(true, isFalse); + }); + }); + } + '''; + + @override + Future setUpIn(Directory dir) { + this.dir = dir; + writeFile(testFilePath, testContent); + return super.setUpIn(dir); + } + + @override + String get testFilePath => fileSystem.path.join(dir.path, 'integration_test', 'app_test.dart'); + + @override + Uri get breakpointUri => throw UnimplementedError(); + + @override + Uri get breakpointAppUri => throw UnimplementedError(); + + @override + int get breakpointLine => throw UnimplementedError(); +}