[dartdev] add a 'test' command
Change-Id: I5f1b509bb7dcc2f415656ed06afe14f2274ea358 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/138061 Commit-Queue: Devon Carew <devoncarew@google.com> Reviewed-by: Jaime Wren <jwren@google.com>
This commit is contained in:
committed by
commit-bot@chromium.org
parent
ca3ad264a6
commit
1fe6ca96d0
@@ -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<int> extends CommandRunner {
|
||||
@@ -28,6 +29,7 @@ class DartdevRunner<int> extends CommandRunner {
|
||||
addCommand(FormatCommand(verbose: verbose));
|
||||
addCommand(MigrateCommand(logProvider: () => log));
|
||||
addCommand(PubCommand(verbose: verbose));
|
||||
addCommand(TestCommand(verbose: verbose));
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -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<int> {
|
||||
TestCommand({bool verbose = false}) : super('test', 'todo: .');
|
||||
|
||||
final ArgParser argParser = ArgParser.allowAnything();
|
||||
|
||||
@override
|
||||
FutureOr<int> 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.''');
|
||||
}
|
||||
}
|
||||
@@ -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');
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user