From 784b17dd24d03b1a2f8860928f1bba248b976b31 Mon Sep 17 00:00:00 2001 From: Jaime Wren Date: Wed, 18 Mar 2020 18:05:26 +0000 Subject: [PATCH] =?UTF-8?q?Fix=20the=20output=20when=20running=20`dart=20h?= =?UTF-8?q?elp=20pub`.=C2=A0=20Process.runSync(..)=20is=20used=20since=20`?= =?UTF-8?q?printUsage()`=20is=20not=20an=20async=20method.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bug: https://github.com/dart-lang/sdk/issues/41040 Change-Id: I9e4fbab333bbd67dcab2b1e0752fc0debb80be46 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/139833 Commit-Queue: Jaime Wren Reviewed-by: Devon Carew --- pkg/dartdev/lib/src/commands/pub.dart | 19 +++++++++++++++++ pkg/dartdev/test/commands/help_test.dart | 26 ++++++++++++++++++++++++ pkg/dartdev/test/test_all.dart | 4 +++- 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 pkg/dartdev/test/commands/help_test.dart diff --git a/pkg/dartdev/lib/src/commands/pub.dart b/pkg/dartdev/lib/src/commands/pub.dart index acc8e398616..1b24c6567f5 100644 --- a/pkg/dartdev/lib/src/commands/pub.dart +++ b/pkg/dartdev/lib/src/commands/pub.dart @@ -15,6 +15,25 @@ class PubCommand extends DartdevCommand { final ArgParser argParser = ArgParser.allowAnything(); + /// Override [printUsage] for invocations of 'dart help pub' which won't + /// execute [run] below. Without this, the 'dart help pub' reports the + /// command pub with no commands or flags. + @override + void printUsage() { + final command = sdk.pub; + final args = ['help']; + + log.trace('$command ${args.first}'); + + // Call 'pub help' + // Process.runSync(..) is used since [printUsage] is not an async method, + // and we want to guarantee that the result (the help text for the console) + // is printed before command exits. + final result = Process.runSync(command, args); + stderr.write(result.stderr); + stdout.write(result.stdout); + } + @override FutureOr run() async { final command = sdk.pub; diff --git a/pkg/dartdev/test/commands/help_test.dart b/pkg/dartdev/test/commands/help_test.dart new file mode 100644 index 00000000000..0c16d3e4a10 --- /dev/null +++ b/pkg/dartdev/test/commands/help_test.dart @@ -0,0 +1,26 @@ +// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file +// for details. 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:test/test.dart'; + +import '../utils.dart'; + +void main() { + group('help', help); +} + +void help() { + TestProject p; + + tearDown(() => p?.dispose()); + + test('pub', () { + p = project(); + var result = p.runSync('help', ['pub']); + + var pubHelpResult = p.runSync('pub', ['help']); + expect(result.stdout, contains(pubHelpResult.stdout)); + expect(result.stderr, contains(pubHelpResult.stderr)); + }); +} diff --git a/pkg/dartdev/test/test_all.dart b/pkg/dartdev/test/test_all.dart index 552691c3a47..4b9ccaad86f 100644 --- a/pkg/dartdev/test/test_all.dart +++ b/pkg/dartdev/test/test_all.dart @@ -8,6 +8,7 @@ import 'commands/analyze_test.dart' as analyze; import 'commands/create_test.dart' as create; import 'commands/flag_test.dart' as flag; import 'commands/format_test.dart' as format; +import 'commands/help_test.dart' as help; import 'commands/migrate_test.dart' as migrate; import 'commands/pub_test.dart' as pub; import 'commands/test_test.dart' as test; @@ -16,11 +17,12 @@ import 'sdk_test.dart' as sdk; import 'utils_test.dart' as utils; main() { - group('dartdev', () { + group('dart', () { analyze.main(); create.main(); flag.main(); format.main(); + help.main(); migrate.main(); pub.main(); test.main();