From 1fe6ca96d0665fe57af0d14a156aa97b361f480c Mon Sep 17 00:00:00 2001 From: Devon Carew Date: Tue, 3 Mar 2020 00:06:26 +0000 Subject: [PATCH] [dartdev] add a 'test' command Change-Id: I5f1b509bb7dcc2f415656ed06afe14f2274ea358 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/138061 Commit-Queue: Devon Carew Reviewed-by: Jaime Wren --- pkg/dartdev/lib/dartdev.dart | 2 + pkg/dartdev/lib/src/commands/test.dart | 59 ++++++++++++++++++++ pkg/dartdev/test/commands/test_test.dart | 68 ++++++++++++++++++++++++ pkg/dartdev/test/test_all.dart | 2 + 4 files changed, 131 insertions(+) create mode 100644 pkg/dartdev/lib/src/commands/test.dart create mode 100644 pkg/dartdev/test/commands/test_test.dart diff --git a/pkg/dartdev/lib/dartdev.dart b/pkg/dartdev/lib/dartdev.dart index d3934f7e685..67137c21b08 100644 --- a/pkg/dartdev/lib/dartdev.dart +++ b/pkg/dartdev/lib/dartdev.dart @@ -11,6 +11,7 @@ import 'src/commands/analyze.dart'; import 'src/commands/create.dart'; import 'src/commands/format.dart'; import 'src/commands/pub.dart'; +import 'src/commands/test.dart'; import 'src/core.dart'; class DartdevRunner extends CommandRunner { @@ -28,6 +29,7 @@ class DartdevRunner extends CommandRunner { addCommand(FormatCommand(verbose: verbose)); addCommand(MigrateCommand(logProvider: () => log)); addCommand(PubCommand(verbose: verbose)); + addCommand(TestCommand(verbose: verbose)); } @override diff --git a/pkg/dartdev/lib/src/commands/test.dart b/pkg/dartdev/lib/src/commands/test.dart new file mode 100644 index 00000000000..da856748571 --- /dev/null +++ b/pkg/dartdev/lib/src/commands/test.dart @@ -0,0 +1,59 @@ +// 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 'dart:async'; +import 'dart:io'; + +import 'package:args/args.dart'; + +import '../core.dart'; +import '../sdk.dart'; + +class TestCommand extends DartdevCommand { + TestCommand({bool verbose = false}) : super('test', 'todo: .'); + + final ArgParser argParser = ArgParser.allowAnything(); + + @override + FutureOr run() async { + final command = sdk.pub; + final args = argResults.arguments.toList(); + + args.insertAll(0, ['run', 'test']); + + log.trace('$command ${args.join(' ')}'); + + // Starting in ProcessStartMode.inheritStdio mode means the child process + // can detect support for ansi chars. + var process = + await Process.start(command, args, mode: ProcessStartMode.inheritStdio); + + int exitCode = await process.exitCode; + + // "Could not find package "test". Did you forget to add a dependency?" + if (exitCode == 65 && project.hasPackageConfigFile) { + if (!project.packageConfig.hasDependency('test')) { + _printPackageTestInstructions(); + } + } + + return exitCode; + } + + void _printPackageTestInstructions() { + log.stdout(''); + + final ansi = log.ansi; + + log.stdout(''' +In order to run tests, you need to add a dependency on package:test in your +pubspec.yaml file: + +${ansi.emphasized('dev_dependencies:\n test: ^1.0.0')} + +See https://pub.dev/packages/test#-installing-tab- for more information on +adding package:test, and https://dart.dev/guides/testing for general +information on testing.'''); + } +} diff --git a/pkg/dartdev/test/commands/test_test.dart b/pkg/dartdev/test/commands/test_test.dart new file mode 100644 index 00000000000..e1e9b5cffa1 --- /dev/null +++ b/pkg/dartdev/test/commands/test_test.dart @@ -0,0 +1,68 @@ +// 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('test', defineTest); +} + +void defineTest() { + TestProject p; + + tearDown(() => p?.dispose()); + + test('--help', () { + p = project(); + + var result = p.runSync('pub', ['get', '--offline']); + expect(result.exitCode, 0); + + result = p.runSync('test', ['--help']); + + expect(result.exitCode, 0); + expect(result.stdout, contains('Runs tests in this package')); + expect(result.stderr, isEmpty); + }, skip: 'https://github.com/dart-lang/sdk/issues/40854'); + + test('no dependency', () { + p = project(mainSrc: 'int get foo => 1;\n'); + p.file('pubspec.yaml', 'name: ${p.name}\n'); + + var result = p.runSync('pub', ['get', '--offline']); + expect(result.exitCode, 0); + + result = p.runSync('test', []); + expect(result.exitCode, 65); + expect( + result.stdout, + contains( + 'In order to run tests, you need to add a dependency on package:test', + ), + ); + }, skip: 'https://github.com/dart-lang/sdk/issues/40854'); + + test('has dependency', () { + p = project(mainSrc: 'int get foo => 1;\n'); + p.file('test/foo_test.dart', ''' +import 'package:test/test.dart'; + +void main() { + test('', () { + print('hello world'); + }); +} +'''); + + var result = p.runSync('pub', ['get', '--offline']); + expect(result.exitCode, 0); + + result = p.runSync('test', ['--no-color', '--reporter', 'expanded']); + expect(result.exitCode, 0); + expect(result.stdout, contains('All tests passed!')); + expect(result.stderr, isEmpty); + }, skip: 'https://github.com/dart-lang/sdk/issues/40854'); +} diff --git a/pkg/dartdev/test/test_all.dart b/pkg/dartdev/test/test_all.dart index c7bc60b5a61..552691c3a47 100644 --- a/pkg/dartdev/test/test_all.dart +++ b/pkg/dartdev/test/test_all.dart @@ -10,6 +10,7 @@ import 'commands/flag_test.dart' as flag; import 'commands/format_test.dart' as format; import 'commands/migrate_test.dart' as migrate; import 'commands/pub_test.dart' as pub; +import 'commands/test_test.dart' as test; import 'core_test.dart' as core; import 'sdk_test.dart' as sdk; import 'utils_test.dart' as utils; @@ -22,6 +23,7 @@ main() { format.main(); migrate.main(); pub.main(); + test.main(); core.main(); sdk.main(); utils.main();