test.dart 2.76 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
// 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;

Ian Hickson's avatar
Ian Hickson committed
12 13
import '../artifacts.dart';
import '../build_configuration.dart';
14
import '../test/loader.dart' as loader;
Ian Hickson's avatar
Ian Hickson committed
15
import 'flutter_command.dart';
16

17
final Logger _logging = new Logger('flutter_tools.test');
18 19

class TestCommand extends FlutterCommand {
Ian Hickson's avatar
Ian Hickson committed
20 21
  String get name => 'test';
  String get description => 'Runs Flutter unit tests for the current project. At least one of --debug and --release must be set.';
22

Ian Hickson's avatar
Ian Hickson committed
23
  bool get requiresProjectRoot => false;
24

Ian Hickson's avatar
Ian Hickson committed
25 26 27 28 29 30 31 32 33
  String getShellPath(TargetPlatform platform, String buildPath) {
    switch (platform) {
      case TargetPlatform.linux:
        return path.join(buildPath, 'sky_shell');
      case TargetPlatform.mac:
        return path.join(buildPath, 'SkyShell.app', 'Contents', 'MacOS', 'SkyShell');
      default:
        throw new Exception('Unsupported platform.');
    }
34 35 36 37
  }

  @override
  Future<int> runInProject() async {
Ian Hickson's avatar
Ian Hickson committed
38
    List<String> testArgs = argResults.rest.toList();
39 40
    Directory flutterDir = new Directory(path.join(ArtifactStore.flutterRoot, 'packages/unit')); // see https://github.com/flutter/flutter/issues/50
    Directory testDir = new Directory(path.join(flutterDir.path, 'test'));
Ian Hickson's avatar
Ian Hickson committed
41 42 43 44
    if (testArgs.isEmpty) {
      testArgs.addAll(testDir.listSync(recursive: true, followLinks: false)
                             .where((FileSystemEntity entity) => entity.path.endsWith('_test.dart') && FileSystemEntity.isFileSync(entity.path))
                             .map((FileSystemEntity entity) => path.absolute(entity.path)));
45
    }
Ian Hickson's avatar
Ian Hickson committed
46 47 48 49 50 51
    testArgs.insert(0, '--');
    if (Platform.environment['TERM'] == 'dumb')
      testArgs.insert(0, '--no-color');
    List<BuildConfiguration> configs = buildConfigurations;
    bool foundOne = false;
    String currentDirectory = Directory.current.path;
52
    Directory.current = flutterDir.path;
53
    loader.installHook();
Ian Hickson's avatar
Ian Hickson committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
    for (BuildConfiguration config in configs) {
      if (!config.testable)
        continue;
      foundOne = true;
      loader.shellPath = path.join(currentDirectory, getShellPath(config.targetPlatform, config.buildDir));
      if (!FileSystemEntity.isFileSync(loader.shellPath)) {
          _logging.severe('Cannot find Flutter shell at ${loader.shellPath}');
        return 1;
      }
      await executable.main(testArgs);
      if (exitCode != 0)
        return exitCode;
    }
    if (!foundOne) {
      stderr.writeln('At least one of --debug or --release must be set, to specify the local build products to test.');
      return 1;
    }

    return 0;
73
  }
Ian Hickson's avatar
Ian Hickson committed
74
}