From fc8cdf4dafdde38a5b2e03bbadb5661819ce23cb Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Wed, 4 Nov 2015 23:43:15 -0800 Subject: [PATCH] Add a command for running unit tests --- packages/flutter_tools/lib/executable.dart | 2 + .../flutter_tools/lib/src/commands/test.dart | 44 +++++++++++++++++++ .../flutter_tools/lib/src/test/loader.dart | 10 +++-- 3 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 packages/flutter_tools/lib/src/commands/test.dart diff --git a/packages/flutter_tools/lib/executable.dart b/packages/flutter_tools/lib/executable.dart index 276ec02294..86b8f089a8 100644 --- a/packages/flutter_tools/lib/executable.dart +++ b/packages/flutter_tools/lib/executable.dart @@ -21,6 +21,7 @@ import 'src/commands/logs.dart'; import 'src/commands/run_mojo.dart'; import 'src/commands/start.dart'; import 'src/commands/stop.dart'; +import 'src/commands/test.dart'; import 'src/commands/trace.dart'; import 'src/process.dart'; @@ -54,6 +55,7 @@ Future main(List args) async { ..addCommand(new RunMojoCommand()) ..addCommand(new StartCommand()) ..addCommand(new StopCommand()) + ..addCommand(new TestCommand()) ..addCommand(new TraceCommand()); return Chain.capture(() async { diff --git a/packages/flutter_tools/lib/src/commands/test.dart b/packages/flutter_tools/lib/src/commands/test.dart new file mode 100644 index 0000000000..3d70ec38eb --- /dev/null +++ b/packages/flutter_tools/lib/src/commands/test.dart @@ -0,0 +1,44 @@ +// Copyright 2015 The Chromium 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 'dart:io'; + +import 'package:logging/logging.dart'; +import 'package:path/path.dart' as path; +import 'package:test/src/executable.dart' as executable; + +import 'flutter_command.dart'; +import '../test/loader.dart' as loader; + +final Logger _logging = new Logger('sky_tools.test'); + +class TestCommand extends FlutterCommand { + final String name = 'test'; + final String description = 'Runs Flutter unit tests for the current project (requires a local build of the engine).'; + + TestCommand() { + argParser.addOption('build-dir', defaultsTo: '../../../engine/src/out/Debug'); + } + + String get _shellPath { + if (Platform.isLinux) + return path.join(argResults['build-dir'], 'sky_shell'); + if (Platform.isMacOS) + return path.join(argResults['build-dir'], 'SkyShell.app', 'Contents', 'MacOS', 'SkyShell'); + throw new Exception('Unsupported platform.'); + } + + @override + Future runInProject() async { + loader.shellPath = _shellPath; + if (!FileSystemEntity.isFileSync(loader.shellPath)) { + _logging.severe('Cannot find Flutter Shell at ${loader.shellPath}'); + return 1; + } + loader.installHook(); + await executable.main(argResults.rest); + return exitCode; + } +} diff --git a/packages/flutter_tools/lib/src/test/loader.dart b/packages/flutter_tools/lib/src/test/loader.dart index 0b6e090f9f..4ad9583ecf 100644 --- a/packages/flutter_tools/lib/src/test/loader.dart +++ b/packages/flutter_tools/lib/src/test/loader.dart @@ -29,8 +29,10 @@ final String _kSkyShell = Platform.environment['SKY_SHELL']; const String _kHost = '127.0.0.1'; const String _kPath = '/runner'; +String shellPath; + // Right now a bunch of our tests crash or assert after the tests have finished running. -// Mostly this is just because the test puts the framework in an inconsistent state with +// Mostly this is just because the test puts the framework in an inconsistent state with // a scheduled microtask that verifies that state. Eventually we should fix all these // problems but for now we'll just paper over them. const bool kExpectAllTestsToCloseCleanly = false; @@ -54,8 +56,8 @@ Future<_ServerInfo> _createServer() async { } Future _startProcess(String path, { String packageRoot }) { - assert(_kSkyShell != null); // Please provide the path to the shell in the SKY_SHELL environment variable. - return Process.start(_kSkyShell, [ + assert(shellPath != null || _kSkyShell != null); // Please provide the path to the shell in the SKY_SHELL environment variable. + return Process.start(shellPath ?? _kSkyShell, [ '--enable-checked-mode', '--non-interactive', '--package-root=$packageRoot', @@ -172,7 +174,7 @@ void main() { completer.complete(response["tests"].map((test) { var testMetadata = new Metadata.deserialize(test['metadata']); return new RemoteTest(test['name'], testMetadata, socket, test['index']); - })); + })); } });