test.dart 6.42 KB
Newer Older
1 2 3 4 5 6 7 8
// 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:path/path.dart' as path;
Ian Hickson's avatar
Ian Hickson committed
9
import 'package:test/src/executable.dart' as executable; // ignore: implementation_imports
10

11
import '../base/logger.dart';
12
import '../base/os.dart';
13
import '../cache.dart';
14
import '../dart/package_map.dart';
15
import '../globals.dart';
16
import '../runner/flutter_command.dart';
17
import '../test/coverage_collector.dart';
18
import '../test/flutter_platform.dart' as loader;
19
import '../toolchain.dart';
20 21

class TestCommand extends FlutterCommand {
Ian Hickson's avatar
Ian Hickson committed
22
  TestCommand() {
23
    usesPubOption();
24 25
    argParser.addFlag('coverage',
      defaultsTo: false,
26
      negatable: false,
27 28
      help: 'Whether to collect coverage information.'
    );
29 30 31 32
    argParser.addFlag('merge-coverage',
      defaultsTo: false,
      negatable: false,
      help: 'Whether to merge converage data with "coverage/lcov.base.info". '
33
            'Implies collecting coverage data. (Requires lcov)'
34
    );
35 36 37 38
    argParser.addOption('coverage-path',
      defaultsTo: 'coverage/lcov.info',
      help: 'Where to store coverage information (if coverage is enabled).'
    );
Ian Hickson's avatar
Ian Hickson committed
39 40
  }

41
  @override
Ian Hickson's avatar
Ian Hickson committed
42
  String get name => 'test';
43 44

  @override
45
  String get description => 'Run Flutter unit tests for the current project.';
46 47

  @override
Ian Hickson's avatar
Ian Hickson committed
48
  bool get requiresProjectRoot => false;
49

50 51 52
  @override
  Validator projectRootValidator = () {
    if (!FileSystemEntity.isFileSync('pubspec.yaml')) {
53 54 55 56 57
      printError(
        'Error: No pubspec.yaml file found in the current working directory.\n'
        'Run this command from the root of your project. Test files must be\n'
        'called *_test.dart and must reside in the package\'s \'test\'\n'
        'directory (or one of its subdirectories).');
58 59 60 61
      return false;
    }
    return true;
  };
62 63 64

  Iterable<String> _findTests(Directory directory) {
    return directory.listSync(recursive: true, followLinks: false)
65 66
                    .where((FileSystemEntity entity) => entity.path.endsWith('_test.dart') &&
                      FileSystemEntity.isFileSync(entity.path))
67 68 69
                    .map((FileSystemEntity entity) => path.absolute(entity.path));
  }

70 71 72 73
  Directory get _currentPackageTestDir {
    // We don't scan the entire package, only the test/ subdirectory, so that
    // files with names like like "hit_test.dart" don't get run.
    return new Directory('test');
74 75 76 77 78
  }

  Future<int> _runTests(List<String> testArgs, Directory testDirectory) async {
    Directory currentDirectory = Directory.current;
    try {
Ian Hickson's avatar
Ian Hickson committed
79 80
      if (testDirectory != null) {
        printTrace('switching to directory $testDirectory to run tests');
81
        PackageMap.globalPackagesPath = path.normalize(path.absolute(PackageMap.globalPackagesPath));
Ian Hickson's avatar
Ian Hickson committed
82 83 84 85 86
        Directory.current = testDirectory;
      }
      printTrace('running test package with arguments: $testArgs');
      await executable.main(testArgs);
      printTrace('test package returned with exit code $exitCode');
87

Ian Hickson's avatar
Ian Hickson committed
88
      return exitCode;
89 90 91 92 93
    } finally {
      Directory.current = currentDirectory;
    }
  }

94 95 96 97
  Future<bool> _collectCoverageData(CoverageCollector collector, { bool mergeCoverageData: false }) async {
    Status status = logger.startProgress('Collecting coverage information...');
    String coverageData = await collector.finalizeCoverage();
    status.stop(showElapsedTime: true);
98 99
    if (coverageData == null)
      return false;
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122

    String coveragePath = argResults['coverage-path'];
    File coverageFile = new File(coveragePath)
      ..createSync(recursive: true)
      ..writeAsStringSync(coverageData, flush: true);
    printTrace('wrote coverage data to $coveragePath (size=${coverageData.length})');

    String baseCoverageData = 'coverage/lcov.base.info';
    if (mergeCoverageData) {
      if (!os.isLinux) {
        printError(
          'Merging coverage data is supported only on Linux because it '
          'requires the "lcov" tool.'
        );
        return false;
      }

      if (!FileSystemEntity.isFileSync(baseCoverageData)) {
        printError('Missing "$baseCoverageData". Unable to merge coverage data.');
        return false;
      }

      if (os.which('lcov') == null) {
123 124 125 126 127 128
        String installMessage = 'Please install lcov.';
        if (os.isLinux)
          installMessage = 'Consider running "sudo apt-get install lcov".';
        else if (os.isMacOS)
          installMessage = 'Consider running "brew install lcov".';
        printError('Missing "lcov" tool. Unable to merge coverage data.\n$installMessage');
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
        return false;
      }

      Directory tempDir = Directory.systemTemp.createTempSync('flutter_tools');
      try {
        File sourceFile = coverageFile.copySync(path.join(tempDir.path, 'lcov.source.info'));
        ProcessResult result = Process.runSync('lcov', <String>[
          '--add-tracefile', baseCoverageData,
          '--add-tracefile', sourceFile.path,
          '--output-file', coverageFile.path,
        ]);
        if (result.exitCode != 0)
          return false;
      } finally {
        tempDir.deleteSync(recursive: true);
      }
    }
    return true;
  }

149 150
  @override
  Future<int> runInProject() async {
151 152
    List<String> testArgs = argResults.rest.map((String testPath) => path.absolute(testPath)).toList();

153
    if (!projectRootValidator())
154 155
      return 1;

Ian Hickson's avatar
Ian Hickson committed
156
    Directory testDir;
157

158
    if (testArgs.isEmpty) {
Ian Hickson's avatar
Ian Hickson committed
159
      testDir = _currentPackageTestDir;
160 161 162 163 164
      if (!testDir.existsSync()) {
        printError("Test directory '${testDir.path}' not found.");
        return 1;
      }

165
      testArgs.addAll(_findTests(testDir));
166
    }
167

Ian Hickson's avatar
Ian Hickson committed
168
    testArgs.insert(0, '--');
169
    if (!terminal.supportsColor)
170
      testArgs.insertAll(0, <String>['--no-color', '-rexpanded']);
171

172 173 174
    if (argResults['coverage'])
      testArgs.insert(0, '--concurrency=1');

175
    loader.installHook();
176 177 178
    loader.shellPath = tools.getHostToolPath(HostTool.SkyShell);
    if (!FileSystemEntity.isFileSync(loader.shellPath)) {
        printError('Cannot find Flutter shell at ${loader.shellPath}');
Ian Hickson's avatar
Ian Hickson committed
179 180
      return 1;
    }
181 182 183

    Cache.releaseLockEarly();

184
    CoverageCollector collector = CoverageCollector.instance;
185
    collector.enabled = argResults['coverage'] || argResults['merge-coverage'];
186 187 188 189

    int result = await _runTests(testArgs, testDir);

    if (collector.enabled) {
190 191
      if (!await _collectCoverageData(collector, mergeCoverageData: argResults['merge-coverage']))
        return 1;
192 193 194
    }

    return result;
195
  }
196
}