test.dart 3.2 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 '../globals.dart';
12
import '../runner/flutter_command.dart';
13
import '../test/flutter_platform.dart' as loader;
14
import '../toolchain.dart';
15 16

class TestCommand extends FlutterCommand {
Ian Hickson's avatar
Ian Hickson committed
17
  TestCommand() {
18
    usesPubOption();
Ian Hickson's avatar
Ian Hickson committed
19 20
  }

21
  @override
Ian Hickson's avatar
Ian Hickson committed
22
  String get name => 'test';
23 24

  @override
25
  String get description => 'Run Flutter unit tests for the current project.';
26 27

  @override
Ian Hickson's avatar
Ian Hickson committed
28
  bool get requiresProjectRoot => false;
29

30 31 32
  @override
  Validator projectRootValidator = () {
    if (!FileSystemEntity.isFileSync('pubspec.yaml')) {
33 34 35 36 37
      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).');
38 39 40 41
      return false;
    }
    return true;
  };
42 43 44

  Iterable<String> _findTests(Directory directory) {
    return directory.listSync(recursive: true, followLinks: false)
45 46
                    .where((FileSystemEntity entity) => entity.path.endsWith('_test.dart') &&
                      FileSystemEntity.isFileSync(entity.path))
47 48 49
                    .map((FileSystemEntity entity) => path.absolute(entity.path));
  }

50 51 52 53
  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');
54 55 56 57 58
  }

  Future<int> _runTests(List<String> testArgs, Directory testDirectory) async {
    Directory currentDirectory = Directory.current;
    try {
Ian Hickson's avatar
Ian Hickson committed
59 60 61 62 63 64 65 66
      if (testDirectory != null) {
        printTrace('switching to directory $testDirectory to run tests');
        Directory.current = testDirectory;
      }
      printTrace('running test package with arguments: $testArgs');
      await executable.main(testArgs);
      printTrace('test package returned with exit code $exitCode');
      return exitCode;
67 68 69 70 71
    } finally {
      Directory.current = currentDirectory;
    }
  }

72 73
  @override
  Future<int> runInProject() async {
74 75
    List<String> testArgs = argResults.rest.map((String testPath) => path.absolute(testPath)).toList();

76
    if (!projectRootValidator())
77 78
      return 1;

Ian Hickson's avatar
Ian Hickson committed
79
    Directory testDir;
80

81
    if (testArgs.isEmpty) {
Ian Hickson's avatar
Ian Hickson committed
82
      testDir = _currentPackageTestDir;
83 84 85 86 87
      if (!testDir.existsSync()) {
        printError("Test directory '${testDir.path}' not found.");
        return 1;
      }

88
      testArgs.addAll(_findTests(testDir));
89
    }
90

Ian Hickson's avatar
Ian Hickson committed
91 92 93
    testArgs.insert(0, '--');
    if (Platform.environment['TERM'] == 'dumb')
      testArgs.insert(0, '--no-color');
94

95
    loader.installHook();
96 97 98
    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
99 100
      return 1;
    }
101
    return await _runTests(testArgs, testDir);
102
  }
103
}