test.dart 2.93 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 (Linux only).';
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 59 60 61 62 63 64 65
  }

  Future<int> _runTests(List<String> testArgs, Directory testDirectory) async {
    Directory currentDirectory = Directory.current;
    try {
      Directory.current = testDirectory;
      return await executable.main(testArgs);
    } finally {
      Directory.current = currentDirectory;
    }
  }

66 67
  @override
  Future<int> runInProject() async {
68 69
    List<String> testArgs = argResults.rest.map((String testPath) => path.absolute(testPath)).toList();

70
    if (!projectRootValidator())
71 72
      return 1;

73
    Directory testDir = _currentPackageTestDir;
74

75 76 77 78 79 80
    if (testArgs.isEmpty) {
      if (!testDir.existsSync()) {
        printError("Test directory '${testDir.path}' not found.");
        return 1;
      }

81
      testArgs.addAll(_findTests(testDir));
82
    }
83

Ian Hickson's avatar
Ian Hickson committed
84 85 86
    testArgs.insert(0, '--');
    if (Platform.environment['TERM'] == 'dumb')
      testArgs.insert(0, '--no-color');
87

88
    loader.installHook();
89 90 91
    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
92 93
      return 1;
    }
94
    return await _runTests(testArgs, testDir);
95
  }
96
}